?引言
本文參考黑馬 分布式Elastic search
Elasticsearch是一款非常強(qiáng)大的開源搜索引擎,具備非常多強(qiáng)大功能,可以幫助我們從海量數(shù)據(jù)中快速找到需要的內(nèi)容
一、RestAPI
ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質(zhì)就是組裝DSL語句,通過http請求發(fā)送給ES。官方文檔地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
?導(dǎo)入數(shù)據(jù)
數(shù)據(jù)結(jié)構(gòu)如下
CREATE TABLE `tb_hotel` (
`id` bigint(20) NOT NULL COMMENT '酒店id',
`name` varchar(255) NOT NULL COMMENT '酒店名稱;例:7天酒店',
`address` varchar(255) NOT NULL COMMENT '酒店地址;例:航頭路',
`price` int(10) NOT NULL COMMENT '酒店價(jià)格;例:329',
`score` int(2) NOT NULL COMMENT '酒店評分;例:45,就是4.5分',
`brand` varchar(32) NOT NULL COMMENT '酒店品牌;例:如家',
`city` varchar(32) NOT NULL COMMENT '所在城市;例:上海',
`star_name` varchar(16) DEFAULT NULL COMMENT '酒店星級,從低到高分別是:1星到5星,1鉆到5鉆',
`business` varchar(255) DEFAULT NULL COMMENT '商圈;例:虹橋',
`latitude` varchar(32) NOT NULL COMMENT '緯度;例:31.2497',
`longitude` varchar(32) NOT NULL COMMENT '經(jīng)度;例:120.3925',
`pic` varchar(255) DEFAULT NULL COMMENT '酒店圖片;例:/img/1.jpg',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
數(shù)據(jù)我放到網(wǎng)盤了,大家可自行獲取
?mapping映射分析
創(chuàng)建索引庫,最關(guān)鍵的是mapping映射,而mapping映射要考慮的信息包括:
- 字段名
- 字段數(shù)據(jù)類型
- 是否參與搜索
- 是否需要分詞
- 如果分詞,分詞器是什么?
其中:
- 字段名、字段數(shù)據(jù)類型,可以參考數(shù)據(jù)表結(jié)構(gòu)的名稱和類型
- 是否參與搜索要分析業(yè)務(wù)來判斷,例如圖片地址,文件地址 就無需參與搜索
- 是否分詞呢要看內(nèi)容,內(nèi)容如果是一個(gè)整體就無需分詞,反之則要分詞
- 分詞器,我們可以統(tǒng)一使用 ik_max_word ik分詞器最大分詞
以下是酒店的索引庫結(jié)構(gòu)
PUT /hotel
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword",
"copy_to": "all"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
幾個(gè)特殊字段說明:
- location:地理坐標(biāo),里面包含精度、緯度
- all:一個(gè)組合字段,其目的是將多字段的值 利用copy_to合并,提供給用戶搜索
地理坐標(biāo) 說明:
copy_to說明:
=
?初始化RestClient
在 Elasticsearch 提供的API中,與 Elasticsearch 一切交互都封裝在一個(gè)名為RestHighLevelClient的類中,必須先完成這個(gè)對象的初始化,建立與 Elasticsearch 的連接。
大概分為3步
引入ES的RestHighLevelClient依賴
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
SpringBoot 的默認(rèn)ES版本為7.6.2,覆蓋默認(rèn)的ES版本
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
初始化RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://ip地址:9200")
));
為了測試方便,我們新建單元測試進(jìn)行初始化
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class HoteTest {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.101:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
二、索引庫操作
?創(chuàng)建索引庫
創(chuàng)建索引庫的API如下:
代碼分為三步:
- 創(chuàng)建Request對象。因?yàn)槭莿?chuàng)建索引庫的操作,因此Request是CreateIndexRequest。
- 添加請求參數(shù),其實(shí)就是DSL的JSON參數(shù)部分。因?yàn)閖son字符串很長,這里是定義了靜態(tài)字符串常量MAPPING_TEMPLATE,讓代碼看起來更加優(yōu)雅。
- 發(fā)送請求,client.indices()方法的返回值是IndicesClient類型,封裝了所有與索引庫操作有關(guān)的方法。
創(chuàng)建一個(gè)類,定義mapping映射的JSON字符串常量:
public class HotelEnum {
public 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" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"score\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"location\":{\n" +
" \"type\": \"geo_point\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
在hotel-demo中的HotelIndexTest測試類中,編寫單元測試,實(shí)現(xiàn)創(chuàng)建索引:
import org.elasticsearch.client.indices.CreateIndexRequest;
@Test
void createHotelIndex() throws IOException {
// 1.創(chuàng)建Request對象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.準(zhǔn)備請求的參數(shù):DSL語句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.發(fā)送請求
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
??刪除索引庫
刪除索引庫的DSL語句非常簡單:
DELETE /hotel
與創(chuàng)建索引庫相比:
- 請求方式從PUT變?yōu)镈ELTE
- 請求路徑不變
- 無請求參數(shù)
所以代碼的差異,注意體現(xiàn)在Request對象上。依然是三步走:
- 創(chuàng)建Request對象。這次是DeleteIndexRequest對象
- 準(zhǔn)備參數(shù)。這里是無參
- 發(fā)送請求。改用delete方法
在hotel-demo中的HotelIndexTest測試類中,編寫單元測試,實(shí)現(xiàn)刪除索引:
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@Test
void testDeleteHotelIndex() throws IOException {
// 1.創(chuàng)建Request對象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 2.發(fā)送請求
restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
}
?判斷索引庫是否存在
判斷索引庫是否存在,本質(zhì)就是查詢,對應(yīng)的DSL是:
GET /hotel
DSL查看如下
因此與刪除的Java代碼流程是類似的。依然是三步走:
- 創(chuàng)建Request對象。這次是GetIndexRequest對象
- 準(zhǔn)備參數(shù)。這里是無參
- 發(fā)送請求。改用exists方法
import org.elasticsearch.client.indices.GetIndexRequest;
@Test
void testExistsHotelIndex() throws IOException {
// 1.創(chuàng)建Request對象
GetIndexRequest request = new GetIndexRequest("hotel");
// 2.發(fā)送請求
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
// 3.輸出
System.out.println(exists ? "索引庫已經(jīng)存在!" : "索引庫不存在!");
}
小結(jié)
JavaRestClient操作elasticsearch的流程基本類似。核心是client.indices()方法來獲取索引庫的操作對象。
索引庫操作的基本步驟:
- 初始化RestHighLevelClient
- 創(chuàng)建XxxIndexRequest。XXX是Create、Get、Delete
- 準(zhǔn)備DSL( Create時(shí)需要,其它是無參)
- 發(fā)送請求。調(diào)用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete
?小結(jié)
以上就是【Bug 終結(jié)者】對 微服務(wù) 分布式搜索引擎 Elastic Search RestAPI 的簡單介紹,ES搜索引擎無疑是最優(yōu)秀的分布式搜索引擎,使用它,可大大提高項(xiàng)目的靈活、高效性! 技術(shù)改變世界?。?!
文章來源:http://www.zghlxwxcb.cn/news/detail-819661.html
如果這篇【文章】有幫助到你,希望可以給【Bug 終結(jié)者】點(diǎn)個(gè)贊??,創(chuàng)作不易,如果有對【后端技術(shù)】、【前端領(lǐng)域】感興趣的小可愛,也歡迎關(guān)注?????? 【Bug 終結(jié)者】??????,我將會給你帶來巨大的【收獲與驚喜】??????!文章來源地址http://www.zghlxwxcb.cn/news/detail-819661.html
到了這里,關(guān)于微服務(wù) 分布式搜索引擎 Elastic Search RestAPI的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!