SpringBoot整合Elasticsearch-elasticsearch-rest-high-level-client

本文介绍了如何在SpringBoot应用中整合Elasticsearch,推荐使用9200端口并通过官方的Elasticsearch-Rest-High-Level-Client进行操作,避免版本不兼容和复杂封装。详细步骤包括:选择合适的整合方式,添加依赖,配置信息,编写测试案例,并展示了如何处理请求设置项,特别是当ES添加安全验证时的身份验证设置。最后,演示了添加数据的API调用及其遇到的问题和解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在前面几篇文章中演示ES安装时对外暴露有两个端口,一个是9200,一个是9300,这里9300是一个TCP端口,ES集群环境下也是通过9300端口通信的,是长连接的形式通信的,这里不推荐使用SpringBoot操作9300端口通信,其原因有两个,
第一个:是由于SpringBoot的版本不同,整合的ES版本也不同,而且最新的SpringBoot也没整合到7.X版本,只整合到6.X.X,那么版本不适配就会倒是语法问题
第二点:是ES 官方在7.X版本就已经不建议使用9300端口了,将在8.X废弃9300端口

所以这里SpringBoot整合ES是采用9200端口操作
这里操作9200http的方式有很多种方式
1.JestClient:不是官方提供的,更新很慢!
2.RestTemplate:模拟发Http请求,ES很多请求方式需要自己封装,比较麻烦
3.HttpClient:这个也是一样的
这里只要是能发http请求那么都是可以操作ES9200端口的
这些方式都需要自己封装,比较麻烦,不是很友好,所以这里使用ES官方提供的Elasticsearch-Rest-Client,他封装好了ES操作,API层次分明,上手简单,而且ES版本更新这个框架可更新!不用担心版本不匹配问题!!!

开始整合

1.进入官网

### Elasticsearch 3.3.2 Compatible `elasticsearch-rest-high-level-client` Version Elasticsearch versions and their corresponding client libraries must align closely to ensure compatibility. For Elasticsearch version **3.3.2**, it is important to note that the `elasticsearch-rest-high-level-client` was introduced starting from Elasticsearch version **6.x**[^4]. Therefore, there is no direct high-level REST client available specifically for Elasticsearch **3.3.2**. For older versions of Elasticsearch such as **3.3.2**, developers typically use the transport client or low-level REST clients instead. If migrating to a newer version of Elasticsearch is not an option, one can rely on the following alternatives: - Use the **Transport Client**: This was the recommended approach before the introduction of the high-level REST client. - Utilize the **low-level REST client**: It provides flexibility but requires manual handling of responses and serialization/deserialization processes[^5]. If upgrading Elasticsearch is feasible, consider moving to at least Elasticsearch **7.x**, where you would then be able to utilize the latest stable release of the `elasticsearch-rest-high-level-client`. ```java // Example usage of elasticsearch-rest-high-level-client (for Elasticsearch >= 6.x) RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http"))); ``` #### Important Considerations When working with legacy systems like Elasticsearch **3.3.2**, always verify whether any third-party plugins or custom integrations might also require updates when changing components within your stack[^6]. ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员劝退师-TAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值