RestHighLevelClient
是 Elasticsearch
官方提供的Java高級客戶端,用于與Elasticsearch
集群進行交互和執(zhí)行各種操作。
主要特點和功能如下:
強類型:RestHighLevelClient 提供了強類型的 API,可以在編碼過程中獲得更好的類型安全性和 IDE 支持。
兼容性:RestHighLevelClient 是 Elasticsearch 官方推薦的 Java 客戶端,在 Elasticsearch 版本升級時會保證與 Elasticsearch 的兼容性。
高級功能:RestHighLevelClient 支持 Elasticsearch 的所有高級功能,例如索引、搜索、聚合、分頁、批量操作、文檔更新、刪除等操作。
異步執(zhí)行:RestHighLevelClient 還支持異步方式執(zhí)行操作,可以通過提供的異步回調方法處理返回結果。
索引管理:RestHighLevelClient 提供了索引管理相關的 API,包括創(chuàng)建索引、映射定義、設置索引的分片和副本等操作。
搜索與聚合:RestHighLevelClient 支持復雜的搜索和聚合操作,可以使用查詢條件、過濾條件、排序、范圍查詢等來獲取所需的數(shù)據(jù)。
安全認證:RestHighLevelClient 支持配置安全認證信息,例如提供用戶名和密碼進行身份驗證。
執(zhí)行配置:RestHighLevelClient 可以配置連接超時、請求超時、重試策略等執(zhí)行參數(shù),以滿足特定的需求和優(yōu)化性能。
RestHighLevelClient是一個功能強大的Java客戶端,可以輕松地與Elasticsearch集群進行交互,并支持許多高級功能和配置選項。
來實操吧…
0. 引入依賴
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
1. 實例創(chuàng)建與關閉
private RestHighLevelClient client;
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://IP:9200")
));
}
void tearDown() throws IOException {
this.client.close();
}
2. 創(chuàng)建索引
@RequestMapping("/create")
public String createHotelIndex() throws IOException {
setUp();
// 1.創(chuàng)建Request對象 "name" 是需要創(chuàng)建的索引名 一般在項目中只創(chuàng)建一次 所以這里是寫死的
CreateIndexRequest request = new CreateIndexRequest("name");
// 2.準備請求的參數(shù):DSL語句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.發(fā)送請求
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
tearDown();
return createIndexResponse.isAcknowledged() ? "創(chuàng)建成功" : "創(chuàng)建失敗";
}
3. 測試索引庫存在不存在
@RequestMapping("/testExistsHotelIndex/{indexName}")
public String testExistsHotelIndex(@PathVariable("indexName") String indexName) throws IOException {
setUp();
// 1.創(chuàng)建Request對象
GetIndexRequest request = new GetIndexRequest(indexName);
// 2.發(fā)送請求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
tearDown();
return exists ? "索引庫已經(jīng)存在!" : "索引庫不存在!";
}
4. 刪除索引庫
@RequestMapping("/testDeleteHotelIndex")
void testDeleteHotelIndex() throws IOException {
setUp();
// 1.創(chuàng)建Request對象
DeleteIndexRequest request = new DeleteIndexRequest("索引名稱");
// 2.發(fā)送請求
client.indices().delete(request, RequestOptions.DEFAULT);
tearDown();
}
5. 遍歷導入數(shù)據(jù)
不建議遍歷導入,這樣效率低低文章來源:http://www.zghlxwxcb.cn/news/detail-507030.html
@RequestMapping("/addAll")
public List<HotelDoc> addAll() throws IOException {
List<Hotel> list = hotelService.list();
List<HotelDoc> docList = list.stream().map(p -> new HotelDoc(p)).collect(Collectors.toList());
setUp();
for (HotelDoc hotelDoc : docList) {
String jsonString = JSON.toJSONString(hotelDoc);
// 1.準備Request對象
IndexRequest request = new IndexRequest("索引名稱").id(hotelDoc.getId().toString());
// 2.準備Json文檔
request.source(jsonString, XContentType.JSON);
// 3.發(fā)送請求
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(hotelDoc.getName() + ":" + index.status());
}
tearDown();
return null;
}
6. 批量導入數(shù)據(jù)(推薦)
@RequestMapping("/testBulkRequest")
public void testBulkRequest() throws IOException {
List<Hotel> hotels = hotelService.list();
setUp();
// 1.創(chuàng)建Request
BulkRequest request = new BulkRequest();
// 2.準備參數(shù),添加多個新增的Request
for (Hotel hotel : hotels) {
// 2.1.轉換為文檔類型HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
// 2.2.創(chuàng)建新增文檔的Request對象
request.add(new IndexRequest("索引名稱")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
// 3.發(fā)送請求
client.bulk(request, RequestOptions.DEFAULT);
tearDown();
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-507030.html
到了這里,關于SpringBoot 實現(xiàn) elasticsearch 索引操作(RestHighLevelClient 的應用)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!