1、启动ES
参考文献: 从零搭建Web所需服务(一)Windows下Elasticsearch环境搭建和介绍
2、导入Elasticsearch相关依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.2</version>
</dependency>
3、配置application.yml
server:
port: 1070 #暴露的端口
spring:
application:
name: ES
es-config: #自定义配置Es参数,可以自定义 es-config 这个名称,例如下面
esname: my-application
esip: 127.0.0.1
esnodes: 9300
#es-config-two:
# esname: my-application
# esip: 127.0.0.1
# esnodes: 9300
4、配置ConfigurationFileModel
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
@Component
//获取application.yml文件中配置的叫 “es-config” 的参数值,并注入到类中
@ConfigurationProperties(prefix = "es-config")
public class ConfigurationFileModel {
//无参构造
public ConfigurationFileModel() {
super();
}
//Get-Set方法进行传值
public String getEsName() {
return esName;
}
public void setEsName(String esName) {
this.esName = esName;
}
public String getEsIp() {
return esIp;
}
public void setEsIp(String esIp) {
this.esIp = esIp;
}
public int getEsNodes() {
return esNodes;
}
public void setEsNodes(int esNodes) {
this.esNodes = esNodes;
}
//有参构造
public ConfigurationFileModel(String esName, String esIp, int esNodes) {
this.esName = esName;
this.esIp = esIp;
this.esNodes = esNodes;
}
//成员变量
private String esName;
private String esIp;
private int esNodes;
@Bean
//作者此方案为非标准配置方式,注意业务开发时将@Bean书写到总配置类中
public TransportClient client(){
TransportClient client=null;
try {
Settings settings=Settings.builder().put("cluster.name",esName).build();
client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(InetAddress.getByName(esIp), esNodes));
return client;
}catch (Exception e){
return null;
}
}
}
5、配置ESController
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Configuration
@RestController
@CrossOrigin
@RequestMapping("/es")
public class ESController {
@Autowired
ConfigurationFileModel configurationFileModel;
@GetMapping("/esRequest")
public void vivi(String one){
AnalyzeRequest request = (new AnalyzeRequest(null)).analyzer("ik_max_word").text(one);
List<AnalyzeResponse.AnalyzeToken> tokens = configurationFileModel.client().admin().indices().analyze(request).actionGet().getTokens();
for (int i=0;i<tokens.size();i++){
System.out.print(tokens.get(i).getTerm()+"-");
}
}
}
6、配置ESServerStart
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
//@EnableConfigurationProperties(ConfigurationFileModel.class) //具体的作用就不介绍了,可以看下这篇文章
/**
* 关于@EnableConfigurationProperties注解的使用
* https://blog.csdn.net/wanghuiwei888/article/details/121054828
* */
public class ESServerStart {
public static void main(String[] args) {
SpringApplication.run(ESServerStart.class);
}
}
7、启动测试
http://localhost:1070/es/esRequest?str=%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD
成功
相关文档
从零搭建Web所需服务(一)Windows下Elasticsearch环境搭建和介绍
从零搭建微服务SpringCloud(四)设计SpringCloud服务提供者