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) {
     }
   }
 }
相关推荐
行业探路者6 小时前
如何利用二维码提升产品画册的制作与传播?
大数据·人工智能·安全·二维码·设备巡检
安达发公司6 小时前
安达发|给“工业心脏”装上新大脑:APS生产排产的硬核智慧
大数据·人工智能·aps高级排程·aps排程软件·生产计划排单软件·aps生产排产
悦数图数据库6 小时前
“复旦大学—杭州悦数先进金融图技术校企联合研究中心年度总结会”圆满举行
大数据·数据库·人工智能
yunni86 小时前
知识库 × AI写作:打通公文写作的“最后一公里”
大数据·人工智能
TDengine (老段)6 小时前
TDengine Rust 连接器入门指南
大数据·数据库·物联网·rust·时序数据库·tdengine·涛思数据
极新6 小时前
新看点/818AI创始人冷煜:AI落地,决胜“最后100米” | 2025极新AIGC峰会演讲实录
大数据·人工智能
hg01186 小时前
首届中埃咖啡经贸促进大会在株洲召开
大数据
五度易链-区域产业数字化管理平台6 小时前
金融级数据治理+企业级架构管控:五度易链的数据治理方案与技术路径
大数据·人工智能·金融·架构
song150265372986 小时前
车身颜色 外观性能测试设备-太阳光模拟器
大数据
齐鲁大虾6 小时前
2026高考生填报志愿是保专业还是保学校
大数据·人工智能