初識elasticsearch
什么是elasticsearch
elasticsearch是一款非常強大的開源搜索引擎,可以幫助我們從海量數(shù)據(jù)中快速找到需要的內(nèi)容。
elasticsearch結(jié)合kibana、Logstash、Beats,也就是elastic stack(ELK)。被廣泛應(yīng)用在日志數(shù)據(jù)分析、實時監(jiān)控等領(lǐng)域。
elasticsearch是elastic stack的核心,負責(zé)存儲、搜索、分析數(shù)據(jù)。
elasticsearch的發(fā)展
Lucene是一個Java語言的搜索引擎類庫,是Apache公司的頂級項目,由DougCutting于1999年研發(fā)。官網(wǎng)地址: https://lucene.apache.orgl。
Lucene的優(yōu)勢:
- 易擴展
- 高性能(基于倒排索引)
Lucene的缺點:
- 只限于Java語言開發(fā)
- 學(xué)習(xí)曲線陡峭
- 不支持水平擴展
2004年Shay Banon基于Lucene開發(fā)了Compass
2010年Shay Banon重寫了Compass,取名為Elasticsearch。
官網(wǎng)地址: https://www.elastic.co/cn/目前最新的版本是:8.7.0
相比與lucene,elasticsearch具備下列優(yōu)勢:
- 支持分布式,可水平擴展
- 提供Restful接口,可被任何語言調(diào)用
為什么學(xué)習(xí)elasticsearch?
搜索引擎技術(shù)排名:
- Elasticsearch:開源的分布式搜索引擎
- Splunk:商業(yè)項目
- Solr: Apache的開源搜索引擎
什么是elasticsearch?
- 一個開源的分布式搜索引擎,可以用來實現(xiàn)搜索、日志統(tǒng)計、分析、
系統(tǒng)監(jiān)控等功能
什么是elastic stack (ELK)?
- 是以elasticsearch為核心的技術(shù)棧,包括beats、Logstash、kibana、elasticsearch
什么是Lucene?
- 是Apache的開源搜索引擎類庫,提供了搜索引擎的核心API
正向索引和倒排索引
傳統(tǒng)數(shù)據(jù)庫(如MySQL)采用正向索引,例如給下表(tb_goods)中的id創(chuàng)建索引:
elasticsearch采用倒排索引:
- 文檔( document) :每條數(shù)據(jù)就是一個文檔
- 詞條(term) :文檔按照語義分成的詞語
什么是文檔和詞條?
- 每一條數(shù)據(jù)就是一個文檔
- 對文檔中的內(nèi)容分詞,得到的詞語就是詞條
什么是正向索引?
- 基于文檔id創(chuàng)建索引。查詢詞條時必須先找到文檔,而后判斷是否包
含詞條
什么是倒排索引?
- 對文檔內(nèi)容分詞,對詞條創(chuàng)建索引,并記錄詞條所在文檔的信息。查詢時先根據(jù)詞條查詢到文檔id,而后獲取到文檔
文檔
elasticsearch是面向文檔存儲的,可以是數(shù)據(jù)庫中的一條商品數(shù)據(jù),一個訂單信息。
文檔數(shù)據(jù)會被序列化為json格式后存儲在elasticsearch中。
索引(lndex)
- 索引(index) :相同類型的文檔的集合
- 映射(mapping) :索引中文檔的字段約束信息,類似表的結(jié)構(gòu)約束
概念對比
架構(gòu)
Mysql:擅長事務(wù)類型操作,可以確保數(shù)據(jù)的安全和一致性
Elasticsearch:擅長海量數(shù)據(jù)的搜索、分析、計算
- 文檔:一條數(shù)據(jù)就是一個文檔,es中是Json格式
- 字段:Json文檔中的字段
- 索引:同類型文檔的集合
- 映射:索引中文檔的約束,比如字段名稱、類型
elasticsearch與數(shù)據(jù)庫的關(guān)系:
- 數(shù)據(jù)庫負責(zé)事務(wù)類型操作
- elasticsearch負責(zé)海量數(shù)據(jù)的搜索、分析、計算
安裝elasticsearch、kibana
1.部署單點es
1.1.創(chuàng)建網(wǎng)絡(luò)
因為我們還需要部署kibana容器,因此需要讓es和kibana容器互聯(lián)。這里先創(chuàng)建一個網(wǎng)絡(luò):
docker network create es-net
1.2.加載鏡像
這里我們采用elasticsearch的7.12.1版本的鏡像,這個鏡像體積非常大,接近1G。不建議大家自己pull。將鏡像上傳到虛擬機中,然后運行命令加載即可:
# 導(dǎo)入數(shù)據(jù)
docker load -i es.tar
同理還有kibana
的tar包也需要這樣做。
1.3.運行
運行docker命令,部署單點es:
docker run -d \
--name es \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-data:/usr/share/elasticsearch/data \
-v es-plugins:/usr/share/elasticsearch/plugins \
--privileged \
--network es-net \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.12.1
命令解釋:
-
-e "cluster.name=es-docker-cluster"
:設(shè)置集群名稱 -
-e "http.host=0.0.0.0"
:監(jiān)聽的地址,可以外網(wǎng)訪問 -
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m"
:內(nèi)存大小 -
-e "discovery.type=single-node"
:非集群模式 -
-v es-data:/usr/share/elasticsearch/data
:掛載邏輯卷,綁定es的數(shù)據(jù)目錄 -
-v es-logs:/usr/share/elasticsearch/logs
:掛載邏輯卷,綁定es的日志目錄 -
-v es-plugins:/usr/share/elasticsearch/plugins
:掛載邏輯卷,綁定es的插件目錄 -
--privileged
:授予邏輯卷訪問權(quán) -
--network es-net
:加入一個名為es-net的網(wǎng)絡(luò)中 -
-p 9200:9200
:端口映射配置
在瀏覽器中輸入:http://ip地址:9200 即可看到elasticsearch的響應(yīng)結(jié)果:
2.部署kibana
kibana可以給我們提供一個elasticsearch的可視化界面,便于我們學(xué)習(xí)。
2.1.部署
運行docker命令,部署kibana
docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=es-net \
-p 5601:5601 \
kibana:7.12.1
-
--network es-net
:加入一個名為es-net的網(wǎng)絡(luò)中,與elasticsearch在同一個網(wǎng)絡(luò)中 -
-e ELASTICSEARCH_HOSTS=http://es:9200"
:設(shè)置elasticsearch的地址,因為kibana已經(jīng)與elasticsearch在一個網(wǎng)絡(luò),因此可以用容器名直接訪問elasticsearch -
-p 5601:5601
:端口映射配置
kibana啟動一般比較慢,需要多等待一會,可以通過命令:
docker logs -f kibana
查看運行日志,當(dāng)查看到下面的日志,說明成功:
此時,在瀏覽器輸入地址訪問:http://ip地址:5601,即可看到結(jié)果
2.2.DevTools
kibana中提供了一個DevTools界面:
這個界面中可以編寫DSL來操作elasticsearch。并且對DSL語句有自動補全功能。
分詞器
es在創(chuàng)建倒排索引時需要對文檔分詞;在搜索時,需要對用戶輸入內(nèi)容分詞。但默認的分詞規(guī)則對中文處理并不友好。我們在kibana的DevTools中測試:
# 測試分詞器
POST /_analyze
{
"text": "霽華學(xué)Java",
"analyzer": "standard"
}
語法說明:
- POST:請求方式
- /_analyze:請求路徑,這里省略了http://192.168.150.101:9200,有kibana幫我們補充
- 請求參數(shù),json風(fēng)格:
- analyzer:分詞器類型,這里是默認的standard分詞器
- text:要分詞的內(nèi)容
處理中文分詞,一般會使用IK分詞器。https://github.com/medcl/elasticsearch-analysis-ik
安裝IK分詞器
1.在線安裝ik插件(較慢)
# 進入容器內(nèi)部
docker exec -it elasticsearch /bin/bash
# 在線下載并安裝
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
#退出
exit
#重啟容器
docker restart elasticsearch
2.離線安裝ik插件(推薦)
1)查看數(shù)據(jù)卷目錄
安裝插件需要知道elasticsearch的plugins目錄位置,而我們用了數(shù)據(jù)卷掛載,因此需要查看elasticsearch的數(shù)據(jù)卷目錄,通過下面命令查看:
docker volume inspect es-plugins
顯示結(jié)果:
[
{
"CreatedAt": "2023-04-29T04:11:49+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/es-plugins/_data",
"Name": "es-plugins",
"Options": null,
"Scope": "local"
}
]
說明plugins目錄被掛載到了:/var/lib/docker/volumes/es-plugins/_data
這個目錄中。
2)解壓縮分詞器安裝包
下面我們需要把ik分詞器解壓縮,重命名為ik
3)上傳到es容器的插件數(shù)據(jù)卷中
也就是/var/lib/docker/volumes/es-plugins/_data
:
4)重啟容器
# 4、重啟容器
docker restart es
# 查看es日志
docker logs -f es
5)測試:
IK分詞器包含兩種模式:
-
ik_smart
:最少切分 -
ik_max_word
:最細切分
ik分詞器-拓展詞庫
要拓展ik分詞器的詞庫,只需要修改一個ik分詞器目錄中的config目錄中的IKAnalyzer.cfg.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!--用戶可以在這里配置自己的擴展字典 -->
<entry key="ext_dict">ext.dic</entry>
</properties>
然后在名為ext.dic的文件中,添加想要拓展的詞語即可:
白嫖
雞你太美
奧力給
ik分詞器-停用詞庫
要禁用某些敏感詞條,只需要修改一個ik分詞器目錄中的config目錄中的IKAnalyzer.cfg.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!--用戶可以在這里配置自己的擴展字典 -->
<entry key="ext_dict">ext.dic</entry>
<!--用戶可以在這里配置自己的擴展停止詞字典-->
<entry key="ext_stopwords">stopword.</entry>
</properties>
然后在名為stopword.dic的文件中,添加想要拓展的詞語即可:
的
了
哦
嗯
啊
索引庫操作
mapping屬性
mapping是對索引庫中文檔的約束,常見的mapping屬性包括:
- type:字段數(shù)據(jù)類型,常見的簡單類型有:
- 字符串: text (可分詞的文本)、keyword(精確值,例如:品牌、國家、ip地址)
- 數(shù)值:long、integer、short、byte、double、float
- 布爾: boolean
- 日期:date
- 對象:object
- index:是否創(chuàng)建索引,默認為true
- analyzer:使用哪種分詞器
- properties:該字段的子字段
創(chuàng)建索引庫
ES中通過Restful請求操作索引庫、文檔。請求內(nèi)容用DSL語句來表示。創(chuàng)建索引庫和mapping的DSL語法如下:
{
"mappings": {
"properties": {
"字段名": {
"type": "text",
"analyzer ": "ik_smart"
},
"字段名2": {
"type": "keyword",
"index": "false"
},
"字段名3": {
"properties": {
"子字段": {
"type ": "keyword"
}
}
},
// ...略
}
}
}
例如:
# 創(chuàng)建索引庫
PUT /jihua
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
}
}
}
}
查看、刪除索引庫
- 查看索引庫語法:
GET /索引庫名
- 刪除索引庫的語法:
DELETE /索引庫名
修改索引庫
索引庫和mapping一旦創(chuàng)建無法修改,但是可以添加新的字段,語法如下:
PUT/索引庫名/ _mapping
{
"properties" : {
"新字段名":{
"type" : "integer"
}
}
}
例如:
# 修改索引庫
PUT /jihua/_mapping
{
"properties": {
"age": {
"type": "integer"
}
}
}
文檔操作
新增文檔
新增文檔的DSL語法如下:
POST /索引庫名/_doc/文檔id
{
"字段1":"值1",
"字段2":"值2",
"字段3": {
"子屬性1":"值3",
"子屬性2":"值4"
},
//...
}
例如:
# 插入文檔
POST /jihua/_doc/1
{
"info": "這里是介紹信息",
"email": "",
"name": {
"firstName": "ji",
"lastName": "hua"
},
"age": 21
}
查詢文檔
查看文檔語法:
GET /索引庫名/_doc/文檔id
刪除文檔
DELETE /索引庫名/_doc/文檔id
修改文檔
方式一:全量修改,會刪除舊文檔,添加新文檔
# 修改文檔
PUT /jihua/_doc/1
{
"字段1":"值1",
"字段2":"值2",
// ...略
}
方式二:增量修改,修改指定字段值
# 修改文檔
POST /_update/_doc/1
{
"doc":{
"字段名":"新的值"
}
}
RestClient操作索引庫
什么是RestClient
ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質(zhì)就是組裝DSL語句,通過http請求發(fā)送給ES。
官方文檔地址: https://www.elastic.co/guide/en/elasticsearch/client/index.html
案例
利用JavaRestClient實現(xiàn)創(chuàng)建、刪除索引庫,判斷索引庫是否存在
根據(jù)課前資料提供的酒店數(shù)據(jù)創(chuàng)建索引庫,索引庫名為hotel,mapping屬性根據(jù)數(shù)據(jù)庫結(jié)構(gòu)定義。
基本步驟如下:
-
導(dǎo)入課前資料Demo
-
分析數(shù)據(jù)結(jié)構(gòu),定義mapping屬性
mapping要考慮的問題:
字段名、數(shù)據(jù)類型、是否參與搜索、是否分詞、如果分詞,分詞器是什么?ES中支持兩種地理坐標數(shù)據(jù)類型:
- geo_point:由緯度(latitude)和經(jīng)度( longitude)確定的一個點。例如:“32.8752345,120.2981576”
- geo_shape:有多個geo_point組成的復(fù)雜幾何圖形。例如一條直線,
“LINESTRING (-77.03653 38.897676,-77.009051 38.889939)”
字段拷貝可以使用copy_to屬性將當(dāng)前字段拷貝到指定字段。示例:
"all": { "type" : "text", "analyzer" : "ik_max_word" }, "brand": { "type " : "keyword", "copy_to": "all" }
最終的mapping:
# 酒店的mapping 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" }, "starName": { "type": "keyword" }, "business": { "type": "keyword", "copy_to": "all" }, "location": { "type": "geo_point" }, "pic": { "type": "keyword", "index": false }, "all": { "type": "text", "analyzer": "ik_max_word" } } } }
-
初始化JavaRestClient
-
引入es的RestHighLevelclient依賴:
<!--elasticsearch--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.1</version> </dependency>
-
因為SpringBoot默認的ES版本是7.6.2,所以我們需要覆蓋默認的ES版本:
<properties> <java.version>1.8</java.version> <elasticsearch.version>7.12.1</elasticsearch.version> </properties>
-
初始化RestHighLevelClient:
package cn.itcast.hotel; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; public class HotelIndexTest { private RestHighLevelClient client; @Test void testInit() { System.out.println(client); } @BeforeEach void setUp() { this.client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://192.168.5.131:9200") )); } @AfterEach void tearDown() throws IOException { this.client.close(); } }
-
-
利用JavaRestClient創(chuàng)建索引庫
package cn.itcast.hotel.constants;
public class HotelConstants {
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" +
" },\n" +
" \"starName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\": {\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\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" +
"}";
}
@Test
void createHotelIndex() throws IOException {
//1.創(chuàng)建Request對象
CreateIndexRequest request = new CreateIndexRequest("hotel");
//2.準備請求的參數(shù):DSL語句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//3.發(fā)送請求
client.indices().create(request, RequestOptions.DEFAULT);
}
-
利用JavaRestClient刪除索引庫
@Test void testDeleteHotelIndex() throws IOException { //1.創(chuàng)建Request對象 DeleteIndexRequest request = new DeleteIndexRequest("hotel"); //2.發(fā)送請求 client.indices().delete(request, RequestOptions.DEFAULT); }
-
利用JavaRestClient判斷索引庫是否存在
@Test void testExistsHotelIndex() throws IOException { //1.創(chuàng)建Request對象 GetIndexRequest request = new GetIndexRequest("hotel"); //2.發(fā)送請求 boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); //3.輸出 System.err.println(exists?"索引庫已經(jīng)存在!" : "索引庫不存在!"); }
RestClient操作文檔
案例1
利用JavaRestClient實現(xiàn)文檔的CRUD
去數(shù)據(jù)庫查詢酒店數(shù)據(jù),導(dǎo)入到hotel索引庫,實現(xiàn)酒店數(shù)據(jù)的CRUD?;静襟E如下:
-
初始化JavaRestClient
新建一個測試類,實現(xiàn)文檔相關(guān)操作,并且完成JavaRestClient的初始化
public class ElasticsearchDocumentTest { //客戶端 private RestHighLevelClient client; @BeforeEach void setUp() { this.client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://192.168.5.131:9200") )); } @AfterEach void tearDown() throws IOException { this.client.close(); } }
-
利用JavaRestClient新增酒店數(shù)據(jù)
先查詢酒店數(shù)據(jù),然后給這條數(shù)據(jù)創(chuàng)建倒排索引,即可完成添加:
@Test void testAddDocument() throws IOException { //根據(jù)id查詢酒店數(shù)據(jù) Hotel hotel = hotelService.getById(61083L); //轉(zhuǎn)換為文檔類型 HotelDoc hotelDoc = new HotelDoc(hotel); //1.準備Request對象 IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString()); //2.準備Json文檔 request.source(JSON.toJSONString(hotelDoc), XContentType.JSON); //3.發(fā)送請求 client.index(request, RequestOptions.DEFAULT); }
-
利用JavaRestClient根據(jù)id查詢酒店數(shù)據(jù)
根據(jù)id查詢到的文檔數(shù)據(jù)是json,需要反序列化為java對象:
@Test void testGetDocumentById() throws IOException { //1.創(chuàng)建request對象 GetRequest request = new GetRequest("hotel", "61083"); //2.發(fā)送請求,得到結(jié)果 GetResponse response = client.get(request, RequestOptions.DEFAULT); //3.解析結(jié)果 String json = response.getSourceAsString(); HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); System.out.println(hotelDoc); }
-
利用JavaRestClient刪除酒店數(shù)據(jù)
修改文檔數(shù)據(jù)有兩種方式:
方式一:全量更新。再次寫入id一樣的文檔,就會刪除舊文檔,添加新文檔(同新增)
方式二:局部更新。只更新部分字段:
@Test void testUpdateDocument() throws IOException { // 1.準備Request UpdateRequest request = new UpdateRequest("hotel", "61083"); // 2.準備請求參數(shù) request.doc( "price", "952", "starName", "四鉆" ); // 3.發(fā)送請求 client.update(request, RequestOptions.DEFAULT); }
-
利用lavaRestClient修改酒店數(shù)據(jù)
@Test void testDeleteDocument() throws IOException { // 1.準備Request DeleteRequest request = new DeleteRequest("hotel", "61083"); // 2.發(fā)送請求 client.delete(request, RequestOptions.DEFAULT); }
案例2
利用JavaRestClient批量導(dǎo)入酒店數(shù)據(jù)到ES
需求:批量查詢酒店數(shù)據(jù),然后批量導(dǎo)入索引庫中
思路:文章來源:http://www.zghlxwxcb.cn/news/detail-436142.html
- 利用mybatis-plus查詢酒店數(shù)據(jù)
- 將查詢到的酒店數(shù)據(jù)(Hotel)轉(zhuǎn)換為文檔類型數(shù)據(jù)(HotelDoc)
- 利用JavaRestClient中的Bulk批處理,實現(xiàn)批量新增文檔
@Test
void testBulkRequest() throws IOException {
//批量查詢數(shù)據(jù)庫
List<Hotel> hotels = hotelService.list();
// 1.創(chuàng)建Request
BulkRequest request = new BulkRequest();
// 2.準備參數(shù),添加多個新增的Request
for (Hotel hotel : hotels) {
//轉(zhuǎn)換為文檔類型HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
//創(chuàng)建新增文檔的Request對象
request.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
// 3.發(fā)送請求
client.bulk(request, RequestOptions.DEFAULT);
}
t
DeleteRequest request = new DeleteRequest(“hotel”, “61083”);
// 2.發(fā)送請求
client.delete(request, RequestOptions.DEFAULT);
}文章來源地址http://www.zghlxwxcb.cn/news/detail-436142.html
## 案例2
利用JavaRestClient**批量導(dǎo)入**酒店數(shù)據(jù)到ES
需求:批量查詢酒店數(shù)據(jù),然后批量導(dǎo)入索引庫中
思路:
1. 利用mybatis-plus查詢酒店數(shù)據(jù)
2. 將查詢到的酒店數(shù)據(jù)(Hotel)轉(zhuǎn)換為文檔類型數(shù)據(jù)(HotelDoc)
3. 利用JavaRestClient中的Bulk批處理,實現(xiàn)批量新增文檔
```java
@Test
void testBulkRequest() throws IOException {
//批量查詢數(shù)據(jù)庫
List<Hotel> hotels = hotelService.list();
// 1.創(chuàng)建Request
BulkRequest request = new BulkRequest();
// 2.準備參數(shù),添加多個新增的Request
for (Hotel hotel : hotels) {
//轉(zhuǎn)換為文檔類型HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
//創(chuàng)建新增文檔的Request對象
request.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
// 3.發(fā)送請求
client.bulk(request, RequestOptions.DEFAULT);
}
到了這里,關(guān)于微服務(wù)學(xué)習(xí)——分布式搜索的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!