引用
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.4.0
org.elasticsearch.client
elasticsearch-rest-client
7.4.0
org.elasticsearch
elasticsearch
7.4.0
3 创建编辑配置类
application.yml
elasticsearch: host: 192.168.156.131 port: 9200
ElasticSearchConfig
package com.yy.esdemo.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
private String host;
private int port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Bean
public RestHighLevelClient client(){
return new RestHighLevelClient(RestClient.builder(
new HttpHost(
host,
port,
"http"
)
));
}
}
4 测试
EsDemoApplicationTests
package com.yy.esdemo;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EsDemoApplicationTests {
@Autowired
private RestHighLevelClient client;
@Test
void contextLoads() {
//创建es客户端对象
// RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
// new HttpHost(
// "192.168.156.131",
// 9200,
// "http"
// )
// ));
System.out.println(client);
}
}
运行结果截图如下,证明整合成功
项目结构:



