在Elasticsearch7.15版本之后,Elasticsearch官方將它的高級客戶端RestHighLevelClient標記為棄用狀態(tài)。同時推出了全新的Java API客戶端Elasticsearch Java API Client,該客戶端也將在Elasticsearch8.0及以后版本中成為官方推薦使用的客戶端。
Elasticsearch Java API Client 支持除 Vector tile search API 和 Find structure API 之外的所有 Elasticsearch API。且支持所有API數(shù)據(jù)類型,并且不再有原始JsonValue屬性。它是針對Elasticsearch8.0及之后版本的客戶端,目前Elasticsearch已經(jīng)更新至8.0.1,所以我們需要學習新的Elasticsearch Java API Client的使用方法。
1. 環(huán)境要求
首先,你的項目需要支持Java8或以上,并且你的項目需要有一個Json對象映射庫,比如Jackson等,本文章中使用Jackson作為示例。
2. 安裝依賴
在Gradle項目中安裝:
dependencies {
implementation 'co.elastic.clients:elasticsearch-java:8.0.1'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
}
在Maven項目中安裝:
<project>
<dependencies>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</project>
3. 連接
// 創(chuàng)建低級客戶端
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
// 使用Jackson映射器創(chuàng)建傳輸層
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// 創(chuàng)建API客戶端
ElasticsearchClient client = new ElasticsearchClient(transport);
4. 測試查詢請求
注意,需要ES內(nèi)有相應的數(shù)據(jù)才可以查到。如沒有數(shù)據(jù)可供測試可以直接跳過本項。
SearchResponse<Product> search = client.search(s -> s
.index("products")
.query(q -> q
.term(t -> t
.field("name")
.value(v -> v.stringValue("testname"))
)),
Product.class);
for (Hit<Product> hit: search.hits().hits()) {
processProduct(hit.source());
}
5. 索引Index的基本操作
5.1 創(chuàng)建索引
// 創(chuàng)建連接
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);
// 創(chuàng)建索引
CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("newapi"));
// 打印結果
System.out.println(createIndexResponse.acknowledged());
// 關閉連接
transport.close();
restClient.close();
其中,newapi為你要創(chuàng)建索引的名字。
5.2 查詢索引
// Create the low-level client
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);
GetIndexResponse createIndexResponse = client.indices().get(e->e.index("newapi"));
System.out.println(String.join(",", createIndexResponse.result().keySet()));
transport.close();
restClient.close();
其中,newapi為你要查詢索引的名字。
5.3 刪除索引
// Create the low-level client
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);
DeleteIndexResponse deleteIndexResponse = client.indices().delete(e->e.index("newapi"));
System.out.println(deleteIndexResponse.acknowledged());
transport.close();
restClient.close();
其中,newapi為你要刪除索引的名字。
6. 文檔Doc的基本操作
6.1 創(chuàng)建文檔Doc
// 創(chuàng)建一個需要保存至ES的對象
Test test = new Test();
test.setName("添加測試");
test.setSex("男");
test.setAge(24);
// 構建一個創(chuàng)建Doc的請求
CreateResponse createResponse = client.create(e->e.index("newapi").id("1003").document(test));
// 打印請求結果
System.out.println(createResponse.result());
其中,index為文檔Doc所屬索引的名字,id為該文檔的id,document參數(shù)現(xiàn)在可以直接傳入Java對象了。
6.2 修改文檔Doc
// 構建需要修改的內(nèi)容,這里使用了Map
Map<String, Object> map = new HashMap<>();
map.put("age", 35);
// 構建修改文檔的請求
UpdateResponse<Test> response = client.update(e -> e.index("newapi").id("1003").doc(map), Test.class);
// 打印請求結果
System.out.println(response.result());
6.3 查詢文檔Doc
// 構建查詢請求
GetResponse<Test> response = client.get(e -> e.index("newapi").id("1003"), Test.class);
// 打印查詢結果
System.out.println(response.source().toString());
6.4 刪除文檔Doc
// 構建刪除文檔請求
DeleteResponse response = client.delete(e -> e.index("newapi").id("1001"));
// 打印請求結果
System.out.println(response.result());
更多新版客戶端Elasticsearch Java API Client的批量及復雜查詢相關操作已更新,可以查閱我的新文章:Elasticsearch8.0版本中Elasticsearch Java API Client客戶端的基本使用方法_無楓的博客,分享Java及Vue方向的技術文章-CSDN博客
如何從高級客戶端High Level Rest Client遷移至新版客戶端Elasticsearch Java API Client?
????????根據(jù)官方給出的答案是無法平滑的遷移,不過新版客戶端和舊版高級客戶端是可以共存的,并且沒有操作開銷。所以在你的項目中可以逐步的從舊版客戶端遷移至新版客戶端。
? ? ? ? 并且,官方還提供了使High Level Rest Client和Elasticsearch Java API Client使用相同的傳輸通道的方案,他可以使兩個客戶端共享相同的Low Level Rest Client,管理所有連接,循環(huán)策略的網(wǎng)絡層以及節(jié)點嗅探等工作。文章來源:http://www.zghlxwxcb.cn/news/detail-793464.html
? ? ? ? 以下代碼展示了如何使用相同的HTTP客戶端同時初始化新客戶端和舊客戶端:文章來源地址http://www.zghlxwxcb.cn/news/detail-793464.html
// 創(chuàng)建低級客戶端(新版客戶端內(nèi)容)
RestClientBuilder httpClientBuilder = RestClient.builder(
new HttpHost("localhost", 9200)
);
// 創(chuàng)建舊版高級客戶端RestHighLevelClient
RestHighLevelClient hlrc = new RestHighLevelClient(httpClientBuilder);
// 使用相同的低級客戶端創(chuàng)建新的Java客戶端
ElasticsearchTransport transport = new RestClientTransport(
hlrc.getLowLevelClient(),
new JacksonJsonpMapper()
);
ElasticsearchClient esClient = new ElasticsearchClient(transport);
// 新舊客戶端共享相同的http客戶端
到了這里,關于Elasticsearch RestHighLevelClient 已標記為被棄用 它的替代方案 Elasticsearch Java API Client 的基礎教程及遷移方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!