在網(wǎng)上上我看已經(jīng)有好多關(guān)于Elasticsearch的介紹郭宝,我就不在翻來覆去講一些基本概念,大家感興趣的可以自己去找一些資料鞏固下。我這只為了顧及眾多首次接觸Elasticsearch,案例都講的很淺顯官辈,還有就是受個人能力所限,實(shí)在寫不出高大上的博文遍坟,各位讀者發(fā)現(xiàn)有錯誤之處拳亿,還不要取笑我,給我指出來即可愿伴。
內(nèi)容規(guī)劃總共分為三個章節(jié)來寫肺魁,分別運(yùn)行環(huán)境構(gòu)建、利用Web應(yīng)用管理索引以及Web應(yīng)用管理數(shù)據(jù)三大塊來說明隔节。
具體有:
一鹅经、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(一):這些操作都是在CentOS下操作的,主要帶大家熟悉下Elasticsearch環(huán)境官帘。
- 1.1.下載& Linux下ElasticSearch安裝
- 1.2.中文分詞插件IK
- 1.3.索引
- 1.4.如何數(shù)據(jù)管理
二瞬雹、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(二):在Springboot環(huán)境下,利用JAVA環(huán)境操作索引刽虹。
- 2.1.新增索引
- 2.2.查詢索引
- 2.3.刪除索引
三、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(三):在Springboot環(huán)境下呢诬,管理數(shù)據(jù)涌哲。
- 3.1.WEB HTTP提交數(shù)據(jù)<單條提交、批量提交>
- 3.2.WEB HTTP方式條件查詢
- 3.3.WEB HTTP刪除數(shù)據(jù)
1. Linux單機(jī)安裝
題外話尚镰,這一章節(jié)僅僅作為開發(fā)學(xué)習(xí)來構(gòu)建基礎(chǔ)的環(huán)境阀圾,并未考慮elasticsearch的高可用性,僅說明一些基礎(chǔ)知識狗唉,帶大家有一個認(rèn)識初烘。當(dāng)然既然是自我發(fā)揮,也就是一家之言分俯,難免有遺漏地方肾筐,希望大家也就本著看看。
1.1. 下載&安裝
1.1.1. 環(huán)境需求
- CentOs7
- 內(nèi)存4G+:這個因環(huán)境而異
- JDK11+:由于
elasticsearch
運(yùn)行需要JDK環(huán)境缸剪,我機(jī)器 JDK 版本是 11吗铐,如果 JDK 低于 9.0 會有一些問題,下圖我也貼出來杏节。
Java HotSpot(TM) 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
1.1.2. 下載
官方 elasticsearch 下載唬渗,下載 elasticsearch
典阵,目前最新的穩(wěn)定版本為 7.4.0 版本。
1.1.3. 安裝
- 下載
elasticsearch
镊逝,會得到一個文件elasticsearch-X.X.X-linux-x86_64.tar.gz
- 創(chuàng)建個文件夾
elastic
- 再創(chuàng)建一個組壮啊,案例中我以
dev
命名 - 再創(chuàng)建一個用戶,
elasticsearch
不允許使用root
啟動撑蒜,創(chuàng)建一個新的用戶elastic
,并為這個賬戶賦予相應(yīng)的權(quán)限來啟動elasticsearch
减江。 - 解壓文件
- 將
elasticsearch-X.X.X-linux-x86_64.tar.gz
文件移入elastic
中 - 重新調(diào)整下權(quán)限
chown -R
[root@localhost download]$ pwd
/data/download/
[root@localhost download]$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0-linux-x86_64.tar.gz
[root@localhost download]$ cd ../app/
[root@localhost app]$ mkdir elastic
[root@localhost app]$ groupadd dev
[root@localhost app]$ useradd elastic -g dev
[root@localhost app]$ passwd elastic
[root@localhost app]$ chown -R elastic:dev elastic
[root@localhost app]$ su elastic
[elastic@localhost app]$ cd /elastic
[elastic@localhost elastic]$ cp ../../download/elasticsearch-7.4.0-linux-x86_64.tar.gz .
[elastic@localhost elastic]$ tar -zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz
[elastic@localhost elastic]$ mv elasticsearch-7.4.0/ .
1.1.4. 修改配置文件
配置文件中有很多配置項(xiàng)辈灼,例如集群信息、端口等司志。
elasticsearch
本身為安全考慮降宅,默認(rèn)不允許外部訪問骂远,我們這里做演示,就需要將這個配置項(xiàng)修改掉腰根,路徑在 config/elasticsearch.yml
[elastic@localhost elastic]$ vi config/elasticsearch.yml
修改后的效果如下:
-- 激活節(jié)點(diǎn)1
node.name: node-1
-- 允許外部IP訪問
network.host: 0.0.0.0
-- 把這個注釋先放開并修改
cluster.initial_master_nodes: ["node-1"]
1.1.5. 啟動&驗(yàn)證結(jié)果
- 啟動
[elastic@localhost elastic]$ ./bin/elasticsearch
- 驗(yàn)證結(jié)果
elasticsearch
默認(rèn)端口是 9200 激才,打開地址:http://192.168.147.128:9200/
注意,啟動后可能會有兩種錯誤導(dǎo)致啟動失敗额嘿。
從中可以看出主要是打開時數(shù)量不夠以及虛擬內(nèi)存不足瘸恼。
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解決措施
- vi /etc/security/limits.conf
elastic hard nofile 65536
elastic soft nofile 65536
其中 elastic
為啟動 elasticsearch
的用戶名。
- vi /etc/sysctl.conf
vm.max_map_count=655360
再執(zhí)行 sysctl -p
1.1.6. 小結(jié)
從中我們看到册养, elasticsearch
安裝本身并不困難东帅,比較簡單,修改配置文件以及注意 JDK
版本球拦。
1.2. 可視化工具
elasticsearch
的可視化工具有很多靠闭,比如 elasticsearch-head
、Dejavu
坎炼、ElasticHD
等愧膀。
此處選擇 ElasticHD
,比較適合學(xué)習(xí)以及演示点弯,這是一個開源 elasticHD
, Github下載地址
提供Windows和linux扇调,但是這個版本有一個弊端,就是好久沒更新抢肛,湊合著用唄狼钮。
1莲镣、unzip elasticHD_linux_amd64.zip
2瑞侮、chmod -R 777 ElasticHD
3半火、./ElasticHD -p 0.0.0.0:9800
在瀏覽器中打開 http://192.168.244.128:9800/
就可以看到。
1.3. 中文分詞插件IK
elasticsearch
本身對中文支持不夠好,所以需要中文的分詞插件消痛,目前主流的都用 IK
秩伞。 以下這是 Google
的中文詞條稠歉。
IK Analyzer是一個開源的带饱,基于java語言開發(fā)的輕量級的中文分詞工具包勺疼。從2006年12月推出1.0版開始酪耕, IKAnalyzer已經(jīng)推出了4個大版本迂烁。最初藏斩,它是以開源項(xiàng)目Luence為應(yīng)用主體的狰域,結(jié)合詞典分詞和文法分析算法的中文分詞組件兆览。從3.0版本開始,IK發(fā)展為面向Java的公用分詞組件驶睦,獨(dú)立于Lucene項(xiàng)目匿醒,同時提供了對Lucene的默認(rèn)優(yōu)化實(shí)現(xiàn)场航。在2012版本中,IK實(shí)現(xiàn)了簡單的分詞歧義排除算法孩饼,標(biāo)志著IK分詞器從單純的詞典分詞向模擬語義分詞衍化镀娶。
1.3.1. IK安裝
安裝地址,截止當(dāng)前 IK 最新版本是 v7.4.0
好啰,但是我 elasticsearch
版本是 7.4所以下載與自己相對應(yīng)的版本鳄抒,否則分詞插件將不能被識別瓤鼻。
- 下載
[root@localhost download]$ wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.0/elasticsearch-analysis-ik-7.4.0.zip
- 安裝插件
在 elasticsearch
安裝目錄下 找到 plugins
文件夾并創(chuàng)建一個名為 ik
的目錄娱仔,將下載的 elasticsearch-analysis-ik-7.10.0.zip
移入。
[elastic@localhost elastic]$ cd plugins
[elastic@localhost plugins]$ cd mkdir ik && cd ik
[elastic@localhost ik]$ cp ../../../download/elasticsearch-analysis-ik-7.4.0.zip .
[elastic@localhost ik]$ unzip elasticsearch-analysis-ik-7.4.0.zip
完成后盹憎,將 elasticsearch
重啟陪每,我們觀察控制臺
其中紅線處就是 elasticsearch
將分詞器加載,說明我們安裝成功盼产。
[2020-12-15T01:19:51,151][INFO ][o.e.p.PluginsService ] [centos8] loaded plugin [analysis-ik]
1.3.2. 分詞器
上一章節(jié)我們演示對中文分詞的安裝,下來我們開始我們分詞器驗(yàn)證之旅灌灾。
使用crul命令锋喜,輸入下面的URL地址,驗(yàn)證分詞器是否成功。
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json" "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中華五千年華夏"}';
有的人喜歡 Postman
功偿,也可以共耍。
至此我們的中文分詞器可以用啦。
1.3.3. ik_max_word和ik_smart
ik_max_word: 將文本按最細(xì)粒度的組合來拆分字旭,比如會將“中華五千年華夏”拆分為“五千年遗淳、五千屈暗、五千年華、華夏弃甥、千年華夏”潘飘,總之是可能的組合;
ik_smart: 最粗粒度的拆分艰毒,比如會將“五千年華夏”拆分為“五千年丑瞧、華夏”
當(dāng)不添加分詞類別,Elastic對于漢字默認(rèn)使用standard只是將漢字拆分成一個個的漢字西乖,而我們ik則更加的智能薄腻,下面通過幾個案例來說明。
1.3.3.1. ik_smart分詞
在JSON格式中添加analyzer節(jié)點(diǎn)內(nèi)容為ik_smart
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json" "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中華五千年華夏","analyzer": "ik_smart"}';
1.3.3.2. ik_max_word分詞
在JSON格式中添加analyzer節(jié)點(diǎn)內(nèi)容為ik_max_word
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json" "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中華五千年華夏","analyzer": "ik_max_word"}';
1.3.4. 自定義分詞
IK
很友好,為我們提供熱更新 IK
分詞蜓斧,在配置文件 {ES_HOME}/plugins/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 擴(kuò)展配置</comment>
<!--用戶可以在這里配置自己的擴(kuò)展字典 -->
<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
<!--用戶可以在這里配置自己的擴(kuò)展停止詞字典-->
<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
<!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展字典 -->
<entry key="remote_ext_dict">location</entry>
<!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典-->
<entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>
我們一般將需要自動更新的熱詞放在一個UTF8的txt文件里,再利用 nginx
直奋,當(dāng) .txt
文件修改時脚线,http server
會在客戶端請求該文件時自動返回相應(yīng)的 Last-Modified
和 ETag
邮绿〈可以另外做一個工具來從業(yè)務(wù)系統(tǒng)提取相關(guān)詞匯,并更新這個 .txt
文件酱鸭。
1.4. 索引
寫到現(xiàn)在怯屉,終于到了索引(Index
)這塊啦防泵,我們先弄清楚索引的基本概念以及索引到底是什么?
ElasticSearch
是文檔型數(shù)據(jù)庫蝗敢,索引(Index
)定義了文檔的邏輯存儲和字段類型,每個索引可以包含多個文檔類型足删,文檔類型是文檔的集合寿谴,文檔以索引定義的邏輯存儲模型,比如失受,指定分片和副本的數(shù)量,配置刷新頻率,分配分析器等,存儲在索引中的海量文檔分布式存儲在ElasticSearch集群中。
ElasticSearch是基于Lucene框架的全文搜索引擎,將所有文檔的信息寫入到倒排索引(Inverted Index)的數(shù)據(jù)結(jié)構(gòu)中细溅,倒排索引建立的是索引中詞和文檔之間的映射關(guān)系邻耕,在倒排索引中御滩,數(shù)據(jù)是面向詞(Term)而不是面向文檔的腕柜。
1.4.1. 創(chuàng)建索引
由于在ElasticSearch 7.x之后就默認(rèn)不在支持指定索引類型,所以在在elasticsearch7.x上執(zhí)行:
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"twitter":{
......
}
}
執(zhí)行結(jié)果則會出錯:Root mapping definition has unsupported parameters(剛開始接觸就踩了這個坑俩功,折煞勞資好久)椿肩。如果在6.x上執(zhí)行厂榛,則會正常執(zhí)行。
出現(xiàn)這個的原因是,elasticsearch7默認(rèn)不在支持指定索引類型,默認(rèn)索引類型是_doc四敞,如果想改變硬纤,則配置include_type_name: true 即可(這個沒有測試腮鞍,官方文檔說的,無論是否可行祝懂,建議不要這么做,因?yàn)閑lasticsearch8后就不在提供該字段)教届。
https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html
1.4.1.1. 官方例子說明
curl -X PUT "localhost:9200/twitter" -H 'Content-Type: application/json' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
'
-d指定了你的參數(shù),這里將這些參數(shù)放到了
JSON
文件中settings設(shè)置內(nèi)容含義
name | 價格 |
---|---|
number_of_shards | 分片數(shù) |
number_of_replicas | 副本數(shù) |
mappings | 結(jié)構(gòu)化數(shù)據(jù)設(shè)置 下面的一級屬性 是自定義的類型 |
properties | 類型的屬性設(shè)置節(jié)點(diǎn)伐弹,下面都是屬性 |
epoch_millis | 表示時間戳 |
1.4.1.2. 自定義索引
- 使用
JSON
文件創(chuàng)建索引
使用 -d‘@your jsonFile’指定你的JSON
文件合住。下邊我創(chuàng)建了一個索引名稱為product
(可自己定義)的索引靶草。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter?pretty=true" -d'@prod.json'
- 參數(shù)形式創(chuàng)建索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter?pretty=true" -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"dynamic": false,
"properties" : {
"productid":{
"type" : "long"
},
"name":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
},
"short_name":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
},
"desc":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
}
}
}
}
'
1.4.2. 查看索引
1.4.2.1. 全部索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open twitter scSSD1SfRCio4F77Hh8aqQ 3 2 0 0 690b 690b
1.4.2.2. 條件查詢
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter?pretty=true"
{
"twitter" : {
"aliases" : { },
"mappings" : {
"dynamic" : "false",
"properties" : {
"desc" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"name" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"productid" : {
"type" : "long"
},
"short_name" : {
"type" : "text",
"analyzer" : "ik_max_word"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1571153735610",
"number_of_shards" : "3",
"number_of_replicas" : "2",
"uuid" : "scSSD1SfRCio4F77Hh8aqQ",
"version" : {
"created" : "7040099"
},
"provided_name" : "twitter"
}
}
}
}
1.4.3. 查看索引分詞器
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_analyze?pretty=true" -d'
{
"field": "text",
"text": "秦皇漢武."
}
'
1.4.4. 修改索引
1.4.5. 刪除索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:9200/twitter?pretty=true"
1.5. 如何數(shù)據(jù)管理
1.5.1. 添加數(shù)據(jù)
- 這里演示PUT方式為twitter索引添加數(shù)據(jù)派继,并且指定id好渠,應(yīng)當(dāng)注意此處的默認(rèn)類型為<font color=red>_doc</font>拌蜘,還有一種就是采用POST方式添加數(shù)據(jù),并且自動生成主鍵掌实,本文就不再演示滋将,請自行查閱相關(guān)材料邻悬。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter/_doc/1?pretty=true" -d'
{
"productid" : 1,
"name" : "測試添加索引產(chǎn)品名稱",
"short_name" : "測試添加索引產(chǎn)品短標(biāo)題",
"desc" : "測試添加索引產(chǎn)品描述"
}
'
執(zhí)行返回結(jié)果如圖,則添加數(shù)據(jù)成功随闽。
- 指定id為1父丰,還可以加上參數(shù)op_type=create,這樣在創(chuàng)建重復(fù)id時會報錯導(dǎo)致創(chuàng)建失敗掘宪,否則會更新該id的屬性值蛾扇。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter/_doc/1?op_type=create&pretty=true" -d'
{
"productid" : 1,
"name" : "測試添加索引產(chǎn)品名稱",
"short_name" : "測試添加索引產(chǎn)品短標(biāo)題",
"desc" : "測試添加索引產(chǎn)品描述"
}
'
1.5.2. 基礎(chǔ)查詢
1.5.2.1. 查詢所有
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true"
1.5.2.2. 條件查詢
條件查詢會涉及到精確詞查詢、匹配查詢魏滚、多條件查詢镀首、聚合查詢四種,分別為"term"鼠次、"match"更哄、"multi_match"、"multi_match"腥寇。
- 按找數(shù)據(jù)的名稱作為條件查詢匹配
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
"query" : {
"match" : {
"name" : "產(chǎn)品"
}
}
}
'
- 按找數(shù)據(jù)的標(biāo)識作為條件查詢匹配
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
"query" : {
"match" : {
"productid" : 100
}
}
}
'
- 多條件匹配
選擇匹配desc成翩、short_name列作為多條件
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
"query" : {
"multi_match" : {
"query":"產(chǎn)品",
"fields" : ["desc","short_name"]
}
}
}
'
- 當(dāng)沒有匹配任何數(shù)據(jù)適合則如下:
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
> {
> "query" : {
> "match" : {
> "productid" : 100
> }
> }
> }
> '
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
1.5.3. 高級條件查詢
1.5.3.1. 權(quán)重boost查詢
指定一個boost值來控制每個查詢子句的相對權(quán)重,該值默認(rèn)為1赦役。一個大于1的boost會增加該查詢子句的相對權(quán)重麻敌。
索引映射定義的時候指定boost在elasticsearch5之后已經(jīng)棄用。建議在查詢的時候使用扩劝。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
"query": {
"match" : {
"title": {
"query": "quick brown fox",
"boost": 2
}
}
}
}
'
1.5.3.2. 過濾coerce查詢
數(shù)據(jù)不總是我們想要的庸论,由于在轉(zhuǎn)換JSON body為真正JSON 的時候,整型數(shù)字5有可能會被寫成字符串"5"或者浮點(diǎn)數(shù)5.0。coerce屬性可以用來清除臟數(shù)據(jù)棒呛。
一般在以下場景中:
- 字符串會被強(qiáng)制轉(zhuǎn)換為整數(shù)
- 浮點(diǎn)數(shù)被強(qiáng)制轉(zhuǎn)換為整數(shù)
1.5.3.2.1. 創(chuàng)建索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/wongs?pretty=true" -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"properties" : {
"col_1":{
"type" : "integer"
},
"col_2":{
"type":"integer",
"coerce": false
}
}
}
}
'
1.5.3.2.2. 創(chuàng)建第一個數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/wongs/_doc/1?pretty=true" -d'
{
"col_1" : "20"
}
'
結(jié)果為成功聂示,說明col_1列數(shù)據(jù)沒問題。
1.5.3.2.3. 創(chuàng)建第二個數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/wongs/_doc/1?pretty=true" -d'
> {
> "col_2" : "20"
> }
> '
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [col_2] of type [integer] in document with id '1'. Preview of field's value: '20'"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [col_2] of type [integer] in document with id '1'. Preview of field's value: '20'",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Integer value passed as String"
}
},
"status" : 400
}
由于不能被格式化簇秒,數(shù)據(jù)新增失敗鱼喉。
1.5.3.3. copy_to
copy_to允許你創(chuàng)造自定義超級字段_all. 也就是說,多字段的取值被復(fù)制到一個字段并且取值所有字段的取值組合, 并且可以當(dāng)成一個單獨(dú)的字段查詢.
如,first_name和last_name可以合并為full_name字段扛禽。
1.5.3.3.1. 定義索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_copy_to?pretty=true" -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"properties" : {
"first_name":{
"type" : "text",
"copy_to": "full_name"
},
"last_name":{
"type":"text",
"copy_to": "full_name"
},
"full_name":{
"type": "text"
}
}
}
}
'
1.5.3.3.2. 新增數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_copy_to/_doc/1?pretty=true" -d'
> {
> "first_name" : "jack",
> "last_name" : "Rose"
> }
> '
{
"_index" : "idx_copy_to",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
1.5.3.3.3. 查詢數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/idx_copy_to/_search?pretty=true" -d'
{
"query" : {
"match": {
"full_name": {
"query": "jack Rose",
"operator": "and"
}
}
}
}
'
從下圖中得知first_name和 last_name字段取值都被復(fù)制到 full_name 字段锋边。
1.5.3.4. doc_values
是為了加快排序、聚合操作编曼,在建立倒排索引的時候豆巨,額外增加一個列式存儲映射,是一個空間換時間的做法掐场。默認(rèn)是開啟的往扔,對于確定不需要聚合或者排序的字段可以關(guān)閉。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_doc_val?pretty=true" -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"properties" : {
"first_name":{
"type" : "text"
},
"last_name":{
"type":"text",
"doc_values": false
}
}
}
}
'
1.5.3.5. dynamic
默認(rèn)情況下熊户,字段可以自動添加到文檔或者文檔的內(nèi)部對象萍膛,elasticsearc也會自動索引映射字段。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_dynamic?pretty=true" -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"properties" : {
"first_name":{
"type" : "text"
},
"last_name":{
"type":"text",
"doc_values": false
}
}
}
}
'
2. 構(gòu)建集群模式
待完善
3. 源碼
Github演示源碼 嚷堡,記得給Star
Gitee演示源碼蝗罗,記得給Star
4. 相關(guān)章節(jié)
一、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(一)