学习es时发现了一个大问题,学习的版本为7.8.0(尚硅谷yyds),自己使用的是7.17.8,但是最新的版本已经是8.5X了(心累,怎么升级这么快)。
因为目前用的还是jdk1.8,所以就按照7.17.8的版本 RestHighLevelClient 创建连接,但是从7.15.0版本开始ES官方已经不推荐Rest客户端了,所以查了一下官网,发现Elasticsearch Java API 客户端(全新的客户端库),可以和 RestHighLevelClient 兼容使用,
官方原文:
避免在过渡阶段的任何运营开销,其中 应用程序将同时使用 HLRC 和新的 Java API 客户端,这两个客户端 可以共享同一个低级 Rest 客户端,即网络层 管理所有连接、轮询策略、节点嗅探等。
下面的代码显示了如何使用相同的 HTTP 客户端初始化两个客户端:
// Create the low-level client
RestClient httpClient = RestClient.builder(
new HttpHost("localhost", 9200)
).build();
// Create the HLRC
RestHighLevelClient hlrc = new RestHighLevelClientBuilder(httpClient)
.setApiCompatibilityMode(true)
.build();
// Create the Java API Client with the same low level client
ElasticsearchTransport transport = new RestClientTransport(
httpClient,
new JacksonJsonpMapper()
);
ElasticsearchClient esClient = new ElasticsearchClient(transport);
// hlrc and esClient share the same httpClient
另外附上Spring Boot与Elasticsearch的对应版本: