家庭電腦做網(wǎng)站班級(jí)優(yōu)化大師免費(fèi)下載安裝
文章目錄
- 前言
- 3 文檔操作
- 3.1 新增文檔
- 3.2 查詢文檔
- 3.3 修改文檔
- 3.3.1 全量修改
- 3.3.2 增量修改
- 3.4 刪除文檔
- 4 RestAPI
- 4.1 創(chuàng)建數(shù)據(jù)庫和表
- 4.2 創(chuàng)建項(xiàng)目
- 4.3 mapping映射分析
- 4.4 初始化客戶端
- 4.5 創(chuàng)建索引庫
- 4.6 判斷索引庫是否存在
- 4.7 刪除索引庫
- 5 RestClient操作文檔
- 5.1 準(zhǔn)備工作
- 5.2 新增文檔
- 5.3 查詢文檔
前言
ElasticSearch學(xué)習(xí)筆記(一)倒排索引、ES和Kibana安裝、索引操作
3 文檔操作
3.1 新增文檔
語法:
POST /{索引庫名}/_doc/{文檔id}
{"字段1": "值1","字段2": "值2","字段3": {"子屬性1":"值3","子屬性2":"值4"}// ...
}
3.2 查詢文檔
語法:
GET /{索引庫名}/_doc/{文檔id}
3.3 修改文檔
3.3.1 全量修改
全量修改是覆蓋原來的文檔,其本質(zhì)是先根據(jù)指定的id刪除文檔(id對(duì)應(yīng)的文檔不存在也可以),再新增一個(gè)相同id的文檔。
語法:
PUT /{索引庫名}/_doc/{文檔id}
{"字段1": "值1","字段2": "值2","字段3": {"子屬性1":"值3","子屬性2":"值4"}// ...
}
3.3.2 增量修改
增量修改是只修改指定id匹配的文檔中的部分字段。
語法:
POST /{索引庫名}/_update/{文檔id}
{"doc": {"修養(yǎng)修改的字段": "新值"}
}
3.4 刪除文檔
語法:
DELETE /{索引庫名}/_doc/{文檔id}
4 RestAPI
ES官方提供了各種不同語言的客戶端用來操作ES,這些客戶端的本質(zhì)是組裝DSL語句,通過Http請(qǐng)求發(fā)送給ES。其官方文檔地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
其中Java語言的客戶端分為兩種:
本文章學(xué)習(xí)的是high-level REST client。
4.1 創(chuàng)建數(shù)據(jù)庫和表
CREATE DATABASE hsgx;
USE hsgx;
CREATE TABLE tb_hotel (`id` BIGINT(20) NOT NULL PRIMARY KEY COMMENT '酒店id',`name` VARCHAR(255) NOT NULL COMMENT '酒店名稱',`address` VARCHAR(255) NOT NULL COMMENT '酒店地址',`price` INT(10) NOT NULL COMMENT '酒店價(jià)格',`score` INT(2) NOT NULL COMMENT '酒店評(píng)分',`brand` VARCHAR(32) NOT NULL COMMENT '酒店品牌',`city` VARCHAR(32) NOT NULL COMMENT '所在城市',`star_name` VARCHAR(16) NOT NULL COMMENT '酒店星級(jí)',`business` VARCHAR(255) NOT NULL COMMENT '商圈',`latitude` VARCHAR(32) NOT NULL COMMENT '緯度',`longitude` VARCHAR(32) NOT NULL COMMENT '經(jīng)度',`pic` VARCHAR(255) DEFAULT NULL COMMENT '酒店圖片'
);INSERT INTO tb_hotel(`id`, `name`, `address`, `price`, `score`, `brand`, `city`, `star_name`, `business`, `latitude`, `longitude`, `pic`)
VALUES (1, '白天鵝', '中山路', 888, 5, '白天鵝', '廣州', '五星', '太古匯', '123.456', '456.748', 'a.png'),
(2, '希爾頓', '南京路', 456, 4.5, '希爾頓', '上海', '四星', '外灘', '123.456', '456.748', 'b.png');
4.2 創(chuàng)建項(xiàng)目
在IDEA中創(chuàng)建一個(gè)maven項(xiàng)目,結(jié)構(gòu)如下:
4.3 mapping映射分析
mapping映射分析要考慮的信息包括:
- 字段名:參考表結(jié)構(gòu)。
- 字段數(shù)據(jù)類型:參考表結(jié)構(gòu)。
- 是否參與搜索:根據(jù)具體業(yè)務(wù)進(jìn)行判斷。
- 是否需要分詞:根據(jù)具體內(nèi)容進(jìn)行判斷,如果內(nèi)容是一個(gè)整體就無需分詞,反之則要分詞。
- 分詞器是什么:可以統(tǒng)一使用ik_max_word。
對(duì)應(yīng)到tb_hotel
表,我們可以新建如下索引:
PUT /hotel
{"mappings": {"properties": {"id": {"type": "integer"},"name":{"type": "text","analyzer": "ik_max_word","copy_to": "all"},"address":{"type": "text","analyzer": "ik_max_word","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"},"pic":{"type": "keyword","index": false},"location":{"type": "geo_point"},"all":{"type": "text","analyzer": "ik_max_word"}}}
}
其中,有兩個(gè)比較特殊的字段:
- location:地理坐標(biāo),類型是
geo_point
,表示由經(jīng)度(latitude)和緯度(longitude)確定一個(gè)點(diǎn)。 - all:一個(gè)組合字段,其目的是將多字段的值 利用
copy_to
屬性合并,提供給用戶搜索。在上面的例子中,name
、brand
、city
字段會(huì)合并到一起。
4.4 初始化客戶端
Java客戶端中,與ES一切交互都封裝在一個(gè)名為RestHighLevelClient
的類中,必須先完成這個(gè)對(duì)象的初始化,建立與ES的連接。主要步驟如下:
- 1)引入依賴,注意版本號(hào)和安裝的ES版本一致
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.12.1</version>
</dependency>
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.12.1</version>
</dependency>
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.12.1</version>
</dependency>
- 2)初始化RestHighLevelClient
private RestHighLevelClient client;@Before
void setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.153.128:9200")));
}@After
void close() throws IOException {this.client.close();
}
4.5 創(chuàng)建索引庫
private static final String DSL = "{\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"integer\"\n" +" },\n" +" \"name\":{\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\",\n" +" \"copy_to\": \"all\"\n" +" },\n" +" \"address\":{\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\",\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" +" \"pic\":{\n" +" \"type\": \"keyword\",\n" +" \"index\": false\n" +" },\n" +" \"location\":{\n" +" \"type\": \"geo_point\"\n" +" },\n" +" \"all\":{\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\"\n" +" }\n" +" }\n" +" }\n" +"}";@Test
public void testCreateHotelIndex() throws IOException {// 1.參數(shù)為索引庫名稱CreateIndexRequest createIndexRequest = new CreateIndexRequest("hotel");// 2.設(shè)置mapping映射createIndexRequest.source(DSL, XContentType.JSON);// 3.發(fā)起創(chuàng)建索引庫請(qǐng)求client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}
由以上代碼可知,創(chuàng)建索引庫的步驟主要又三步:
- 1)創(chuàng)建Request對(duì)象。創(chuàng)建索引庫的操作對(duì)應(yīng)的Request對(duì)象是CreateIndexRequest。
- 2)設(shè)置mapping映射,其實(shí)就是DSL的JSON參數(shù)部分。因?yàn)镴SON字符串很長,所以定義了一個(gè)靜態(tài)字符串常量來表示,讓代碼看起來更加優(yōu)雅。
- 3)發(fā)送創(chuàng)建索引庫請(qǐng)求,
client.indices()
方法的返回值是IndicesClient類型,封裝了所有與索引庫操作有關(guān)的方法。
執(zhí)行以上單元測(cè)試,在DevTools工具中查詢?cè)撍饕龓?#xff1a;
4.6 判斷索引庫是否存在
判斷索引庫是否存在,本質(zhì)是使用GET
命令查詢索引庫,因此它對(duì)應(yīng)的Request對(duì)象是GetIndexRequest。
@Test
public void testExistsHotelIndex() throws IOException {// 1.參數(shù)為索引庫名稱GetIndexRequest request = new GetIndexRequest("hotel");// 2.發(fā)送請(qǐng)求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 3.輸出System.err.println(exists ? "索引庫已經(jīng)存在!" : "索引庫不存在!");
}
執(zhí)行以上單元測(cè)試,結(jié)果如下:
4.7 刪除索引庫
刪除索引庫對(duì)應(yīng)的Request對(duì)象是DeleteIndexRequest。
@Test
public void testDeleteHotelIndex() throws IOException {// 1.參數(shù)為索引庫名稱DeleteIndexRequest request = new DeleteIndexRequest("hotel");// 2.發(fā)送請(qǐng)求client.indices().delete(request, RequestOptions.DEFAULT);
}
執(zhí)行以上單元測(cè)試,在DevTools工具中查詢?cè)撍饕龓?#xff1a;
5 RestClient操作文檔
5.1 準(zhǔn)備工作
由于上文定義的索引庫hotel
的mapping映射與數(shù)據(jù)庫表結(jié)構(gòu)有一些差異,因此還需要定義一個(gè)新的實(shí)體類,與索引庫的mapping映射對(duì)應(yīng)起來:
@Data
@NoArgsConstructor
public class HotelDoc {private Long id;private String name;private String address;private Integer price;private Integer score;private String brand;private String city;private String starName;private String business;private String location;private String pic;public HotelDoc(Hotel hotel) {this.id = hotel.getId();this.name = hotel.getName();this.address = hotel.getAddress();this.price = hotel.getPrice();this.score = hotel.getScore();this.brand = hotel.getBrand();this.city = hotel.getCity();this.starName = hotel.getStarName();this.business = hotel.getBusiness();this.location = hotel.getLatitude() + ", " + hotel.getLongitude();this.pic = hotel.getPic();}
}
主要的區(qū)別在于,將latitude
、longitude
兩個(gè)字段合并為location
一個(gè)字段。
5.2 新增文檔
新增文檔的DSL語句示例如下:
POST /hotel/_doc/1
{"name": "白天鵝","score": 5
}
對(duì)應(yīng)的Java代碼如下:
@Test
public void testCreateDocIndex() throws IOException {// 1.POST /hotel/_doc/1 { "name": "白天鵝", "score": 5 }IndexRequest request = new IndexRequest("hotel").id("1");request.source("{\"name\": \"白天鵝\", \"score\": 5}", XContentType.JSON)// 2.發(fā)送請(qǐng)求client.index(request, RequestOptions.DEFAULT);
}
執(zhí)行以上單元測(cè)試,在DevTools工具中查詢?cè)撐臋n:
下面實(shí)現(xiàn)把數(shù)據(jù)庫tb_hotel
表的數(shù)據(jù)讀取出來,并保存到ES中:
@Test
public void testSaveHotel() throws IOException {// 1.根據(jù)id查詢酒店數(shù)據(jù)Hotel hotel = hotelService.getById(2);// 2.轉(zhuǎn)換為文檔類型HotelDoc hotelDoc = new HotelDoc(hotel);// 3.將HotelDoc轉(zhuǎn)jsonString json = JSON.toJSONString(hotelDoc);// 4.準(zhǔn)備Request對(duì)象IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());// 5.準(zhǔn)備Json文檔request.source(json, XContentType.JSON);// 6.發(fā)送請(qǐng)求client.index(request, RequestOptions.DEFAULT);
}
執(zhí)行以上單元測(cè)試,在DevTools工具中查詢?cè)撐臋n:
5.3 查詢文檔
新增文檔的DSL語句示例如下:
GET /hotel/_doc/2
對(duì)應(yīng)的Java代碼如下:
@Test
public void testQueryHotelDoc() throws IOException {// 1.創(chuàng)建Request對(duì)象GetRequest request = new GetRequest("hotel", "2");// 2.發(fā)送請(qǐng)求GetResponse response = client.get(request, RequestOptions.DEFAULT);// 3.解析結(jié)果String json = response.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);
}
執(zhí)行以上單元測(cè)試,結(jié)果如下:
…
本節(jié)完,更多內(nèi)容請(qǐng)查閱分類專欄:微服務(wù)學(xué)習(xí)筆記
感興趣的讀者還可以查閱我的另外幾個(gè)專欄:
- SpringBoot源碼解讀與原理分析
- MyBatis3源碼深度解析
- Redis從入門到精通
- MyBatisPlus詳解
- SpringCloud學(xué)習(xí)筆記