國外 網(wǎng)站 設(shè)計(jì)發(fā)布軟文網(wǎng)站
RestAPI
- 初始化RestClient
- 創(chuàng)建索引庫
- Mapping映射
- 判斷索引庫是否存在
- 刪除索引庫
- 總結(jié)
ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質(zhì)就是組裝DSL語句,通過http請求發(fā)送給ES。
官方文檔地址
由于ES目前最新版本是8.8,提供了全新版本的客戶端,老版本的客戶端已經(jīng)被標(biāo)記為過時(shí)。而我們采用的是7.12版本,因此只能使用老版本客戶端:
然后選擇7.12版本,HighLevelRestClient版本
初始化RestClient
在elasticsearch提供的API中,與elasticsearch一切交互都封裝在一個(gè)名為RestHighLevelClient的類中,必須先完成這個(gè)對象的初始化,建立與elasticsearch的連接。
分為三步:
1)在item-service模塊中引入es的RestHighLevelClient依賴:
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
2)因?yàn)镾pringBoot默認(rèn)的ES版本是7.17.10,所以我們需要覆蓋默認(rèn)的ES版本:
<properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><elasticsearch.version>7.12.1</elasticsearch.version></properties>
3)測試代碼:
@Slf4j
@SpringBootTest
class EsClientTest {private RestHighLevelClient client;@Testvoid test() throws IOException {log.info("client = {}",client);}@BeforeEachvoid setUp() {client = new RestHighLevelClient(RestClient.builder(new HttpHost("49.233.155.186", 9200, "http")));}@AfterEachvoid tearDown() throws IOException {if (client != null) {client.close();}}
}
創(chuàng)建索引庫
由于要實(shí)現(xiàn)對商品搜索,所以我們需要將商品添加到Elasticsearch中,不過需要根據(jù)搜索業(yè)務(wù)的需求來設(shè)定索引庫結(jié)構(gòu),而不是一股腦的把MySQL數(shù)據(jù)寫入Elasticsearch.
Mapping映射
搜索頁面的效果如圖所示:
實(shí)現(xiàn)搜索功能需要的字段包括三大部分:
- 搜索過濾字段
- 分類
- 品牌
- 價(jià)格
- 排序字段
- 默認(rèn):按照更新時(shí)間降序排序
- 銷量
- 價(jià)格
- 展示字段
- 商品id:用于點(diǎn)擊后跳轉(zhuǎn)
- 圖片地址
- 是否是廣告推廣商品
- 名稱
- 價(jià)格
- 評價(jià)數(shù)量
- 銷量
對應(yīng)的商品表結(jié)構(gòu)如下,索引庫無關(guān)字段已經(jīng)劃掉:
結(jié)合數(shù)據(jù)庫表結(jié)構(gòu),以上字段對應(yīng)的mapping映射屬性如下:
字段名 | 字段類型 | 類型說明 | 是否參與搜索 | 是否參與分詞 | 分詞器 |
---|---|---|---|---|---|
id | long | 長整數(shù) | [x] | [] | |
name | text | 字符串,參與分詞搜索 | [x] | [x] | IK |
price | integer | 以分為單位,所以是整數(shù) | [x] | [] | |
stock | integer | 字符串,但需要分詞 | [x] | [] | |
image | keyword | 字符串,但是不分詞 | [] | [] | |
category | keyword | 字符串,但是不分詞 | [x] | [] | |
brand | keyword | 字符串,但是不分詞 | [x] | [] | |
sold | integer | 銷量,整數(shù) | [x] | [] | |
commentCount | integer | 評價(jià),整數(shù) | [] | [] | |
isAD | boolean | 布爾類型 | [x] | [] | |
updateTime | Date | 更新時(shí)間 | [x] | [] |
因此,最終我們的索引庫文檔結(jié)構(gòu)應(yīng)該是這樣:
PUT /items
{"mappings": {"properties": {"id": {"type": "keyword"},"name":{"type": "text","analyzer": "ik_max_word"},"price":{"type": "integer"},"stock":{"type": "integer"},"image":{"type": "keyword","index": false},"category":{"type": "keyword"},"brand":{"type": "keyword"},"sold":{"type": "integer"},"commentCount":{"type": "integer","index": false},"isAD":{"type": "boolean"},"updateTime":{"type": "date"}}}
}
創(chuàng)建索引
創(chuàng)建索引庫的API如下:
代碼分為三步:
- 創(chuàng)建Request對象。
因?yàn)槭莿?chuàng)建索引庫的操作,因此Request是CreateIndexRequest。 - 添加請求參數(shù)
其實(shí)就是Json格式的Mapping映射參數(shù)。因?yàn)閖son字符串很長,這里是定義了靜態(tài)字符串常量MAPPING_TEMPLATE,讓代碼看起來更加優(yōu)雅。 - 發(fā)送請求
client.indices()方法的返回值是IndicesClient類型,封裝了所有與索引庫操作有關(guān)的方法。例如創(chuàng)建索引、刪除索引、判斷索引是否存在等
測試類中,具體代碼如下:
@Test
void testCreateIndex() throws IOException {// 1.創(chuàng)建Request對象CreateIndexRequest request = new CreateIndexRequest("items");// 2.準(zhǔn)備請求參數(shù)request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3.發(fā)送請求client.indices().create(request, RequestOptions.DEFAULT);
}static final String MAPPING_TEMPLATE = "{\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"name\":{\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\"\n" +" },\n" +" \"price\":{\n" +" \"type\": \"integer\"\n" +" },\n" +" \"stock\":{\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" +" },\n" +" \"isAD\":{\n" +" \"type\": \"boolean\"\n" +" },\n" +" \"updateTime\":{\n" +" \"type\": \"date\"\n" +" }\n" +" }\n" +" }\n" +"}";
判斷索引庫是否存在
判斷索引庫是否存在,本質(zhì)就是查詢,對應(yīng)的請求語句是:
GET /索引庫
因此與,流程如下:
- 1)創(chuàng)建Request對象。這次是GetIndexRequest對象
- 2)準(zhǔn)備參數(shù)。這里是無參,直接省略
- 3)發(fā)送請求。改用exists方法
@Test
void testExistsIndex() throws IOException {// 1.創(chuàng)建Request對象GetIndexRequest request = new GetIndexRequest("items");// 2.發(fā)送請求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 3.輸出System.err.println(exists ? "索引庫已經(jīng)存在!" : "索引庫不存在!");
}
刪除索引庫
刪除索引庫的請求非常簡單:
DELETE /索引庫
與創(chuàng)建索引庫相比:
- 請求方式從PUT變?yōu)镈ELTE
- 請求路徑不變
- 無請求參數(shù)
所以代碼的差異,注意體現(xiàn)在Request對象上。流程如下:
- 1)創(chuàng)建Request對象。這次是DeleteIndexRequest對象
- 2)準(zhǔn)備參數(shù)。這里是無參,因此省略
- 3)發(fā)送請求。改用delete方法
編寫單元測試,實(shí)現(xiàn)刪除索引:
@Test
void testDeleteIndex() throws IOException {// 1.創(chuàng)建Request對象DeleteIndexRequest request = new DeleteIndexRequest("items");// 2.發(fā)送請求client.indices().delete(request, RequestOptions.DEFAULT);
}
總結(jié)
JavaRestClient操作elasticsearch的流程基本類似。核心是client.indices()方法來獲取索引庫的操作對象。
索引庫操作的基本步驟:
- 初始化RestHighLevelClient
- 創(chuàng)建XxxIndexRequest。XXX是Create、Get、Delete
- 準(zhǔn)備請求參數(shù)( Create時(shí)需要,其它是無參,可以省略)
- 發(fā)送請求。調(diào)用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete