Elasticsearch-Elasticsearch-Rest-Client(三)

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) {
     }
   }
 }
相关推荐
毕设源码-邱学长2 小时前
【开题答辩全过程】以 基于大数据技术的音乐推荐系统设计与实现为例,包含答辩的问题和答案
大数据
旺仔Sec2 小时前
2026年广东省职业院校技能大赛中职组“大数据应用与服务“赛项任务书(三)
大数据·hadoop
曾阿伦2 小时前
Elasticsearch 自定义分词匹配与同义词处理实战详解
大数据·elasticsearch·搜索引擎
天远云服3 小时前
天远企业司法认证API对接实战:PHP构建B2B供应链合规防火墙
大数据·开发语言·后端·node.js·php
赵谨言3 小时前
基于YOLOv5的植物目标检测研究
大数据·开发语言·经验分享·python
Hello.Reader3 小时前
Flink 应用升级与版本迁移Savepoint、状态兼容、跨版本恢复一次讲透
大数据·chrome·flink
毕设源码-朱学姐4 小时前
【开题答辩全过程】以 基于大数据技术的电商推荐系统的设为例,包含答辩的问题和答案
大数据
远方16094 小时前
115-使用freesql体验Oracle 多版本特性
大数据·数据库·sql·ai·oracle·database
上海蓝色星球4 小时前
造价机器人CER V2.0正式上线!
大数据·人工智能·智慧城市·运维开发