1. 介紹
ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質就是組裝DSL語句,通過http請求發送給ES。
Elasticsearch目前最新版本是8.0,其java客戶端有很大變化。不過大多數企業使用的還是8以下版本
2. 客戶端初始化
在elasticsearch提供的API中,與elasticsearch一切交互都封裝在一個名為RestHighLevelClient
的類中,必須先完成這個對象的初始化,建立與elasticsearch的連接。
下面就使用黑馬商城作為示例進行一個改造
2.1 步驟
2.1.1 導入依賴
在item-service商品模塊導入該依賴
代碼如下:
<!--ElasticSearch--><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>
2.1.2? 覆蓋SpringBoot默認的ES版本
在父工程hmall進行修改,由于SpringBoot默認的ES版本是7.17.10
,所以我們需要覆蓋默認的ES版本
在properties標簽中添加elasticsearch-version的7.12.1版本
2.1.3??初始化RestHighLevelClient
寫一個單元測試進行連接
寫三個方法,分別是初始化、結束、測試連接方法
快捷鍵生成:按下Alt+Insert生成以上三個方法
?啟動測試連接方法
3. 索引庫操作
在之前寫的單元測試中進行索引庫操作
?代碼如下:
package com.hmall.item.es;import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import java.io.IOException;/*
RestHighLevelClient 客戶端初始化 索引庫操作 增刪查測試
*/
public class ElasticIndexTest {private RestHighLevelClient client;// 測試連接@Testvoid testConnection() {System.out.println("client=" + client);}// 初始化方法@BeforeEachvoid setUp() {client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.115.15:9200")));}//結束方法@AfterEachvoid tearDown() throws IOException {if (client != null) {client.close();}}//創建索引庫@Testvoid testCreateIndex() throws IOException {//1. 準備Request對象CreateIndexRequest request = new CreateIndexRequest("items");//2. 準備請求參數request.source(MAPPING_TEMPLATE, XContentType.JSON);//3. 發送請求client.indices().create(request, RequestOptions.DEFAULT);}// 查詢索引庫是否存在@Testvoid testGetIndex() throws IOException {//1. 準備Request對象GetIndexRequest request = new GetIndexRequest("items");//2. 發送請求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println("exists======================" + exists);}//刪除索引庫@Testvoid testDeleteIndex() throws IOException {//1. 準備Request對象DeleteIndexRequest request = new DeleteIndexRequest("items");//2. 發送請求AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println("delete------------------------->" + delete.isAcknowledged());}// 索引庫映射private static final String MAPPING_TEMPLATE = "{\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"name\": {\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_smart\"\n" +" },\n" +" \"price\": {\n" +" \"type\": \"integer\"\n" +" },\n" +" \"image\": {\n" +" \"type\": \"keyword\",\n" +" \"index\": false\n" +" },\n" +" \"category\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"brand\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"sold\": {\n" +" \"type\": \"integer\"\n" +" },\n" +" \"commentCount\": {\n" +" \"type\": \"integer\",\n" +" \"index\": false\n" +" },\n" +" \"isAD\": {\n" +" \"type\": \"boolean\"\n" +" },\n" +" \"updateTime\": {\n" +" \"type\": \"date\"\n" +" }\n" +" }\n" +" }\n" +"}";
}