1)TCP
spring-data-elasticsearch:transport-api.jar;
springboot版本不同,transport-api.jar不同,不能适配es版本, 7.x已经不建议使用,8以后就要废弃
2)、9200:HTTP
JestClient:非官方,更新慢
RestTemplate:模拟发HTTP请求,ES很多操作需要自己封装,麻烦
HttpClient:同上
Elasticsearch-Rest-Client:官方RestClient,封装了ES操作,API层次分明,上手简单
最终选择Elasticsearch-Rest-Client(elasticsearch-rest-high-level-client) https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
1、SpringBoot整合
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
2、配置
@Bean RestHighLevelClientclient(){
RestClientBuilder builder = RestClient.builder(newHttpHost("192.168.56.10", 9200, "http"));
return new RestHighLevelClient(builder);
}
3、使用 参照官方文档
@Test
void test1() throwsIOException{
Product product=new Product();
product.setSpuName("华为");
product.setId(10L);
IndexRequest request=new IndexRequest("product").id("20").source("spuName","华为","id",20L);
try{
IndexResponse response=client.index(request,RequestOptions.DEFAULT);
System.out.println(request.toString());
IndexResponse response2 = client.index(request, RequestOptions.DEFAULT);
} catch (ElasticsearchException e) {
if (e.status() == RestStatus.CONFLICT) {
}
}
}