SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例
RestHighLevelClient 是 Elasticsearch 官方提供的Java高級(jí)客戶端,用于與Elasticsearch集群進(jìn)行交互和執(zhí)行各種操作。
主要特點(diǎn)和功能如下:
強(qiáng)類型:RestHighLevelClient 提供了強(qiáng)類型的 API,可以在編碼過(guò)程中獲得更好的類型安全性和 IDE 支持。
兼容性:RestHighLevelClient 是 Elasticsearch 官方推薦的 Java 客戶端,在 Elasticsearch 版本升級(jí)時(shí)會(huì)保證與 Elasticsearch 的兼容性。
高級(jí)功能:RestHighLevelClient 支持 Elasticsearch 的所有高級(jí)功能,例如索引、搜索、聚合、分頁(yè)、批量操作、文檔更新、刪除等操作。
異步執(zhí)行:RestHighLevelClient 還支持異步方式執(zhí)行操作,可以通過(guò)提供的異步回調(diào)方法處理返回結(jié)果。
索引管理:RestHighLevelClient 提供了索引管理相關(guān)的 API,包括創(chuàng)建索引、映射定義、設(shè)置索引的分片和副本等操作。
搜索與聚合:RestHighLevelClient 支持復(fù)雜的搜索和聚合操作,可以使用查詢條件、過(guò)濾條件、排序、范圍查詢等來(lái)獲取所需的數(shù)據(jù)。
安全認(rèn)證:RestHighLevelClient 支持配置安全認(rèn)證信息,例如提供用戶名和密碼進(jìn)行身份驗(yàn)證。
執(zhí)行配置:RestHighLevelClient 可以配置連接超時(shí)、請(qǐng)求超時(shí)、重試策略等執(zhí)行參數(shù),以滿足特定的需求和優(yōu)化性能。
RestHighLevelClient是一個(gè)功能強(qiáng)大的Java客戶端,可以輕松地與Elasticsearch集群進(jìn)行交互,并支持許多高級(jí)功能和配置選項(xiàng)。
來(lái)實(shí)操吧…
0. 引入依賴
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.1</version> </dependency>
1. 實(shí)例創(chuàng)建與關(guān)閉
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對(duì)象 "name" 是需要?jiǎng)?chuàng)建的索引名 一般在項(xiàng)目中只創(chuàng)建一次 所以這里是寫(xiě)死的
CreateIndexRequest request = new CreateIndexRequest("name");
// 2.準(zhǔn)備請(qǐng)求的參數(shù):DSL語(yǔ)句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.發(fā)送請(qǐng)求
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
tearDown();
return createIndexResponse.isAcknowledged() ? "創(chuàng)建成功" : "創(chuàng)建失敗";
}3. 測(cè)試索引庫(kù)存在不存在
@RequestMapping("/testExistsHotelIndex/{indexName}")
public String testExistsHotelIndex(@PathVariable("indexName") String indexName) throws IOException {
setUp();
// 1.創(chuàng)建Request對(duì)象
GetIndexRequest request = new GetIndexRequest(indexName);
// 2.發(fā)送請(qǐng)求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
tearDown();
return exists ? "索引庫(kù)已經(jīng)存在!" : "索引庫(kù)不存在!";
}4. 刪除索引庫(kù)
@RequestMapping("/testDeleteHotelIndex")
void testDeleteHotelIndex() throws IOException {
setUp();
// 1.創(chuàng)建Request對(duì)象
DeleteIndexRequest request = new DeleteIndexRequest("索引名稱");
// 2.發(fā)送請(qǐng)求
client.indices().delete(request, RequestOptions.DEFAULT);
tearDown();
}5. 遍歷導(dǎo)入數(shù)據(jù)
不建議遍歷導(dǎo)入,這樣效率低低
@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.準(zhǔn)備Request對(duì)象
IndexRequest request = new IndexRequest("索引名稱").id(hotelDoc.getId().toString());
// 2.準(zhǔn)備Json文檔
request.source(jsonString, XContentType.JSON);
// 3.發(fā)送請(qǐng)求
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(hotelDoc.getName() + ":" + index.status());
}
tearDown();
return null;
}6. 批量導(dǎo)入數(shù)據(jù)(推薦)
@RequestMapping("/testBulkRequest")
public void testBulkRequest() throws IOException {
List<Hotel> hotels = hotelService.list();
setUp();
// 1.創(chuàng)建Request
BulkRequest request = new BulkRequest();
// 2.準(zhǔn)備參數(shù),添加多個(gè)新增的Request
for (Hotel hotel : hotels) {
// 2.1.轉(zhuǎn)換為文檔類型HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
// 2.2.創(chuàng)建新增文檔的Request對(duì)象
request.add(new IndexRequest("索引名稱")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
// 3.發(fā)送請(qǐng)求
client.bulk(request, RequestOptions.DEFAULT);
tearDown();
}到此這篇關(guān)于SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例的文章就介紹到這了,更多相關(guān)SpringBoot elasticsearch索引操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot實(shí)戰(zhàn)之靜態(tài)資源處理
這篇文章主要介紹了Spring Boot實(shí)戰(zhàn)之靜態(tài)資源處理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java通過(guò)Lambda表達(dá)式實(shí)現(xiàn)簡(jiǎn)化代碼
我們?cè)诰帉?xiě)代碼時(shí),常常會(huì)遇到代碼又長(zhǎng)又重復(fù)的情況,就像調(diào)用第3方服務(wù)時(shí),每個(gè)方法都差不多,?寫(xiě)起來(lái)啰嗦,?改起來(lái)麻煩,?還容易改漏,所以本文就來(lái)用Lambda表達(dá)式簡(jiǎn)化一下代碼,希望對(duì)大家有所幫助2023-05-05
AutoScan Spring Boot 項(xiàng)目底座跨包掃描的終極解決方案全解析
文章介紹了autoscan-spring-boot-starter,這是一種為解決企業(yè)級(jí)SpringBoot開(kāi)發(fā)中跨包掃描問(wèn)題而設(shè)計(jì)的解決方案,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì)感興趣的朋友跟隨小編一起看看吧2026-03-03
Spring Cloud Gateway 如何修改HTTP響應(yīng)信息
這篇文章主要介紹了Spring Cloud Gateway 修改HTTP響應(yīng)信息的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

