學(xué)習(xí)地址為極客時間《Elasticsearch核心技術(shù)與實戰(zhàn)》镣隶,這只是我做的筆記劝赔,僅供參考荠诬;
三龟梦、Elasticsearch
入門
1. Elasticsearch
基本概念
1.1 文檔
-
Elasticsearch
是面向文檔的汁蝶,<font size = 3 color = red>文檔是所有可搜索數(shù)據(jù)的最小單位</font>- 日志文件中的日志項
- 一本電影的具體信息/一張唱片的詳細(xì)信息
-
MP3
播放器里的一首歌/一篇PDF
文檔中的具體內(nèi)容
- 文檔會被序列化成
Json
格式渐扮,保存在Elasticsearch
中-
Json
對象由字段組成 - 每個字段都有對應(yīng)的字段類型(字符串/數(shù)值/布爾/日期/二進制/范圍類型)
-
- 每個文檔都有一個
UniqueID
- 你可以自己指定
ID
- 或者通過
Elasticsearch
自動生成
- 你可以自己指定
1.1.1 Json文檔
- 一篇文檔包含了一系列的字段。類似數(shù)據(jù)庫表中一條記錄
-
Json
文檔掖棉,格式靈活墓律,不需要預(yù)先定義格式- 字段的類型可以指定或者通過
Elasticsearch
自動推算 - 支持?jǐn)?shù)據(jù)/支持嵌套
- 字段的類型可以指定或者通過
1.1.2 文檔元數(shù)據(jù)
{
"index" : "movies",
"_type" : "_doc",
"_id" : "1",
"_score" : 14.69302,
"_source" : {
"year" : 1995,
"@version" : "1",
"genre" : [
"Adventure",
"Animation",
"Children",
"Comedy",
"Fantasy"
],
"id" : "1",
"title" : "Toy Story"
}
},
- 元數(shù)據(jù),用于標(biāo)注文檔的相關(guān)信息
-
_index
: 文檔所屬的索引名 -
_type
: 文檔所屬的類型名 -
_id
: 文檔唯一ID
-
_source
: 文檔的原始JSON
數(shù)據(jù) -
_all
: 整合所有字段內(nèi)容到該字段幔亥,已被廢除 -
_version
: 文檔的版本信息 -
_score
: 相關(guān)性打分
-
1.2 索引
{
"movies" : {
"settings" : {
"index" : {
"creation_date" : "1604218204918",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "F9-uy1bUTemm1Hs_LaDMQQ",
"version" : {
"created" : "7010099"
},
"provided_name" : "movies"
}
}
}
}
-
index
: 索引是文檔的容器耻讽,是一類文檔的結(jié)合-
index
體現(xiàn)了邏輯空間的概念:每個索引都有自己的Mapping
定義,用于定義包含文檔的字段名和字段類型 -
Shard
體現(xiàn)了物理空間的概念:索引中的數(shù)據(jù)分散在Shard
中
-
- 索引的
Mapping
與Setting
-
Mapping
定義文檔字段的類型 -
Setting
定義不同的數(shù)據(jù)分布
-
1.2.1 索引的不同語義
image
- 名詞:一個
Elasticsearch
集群中帕棉,可以創(chuàng)建很多不同的索引 - 動詞:保存一個文檔到
Elasticsearch
的過程也叫索引(indexing
)-
ES
中针肥,創(chuàng)建一個倒排索引的過程
-
- 名詞:一個
B樹
索引饼记,一個倒排索引
1.2.2 Type
(這個我也不知道是干什么的)
image
1.2.3 ES
和RDBMS
對比
RDBMS | Elasticsearch |
---|---|
Table | index(Type) |
Row | Document |
Column | Filed |
Schema | Mapping |
SQL | DSL |
- 傳統(tǒng)關(guān)系型數(shù)據(jù)庫和
Elasticsearch
的區(qū)別-
Elasticsearch
-
Schemaless
:數(shù)據(jù)組織更加靈活 - 相關(guān)性:算相關(guān)度
- 高性能全文檢索
-
-
RDBMS
- 事務(wù)
Join
-
1.3 REST API
image
1.4 kibana
上索引管理
image
image
//查看索引相關(guān)信息
GET movies
//查看索引的文檔總數(shù)
GET movies/_count
//查看前10條文檔,了解文檔格式
POST movies/_search{
}
//_cat indices API
//查看indices
GET /_cat/indices/movies*?v&s=index
//查看狀態(tài)未綠的索引
GET /_cat/indices?v&health=green
//按照文檔個數(shù)排序
GET /_cat/indices?v&s=docs.count:desc
//查看具體的字段
GET /_cat/indices/movies*?pri&v&h=health,index,pri,rep,docs.count,mt
//How much memory is used per index?
GET /_cat/indices?v&h=i,tm&s=tm:desc
1.5 分布式系統(tǒng)的可用性和擴展性
1.5.1 高可用
- 服務(wù)可用性-運行有節(jié)點停止服務(wù)
- 數(shù)據(jù)可用性-部分節(jié)點丟失慰枕,不會丟失數(shù)據(jù)
1.5.2 可擴展性
- 請求量提升/數(shù)據(jù)的不斷增長(將數(shù)據(jù)分布到所有節(jié)點上)
1.6 ES
的分布式特性
1.6.1 ES
的分布式架構(gòu)的好處
- 存儲的水平擴容
- 提高系統(tǒng)的可用性握恳,部分節(jié)點停止服務(wù),整個集群的服務(wù)不受影響
1.6.2 ES
的分布式架構(gòu)
- 不同的集群通過不同的名字來區(qū)分捺僻,默認(rèn)名字
elasticsearch
- 通過配置文件修改乡洼,或者在命令行中
-E cluster.name=node1
進行設(shè)定 - 一個集群可以有一個或者多個節(jié)點
1.7 ES
節(jié)點
- 節(jié)點是一個
Elasticsearch
的實例- 本質(zhì)上就是一個<font size = 3 color = red>
Java
進程</font> - 一臺機器上可以運行多個
Elasticsearch
進程,但是生產(chǎn)環(huán)境一般建議一臺機器上只運行一個Elasticsearch
實例
- 本質(zhì)上就是一個<font size = 3 color = red>
- 每一個節(jié)點都有名字匕坯,通過配置文件配置束昵,或者啟動時候
-E node.name=node1
指定 - 每一個節(jié)點在啟動之后,會分配一個
UID
葛峻,保存在data
目錄下
1.8 Master-eligible nodes
和Master Node
- 每個節(jié)點啟動后锹雏,默認(rèn)就是一個
Master eligible
節(jié)點- 可以設(shè)置
node.master:false
禁止
- 可以設(shè)置
-
Master-eligible
節(jié)點可以參加選主流程,成為Master
節(jié)點 - 當(dāng)?shù)谝粋€節(jié)點啟動時候术奖,它會將自己選舉成
Master
節(jié)點 - 每個節(jié)點上都保存了集群的狀態(tài)礁遵,只有
Master
節(jié)點才能修改集群的狀態(tài)信息- 集群狀態(tài)(Cluster State),維護了一個集群中,必要的信息
- 所有的節(jié)點信息
- 所有的索引和其相關(guān)的
Mapping
和Setting
信息 - 分片的路由信息
- 任意節(jié)點都能修改信息會導(dǎo)致數(shù)據(jù)的不一致性
- 集群狀態(tài)(Cluster State),維護了一個集群中,必要的信息
1.8 Data Node & Coordinating Node
-
Data Node
- 可以保存數(shù)據(jù)的節(jié)點采记,叫做
Data Node
佣耐。負(fù)責(zé)保存分片數(shù)據(jù)。在數(shù)據(jù)擴展上起到了至關(guān)重要的作用
- 可以保存數(shù)據(jù)的節(jié)點采记,叫做
-
Coordinating Node
- 負(fù)責(zé)接受
Client
的請求唧龄,將請求分發(fā)到合適的節(jié)點兼砖,最終把結(jié)果匯集到一起 - 每個節(jié)點默認(rèn)都起到了
Coordinating Node
的職責(zé)
- 負(fù)責(zé)接受
1.9 其他節(jié)點類型
-
Hot
和Warm Node
- 不同硬件配置的
Data Node
,用來實現(xiàn)Hot
和Warm
架構(gòu),降低集群部署的成本
- 不同硬件配置的
-
Machine Learing Node
- 負(fù)責(zé)跑機器學(xué)習(xí)的
Job
,用來做異常檢測
- 負(fù)責(zé)跑機器學(xué)習(xí)的
-
Tribe Node
- (5.3開始使用
Cross Cluster Serarch
)Tribe Node
連接到不同的Elasticsearch
集群既棺,并且支持將這些集群當(dāng)成一個單獨的集群處理
- (5.3開始使用
1.10 配置節(jié)點類型
- 開發(fā)環(huán)境中一個節(jié)點可以承擔(dān)多種角色
- 生產(chǎn)環(huán)境中讽挟,應(yīng)該設(shè)置單一的角色的節(jié)點(
dedicated node
)
節(jié)點類型 | 配置參數(shù) | 默認(rèn)值 |
---|---|---|
master eligible | node.master | true |
data | node.data | true |
ingest | node.ingest | true |
coordinating only | 無 | 每個節(jié)點默認(rèn)都是 coordinating 節(jié)點。設(shè)置其他類型全部為false
|
machine learing | node.ml | true(需 enable x-pack) |
1.11 分片(Primary Shard
& Replica Shard
)
- 主分片丸冕,用以解決數(shù)據(jù)水平擴展的問題耽梅。通過主分片,可以將數(shù)據(jù)分布到集群內(nèi)的所有節(jié)點之上
- 一個分片是一個運行的
Lucene
的實例 - 主分片數(shù)在索引創(chuàng)建時指定胖烛,后續(xù)不允許修改眼姐,除非
Reindex
- 一個分片是一個運行的
- 副本,用以解決數(shù)據(jù)高可用的問題洪己。分片時主分片的拷貝
- 副本分片數(shù)妥凳,可以動態(tài)的調(diào)整
- 增加副本數(shù)竟贯,還可以在一定程度上提高服務(wù)的可用性(讀取的吞吐)
- 一個三節(jié)點的集群中答捕,
blogs
索引的分片分布情況-
思考:增加一個節(jié)點或改大主分片數(shù)對系統(tǒng)的影響?
image
-
1.12 分片的設(shè)定(主分片是在創(chuàng)建時設(shè)定的屑那,后期不能改拱镐,除非reindex
)
- 對與生產(chǎn)環(huán)境中分片的設(shè)定艘款,需要提前做好容量規(guī)劃
- 分片數(shù)設(shè)置過小
- 導(dǎo)致后續(xù)無法增加節(jié)點實現(xiàn)水平擴展
- 單個分片的數(shù)據(jù)量太大,導(dǎo)致數(shù)據(jù)重新分配耗時(數(shù)據(jù)傾斜)
- 分片數(shù)設(shè)置過大沃琅,
7.0
開始哗咆,默認(rèn)主分片設(shè)置成1,解決了over-sharding
的問題- 影響搜索結(jié)果的相關(guān)性打分益眉,影響統(tǒng)計結(jié)果的準(zhǔn)確性
- 單個節(jié)點上過多的分片晌柬,會導(dǎo)致資源浪費,同時也會影響性能
- 分片數(shù)設(shè)置過小
1.13 查看集群的健康狀況
GET _cluster/health
image
image
-
Green
:主分片與副本都正常分配 -
Yellow
:主分片全部正常分配郭脂,有副本分片未能正常分配 -
Red
:有主分片未能分配- 例如:當(dāng)服務(wù)器的磁盤容量超過
85%
時年碘,去創(chuàng)建了一個新的索引
- 例如:當(dāng)服務(wù)器的磁盤容量超過
2. 文檔的基本CRUD與批量操作
2.1 文檔的CRUD
類型 | API |
---|---|
Index |
PUT my_index/_doc/1 {"user":"mike","comment":"xxxx"} |
Create |
PUT my_index/_create/1 {"user":"mike","comment":"xxxx"} POST my_index/_doc (不指定ID,自動生成){"user":"mike","comment":"xxxx"} |
Read | GET my_index/_doc/1 |
Update |
POST my_index/_update/1 {"doc":{"user":"mike","comment":"xxxx"}} |
Delete | DELETE my_index/_doc/1 |
-
Type
名展鸡,約定都用_doc
-
Create
:如果ID已經(jīng)存在屿衅,會失敗 -
Index
:如果ID不存在,創(chuàng)建新的文檔莹弊。否則涤久,先刪除現(xiàn)有的文檔,再創(chuàng)建新的文檔忍弛,版本會增加 -
Update
:文檔必須已經(jīng)存在响迂,更新只會對相應(yīng)字段做增量修改
2.1.1 Create
一個文檔
image
- 支持自動生成文檔
Id
和指定文檔Id
兩種方式 - 通過調(diào)用
post users/_doc
- 系統(tǒng)會自動生成
document Id
- 系統(tǒng)會自動生成
- 使用
HTTP PUT user/_create/1
創(chuàng)建時,URI
中顯示指定_create
,此時如果該id
的文檔已經(jīng)存在细疚,操作失敗
image
image
//create document. 自動生成 _id
POST users/_doc
{
"user" : "Mike",
"post_date" : "2020-11-15T19:38:42",
"message" : "trying out Kibana"
}
//create document. 指定Id栓拜。如果id已經(jīng)存在,報錯
PUT users/_doc/1?op_type=create
{
"user" : "Mike",
"post_date" : "2020-11-15T19:38:42",
"message" : "trying out Elasticsearch"
}
2.1.2 Get
一個文檔
image
- 找到文檔惠昔,返回
HTTP 200
- 文檔元信息
- _index/_type/
- 版本信息幕与,同一個
Id
的文檔,即使被刪除镇防,Version
號也會不斷增加 -
_source
中默認(rèn)包含了文檔的所有原始信息
- 文檔元信息
- 找不到文檔啦鸣,返回
HTTP 404
image
image
//Get document by Id
GET users/_doc/1
2.1.3 Index
文檔
//Index,如果文檔已經(jīng)存在,就會刪除舊文檔来氧,寫入新文檔诫给,版本號會+1
PUT users/_doc/1
{
"user" : "Mike"
}
-
Index
和Create
不一樣的地方:如果文檔不存在,就索引新的文檔啦扬。否則現(xiàn)有文檔會被刪除中狂,新的文檔被索引。版本信息+1
image
image
image
image
2.1.4 Update
文檔
-
Update
方法不會刪除原來的文檔扑毡,而是實現(xiàn)真正的數(shù)據(jù)更新 -
Post
方法/Payload
需要包含再doc
中
image
image
image
//Update,在原文檔的基礎(chǔ)上增加字段
POST users/_update/1/
{
"doc":{
"post_date" : "2020-11-15T19:59:42",
"message" : "trying out Elasticsearch"
}
}
2.2 批量操作
2.2.1 Bulk API
image
- 在一次
REST
請求中胃榕,<font size = 3 color = red>重新建立網(wǎng)絡(luò)連接是非常損耗性能的</font> - 支持在一次
API
調(diào)用中,對不同的索引進行操作 - 支持四種類型操作
Index
Create
Update
Delete
- 可以在
URI
中指定Index
,也可以在請求的Payload
中進行 - <font size = 3 color = red>操作中單挑操作失敗瞄摊,并不會影響其他操作</font>
-
返回結(jié)果包含了每一條操作執(zhí)行的結(jié)果
image
//Bulk 操作
//執(zhí)行兩次勋又,查看每次的結(jié)果
//執(zhí)行第一次
POST _bulk
{"index":{"_index":"test","_id":"1"}}
{"field1":"value1"}
{"delete":{"_index":"test","_id":"2"}}
{"create":{"_index":"test2","_id":"3"}}
{"field1":"value3"}
{"update":{"_id":"1","_index":"test"}}
{"doc":{"field2":"value2"}}
2.2.2 批量讀取-mget
-
批量操作苦掘,可以減少網(wǎng)絡(luò)連接所產(chǎn)生的開銷,提高性能
image
image
// mget 操作
GET /_mget
{
"docs":[
{
"_index":"test",
"_id":"1"
},
{
"_index":"test",
"_id":"2"
}
]
}
2.2.3 批量查詢-msearch
### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}
image
2.2.4 常見錯誤返回
問題 | 原因 |
---|---|
無法連接 | 網(wǎng)絡(luò)故障或集群掛了 |
連接無法關(guān)閉 | 網(wǎng)路故障或節(jié)點出錯 |
429 | 集群過于繁忙 |
4xx | 請求體格式有錯 |
500 | 集群內(nèi)部錯誤 |
3. 正排與倒排索引
3.1 正排
image
3.2 倒排索引
如果你想找某個詞楔壤,在書中的具體頁碼鹤啡,那正排就不能滿足了,那我們可以建一個內(nèi)容
->文檔Id
的一種索引結(jié)構(gòu)蹲嚣,就可以滿足我們的需求了
image
3.2.1 倒排索引的核心組成
倒排索引包含兩部分
- 單詞詞典(Term Dictionary),記錄所有文檔的單詞递瑰,記錄單詞到倒排列表的關(guān)聯(lián)關(guān)系
- 單詞詞典一般比較大,可以通過
B+樹
或哈希拉鏈法
實現(xiàn)隙畜,以滿足高性能的插入和查找
- 單詞詞典一般比較大,可以通過
- 倒排序列(Posting List)記錄了單詞對應(yīng)的文檔結(jié)合泣矛,由倒排索引項組成
- 倒排索引項(Posting)
- 文檔Id
- 詞頻TF-該單詞在文檔中出現(xiàn)的次數(shù),用于相關(guān)性評分
- 位置(Position)-單詞在文檔中分詞的位置禾蚕。用于語句搜索(phrase query)
- 偏移(Offset)-記錄單詞的開始結(jié)束位置您朽,實現(xiàn)高亮顯示
- 倒排索引項(Posting)
3.2.2 Elasticsearch
的一個例子
image
3.2.3 Elasticsearch
的倒排索引
-
Elasticsearch
的Json
文檔中的每個字段,都有自己的倒排索引 - 可以指定對某些字段不做索引
- 優(yōu)點:節(jié)省存儲空間
- 缺點:字段無法被搜索
4. 通過Analyzer
進行分詞
4.1 Analysis
與Analyzer
-
Analysis
:文本分析是把全文本轉(zhuǎn)換一系列單詞(term\token
)的過程换淆,也叫分詞 -
Analysis
是通過Analyzer
來實現(xiàn)的- 可使用
Elasticsearch
內(nèi)置的分析器/或者按需定制化分析器
- 可使用
- 除了在數(shù)據(jù)寫入時轉(zhuǎn)換詞條哗总,匹配
Query
語句時候也需要用相同的分析器對查詢語句進行分析
image
4.2 Analyzer
的組成
- 分詞器時專門處理分詞的組件,
Analyzer
由三部分組成-
Character Filters
(針對原始文本處理倍试,例如去除html
) -
Tokenizer
(按照規(guī)則切分為單詞) -
Token Filter
(將切分的單詞進行加工讯屈,小寫,刪除stopword
,增加同義詞)
-
image
5. Elasticsearch
的內(nèi)置分詞器
-
Standard Analyzer
:默認(rèn)分詞器县习,按詞切分涮母,小寫處理 -
Simple Analyzer
:按照非字母切分(符號被過濾),小寫處理 -
Stop Analyzer
:小寫處理躁愿,停用詞過濾(the
,a
,is
) -
Whitespace Analyzer
:按照空格切分叛本,不轉(zhuǎn)小寫 -
Keyword Analyzer
:不分詞,直接將輸入當(dāng)作輸出 -
Patter Analyzer
:正則表達(dá)式彤钟,默認(rèn)\w+(非字符分割)
-
Language
:提供了30多種常用語言的分詞器 -
Customer Analyzer
:自定義分詞器
5.1 使用_analyzer API
5.1.1 直接指定Analyzer
進行測試
image
# 直接指定Analyzer進行測試
GET _analyze
{
"analyzer": "standard",
"text":"Mastering Elasticsearch,elasticsearch in Action"
}
5.2.2 指定索引的字段進行測試
image
# 指定索引的字段進行測試
POST users/_analyze
{
"field":"message",
"text":"Mastering Elasticsearch"
}
5.2.3 自定義分詞器進行測試
image
# 自定義分詞器進行測試
POST _analyze
{
"tokenizer": "standard",
"filter": ["lowercase"],
"text": "Mastering Elasticsearch"
}
5.2 Standard Analyzer
分詞器(<font size = 3 color = red>Elasticsearch
的默認(rèn)分詞器</font>)
- 默認(rèn)分詞器
- 按詞切分
- 小寫處理
image
image
5.3 Simple Analyzer
- 按照非字母切分来候,非字母的都被去除
-
小寫處理
image
image
# Simple Analyzer
GET _analyze
{
"analyzer": "simple",
"text":"2 runing Quick brown-foxes leap over lazy dogs in the summer evening"
}
5.3 Whitespace Analyzer
-
按照空格切分
image
5.4 Stop Analyzer
- 相比
Simple Analyzer
- 多了
stop filter
- 會把
the、a逸雹、is
等修飾性詞語去掉
image
- 會把
5.5 Keyword Analyzer
- 不分詞营搅,直接將輸入當(dāng)一個
term
輸出
image
image
# Keyword Analyzer
GET _analyze
{
"analyzer": "keyword",
"text":"2 runing Quick brown-foxes leap over lazy dogs in the summer evening"
}
5.5 Pattern Analyzer
- 通過正則表達(dá)式進行分詞
- 默認(rèn)是
\w+
,非字符的符號進行分割
image
image
# Pattern Analyzer
GET _analyze
{
"analyzer": "pattern",
"text":"2 runing Quick brown-foxes leap over lazy dogs in the summer evening"
}
5.6 Language Analyzer
-
對不同國家的語言進行分詞
image
# Language Analyzer
POST _analyze
{
"analyzer": "english",
"text":"2 runing Quick brown-foxes leap over lazy dogs in the summer evening"
}
5.7 中文分詞的難點
- 中文句子,切分成一個一個詞(不是一個個字)
- 英文中梆砸,單詞有自然的空格作為分隔
- 一句中文转质,在不同的上下文,有不同的理解
- 這個蘋果帖世,不大好吃/這個蘋果休蟹,不大,好吃!
- 例子
- 他說的確實在理/這事的確定不下來
5.8 ICU Analyzer
- 需要安裝
plugin
Elasticsearch-plugin install analysis-icu
- 提供了
Unicode
的支持鸡挠,更好的支持亞洲語言
image
image
image
POST _analyze
{
"analyzer": "icu_analyzer",
"text":"他說的確實在理"
}
GET /movies/_search
5. Search API
概述
-
URI Search
- 在
URL
中使用查詢參數(shù)
- 在
-
Request Body Search
- 使用
Elasticsearch
提供的刻伊,基于JSON格式的更加完備的Query Domain Specific Language(DSL)
- 使用
5.1 指定查詢的索引
語法 | 范圍 |
---|---|
/_search | 集群上所有的索引(用_search 來標(biāo)明這是一個搜索的請求) |
/index1/_search | 指定索引的名稱為index1 |
/index1,inde-2/_search | 可以指定多個索引正勒,index1和index-2 |
/index*/_search | 以index開頭的索引 |
5.2 URI
查詢
- 使用
"q"
,指定查詢字符串 - "query string syntax",
KV
鍵值對
curl -XGET "http://elasticsearch:9200/kibana_sample_data_ecommerce/_search?q=customer_first_name:Eddie"
image
5.3 Request Body
查詢
image
5.4 搜索的返回結(jié)果(Response)
image
5.5 相關(guān)性
5.5.1 搜索的相關(guān)性
- 搜索時用戶和搜索引擎的對話
- 用戶關(guān)心的時搜索結(jié)果的相關(guān)性
- 是否可以找到所有相關(guān)的內(nèi)容
- 有多少相關(guān)的內(nèi)容被返回了
- 文檔的打分是否合理
- 結(jié)合業(yè)務(wù)需求梭灿,平衡結(jié)果排名
5.5.2 衡量相關(guān)性
-
Information Retrieval
(計算機領(lǐng)域的一門學(xué)課)-
Precision(查準(zhǔn)率)
: 盡可能返回較少的無關(guān)文檔 -
Recall(查全率)
:盡可能返回較多的相關(guān)文檔 -
Ranking
:是否能夠按照相關(guān)度進行排序
-
5.5.3 Precision
and Recall
image
5.6 URI Search
:通過URI query
實現(xiàn)搜索
GET /movies/_search?q=2012&df=title&sort=year:desc&from=o&size=10&timeout=1s
{
"profile":true
}
-
q
:指定查詢語句缔逛,使用Query String Syntax
(<font color = red size = 3>Query String Syntax</font>) -
df
:默認(rèn)字段备埃,不指定時,會對所有字段進行查詢 -
Sort
:排序/from
和size
用于分頁 -
Profile
:可以查看查詢時如何被執(zhí)行的
5.6.1 指定字段查詢 VS 范查詢
-
q=title:2012
/q=2012
image
image
image
image
image
# 指定字段查詢 VS 范查詢
# 指定字段查詢
GET /movies/_search?q=2012&df=title
{
"profile":true
}
# 范查詢
GET /movies/_search?q=2012
{
"profile":true
}
5.6.2 Term
VS Phrase
-
Beautiful Mind
等效于Beautiful OR Mind
褐奴,意思是只要有一個包含按脚,就會返回 -
"Beautiful Mind"
,等效于Beautiful AND Mind
。Phrase
查詢(<font size = 3 color =red>注意:要使用Phrase
查詢敦冬,需要將內(nèi)容用引號引起來</font>)辅搬,還要求前后順序保持一致;意思是這兩個詞都要包含 - 分組與引號
-
title:(Beautiful AND Mind)
分組 title:"Beautiful Mind"
-
image
image
image
image
image
image
image
image
# Term VS Phrase
# Phrase 精確查找
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile": "true"
}
GET /movies/_search?q=title:Beautiful Mind
{
"profile": "true"
}
# TermQuery 分組,Bool查詢
GET /movies/_search?q=title:(Mind Beautiful)
{
"profile": "true"
}
TermQuery
需要用()
括號來把要查找的詞括起來代表一個分組脖旱,不然就會變成上面的那種情況堪遂,兩個Term
在一起的時候默認(rèn)是or
的關(guān)系PhraseQuery
就是需要包含所有詞,且保證順序不變
5.6.3 布爾操作和分組操作
-
AND
/OR
/NOT
或者&&
/||
/!
- 必須大寫
title:(matrix NOT reloaded)
- 分組
-
+
表示must
-
-
表示must_not
-
title:(+matrix -reloaded)
image
-
image
image
image
image
image
# TermQuery 分組萌庆,Bool查詢
GET /movies/_search?q=title:(Mind Beautiful)
{
"profile": "true"
}
# AND
GET /movies/_search?q=title:(Mind AND Beautiful)
{
"profile": "true"
}
# NOT
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile": "true"
}
# +
GET /movies/_search?q=title:(Beautiful %2BMind)
{
"profile": "true"
}
5.6.4 范圍查詢和算數(shù)符號
- 范圍查詢
- 區(qū)間表示:
[]
閉區(qū)間溶褪,{}
開區(qū)間year:{2019 TO 2018}
year:[* TO 2018]
- 區(qū)間表示:
- 算數(shù)符號
year:>2010
year:(>2010 && <=2018)
year:(+>2010 +<=2018)
image
# 所有的電影年份必須>=1980
GET /movies/_search?q=year:>=1980
{
"profile": "true"
}
5.6.5 通配符查詢、正則表達(dá)式践险、模糊匹配與近似查詢
- 通配符查詢(通配符查詢效率低猿妈,占用內(nèi)存大,不建議使用巍虫。特別是放在最前面)
-
?
代表 1 個字符,*
代表 0 或多個字符title:mi?d
title:be*
-
- 正則表達(dá)式
title:[bt]oy
- 模糊匹配與近似查詢
title:befutifl~1
title:“l(fā)ord rings”~2
image
# 通配符查詢
GET /movies/_search?q=title:b*
{
"profile": "true"
}
# 模糊匹配&近似匹配
GET /movies/_search?q=title:beautifl~1
{
"profile": "true"
}
GET /movies/_search?q=title:"lord Ring"~2
{
"profile": "true"
}
5.7 Request Body
與Query DSL
簡介
5.7.1 Request Body Search
- 將查詢語句通過
HTTP Request Body
發(fā)送給Elasticcsearch
-
Query DSL
image
5.7.2 分頁
-
From
從0開始占遥,默認(rèn)返回10個結(jié)果 -
獲取靠后的翻頁成本較高
image
5.7.3 排序
- 最好在"數(shù)字型"與"日期型"字段上排序
-
因為對于多值類型或分析過的字段排序贰剥,系統(tǒng)會選一個值,無法得知該值
image
image
//對日期排序
POST kibana_sample_data_ecommerce/_search
{
"profile": "true",
"sort": [{"order_date": "desc"}],
"query": {
"match_all": {}
}
}
5.7.4 _source filtering
- 如果
_source
沒有存儲,那就只返回匹配的文檔的元數(shù)據(jù) -
_source
支持使用通配符:_source["name*,"desc"]
image
image
//source filstering
POST kibana_sample_data_ecommerce/_search
{
"_source": ["order_date"],
"query": {
"match_all": {}
}
}
5.7.5 腳本字段
-
用例:訂單中有不同的匯率梨水,需要結(jié)合匯率對訂單價格進行排序
image
//腳本字段
GET kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "doc['order_date'].value+'_hello'"
}
}
},
"query": {
"match_all": {}
}
}
5.7.6 使用查詢表達(dá)式:Match Query
(我們之前學(xué)了Term Query
,Phrase Query
)
image
image
//matchQuery OR
POST movies/_search
{
"query": {
"match": {
"title": "last Christmas"
}
}
}
//matchQuery AND
POST movies/_search
{
"query": {
"match": {
"title": {
"query": "last Christmas",
"operator": "and"
}
}
}
}
5.7.7 短語搜索:Match Phrase
查詢
image
image
//matchPhrase
POST movies/_search
{
"query": {
"match_phrase": {
"title": "one love"
}
}
}
//matchPhrase slop:1
POST movies/_search
{
"query": {
"match_phrase": {
"title": {
"query": "one love",
"slop": 1
}
}
}
}
5.8 <font size = 3 color = red>幾種查詢方式的總結(jié)</font>
image
image
5.9 Query String
和Simple Query String
查詢
5.8.1 Query String Query
- 類似
URI Query
image
image
image
5.8.1 Simple Query String Query
- 類似
Query String
,但是會忽略錯誤的語法壮莹,同時只支持部分查詢語法 - 不支持
AND、OR、NOT
,會當(dāng)作字符串處理 -
Term
之間默認(rèn)的關(guān)系是OR
,可以指定Operator
- 支持部分邏輯
-
+
代替AND
-
|
代替OR
-
-
代替NOT
-
image
image
image
PUT /users/_doc/1
{
"name":"Ruan Yiming",
"about":"java,golang,node,swift,elasticsearch"
}
PUT /users/_doc/2
{
"name":"Li Yiming",
"about":"Hadoop"
}
# Query String
POST users/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "Ruan AND Yiming"
}
}
}
POST users/_search
{
"query": {
"query_string": {
"fields": ["name","about"],
"query": "(Ruan AND Yiming) OR (Hadoop)"
}
}
}
# Simple Query String 默認(rèn)operator是 OR
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Ruan AND Yiming",
"fields": ["name"]
}
}
}
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Ruan Yiming",
"fields": ["name"],
"default_operator": "AND"
}
}
}
5.10 <font color = red>es match单山、match_phrase、query_string和term的區(qū)別</font>
https://www.cnblogs.com/chenmz1995/p/10199147.html
6. Dynamic Mapping
和常見字段類型
6.1 什么是Mapping
-
Mapping
類似數(shù)據(jù)庫中的schema
的定義爽篷,作用如下- 定義索引中的字段的名稱
- 定義字段的數(shù)據(jù)類型悴晰,例如字符串,數(shù)字逐工,布爾......
- 字段铡溪,倒排索引的相關(guān)配置,(Analyzed or Not Analyzed,Analyzer)
-
Mapping
會把Json
文檔映射成Lucene
所需要的扁平格式 - 一個
Mapping
屬于一個索引的Type
- 每個文檔都屬于一個
Type
- 一個
Type
有一個Mapping
定義 - 7.0開始泪喊,不需要在
Mapping
定義中指定type
信息
- 每個文檔都屬于一個
6.2 Elasticsearch
字段的數(shù)據(jù)類型
- 簡單類型
Text/Keyword
Date
Integer/Floating
Boolean
IPv4 & IPv6
- 復(fù)雜類型:對象和嵌套對象
- 對象類型/嵌套類型
- 特殊類型
geo_point & geo_shape/percolator
6.3 什么是Dynamic Mapping
- 在寫入文檔時棕硫,如果索引不存在,會自動創(chuàng)建索引
-
Dynamic Mapping
的機制袒啼,使得我們無需手動定義Mappings
,Elasticsearch
會自動根據(jù)文檔信息哈扮,推算出字段的類型 - 但是有時候推算的不對,例如地理位置信息
- 當(dāng)類型如果設(shè)置不對時蚓再,會導(dǎo)致一些功能無法正常運行滑肉,例如
Range
查詢
image
6.4 類型的自動識別
JSON 類型 |
Elasticsearch 類型 |
---|---|
字符串 | 匹配日期格式,設(shè)置成Date 配置數(shù)字設(shè)置為 float 或者long ,該選項默認(rèn)關(guān)閉設(shè)置為 Text 摘仅,并且增加keyword 子字段 |
布爾值 | boolean |
浮點數(shù) | float |
整數(shù) | long |
對象 | Object |
數(shù)組 | 由第一個非空數(shù)值的類型所決定 |
空值 | 忽略 |
image
image
//寫入文檔靶庙,查看Mapping
PUT mapping_test/_doc/1
{
"firstName":"Chan",
"lastName":"Jackie",
"loginDate":"2018-07-24T10:29:48.103Z"
}
//查看Mapping
GET mapping_test/_mapping
//Delete index 刪除索引
DELETE mapping_test
//dynamic mapping,推斷字段的類型
PUT mapping_test/_doc/1
{
"uid":"123",
"isVip":false,
"isAdmin":"true",
"age":19,
"heigh":180
}
#查看Mapping
GET mapping_test/_mapping
6.5 能否更改Mapping
的字段類型
- 兩種情況
- 新增字段
-
Dynamic
設(shè)為true
時,一旦有新增字段的文檔寫入娃属,Mapping
也同時被更新 -
Dynamic
設(shè)為false
,Mapping
不會被更新六荒,新增字段的數(shù)據(jù)無法被索引,但是信息會出現(xiàn)在_source
中 -
Dynamic
設(shè)置成Strict
,文檔寫入失敗
-
- 對已有字段膳犹,一旦一句有數(shù)據(jù)寫入恬吕,就不再支持修改字段定義
-
Lucene
實現(xiàn)的倒排索引,一旦生成后须床,就不允許修改
-
- 如果希望改變字段類型铐料,必須
Reindex API
,重建索引
- 新增字段
- 原因
- 如果修改了字段的數(shù)據(jù)類型,會導(dǎo)致已被索引的屬于無法被搜索
- 但是如果是增加新的字段豺旬,就不會有這樣的影響
6.6 控制Dynamic Mappings
image
"true" |
"false" |
"strict" |
|
---|---|---|---|
文檔可索引 | YES |
YES |
NO |
字段可索引 | YES |
NO |
NO |
Mapping 被更新 |
YES |
NO |
NO |
- 當(dāng)
dynamic
被設(shè)置成false
時候钠惩,存在新增字段的數(shù)據(jù)寫入,該數(shù)據(jù)可以被索引族阅,但是新增字段被丟棄 - 當(dāng)設(shè)置成
Strict
模式時候篓跛,數(shù)據(jù)寫入直接出錯
image
image
image
image
image
image
image
image
#默認(rèn)Mapping支持dynamic,寫入的文檔中加入新的字段
PUT dynamic_mapping_test/_doc/1
{
"newField":"someValue"
}
POST dynamic_mapping_test/_search
{
"query": {
"match": {
"newField": "someValue"
}
}
}
#修改dynamic的值為false
PUT dynamic_mapping_test/_mapping
{
"dynamic":false
}
#新增 anotherField字段,字段依舊可以存入ES
PUT dynamic_mapping_test/_doc/10
{
"anotherField":"someValue"
}
#但是坦刀,該字段不可以被搜索愧沟,因為dynamic的值被我們設(shè)置為了false
POST dynamic_mapping_test/_search
{
"query": {
"match": {
"anotherField": "anotherField"
}
}
}
#查看索引的Mapping文件
GET dynamic_mapping_test/_mapping
#修改dynamic的值為strict
PUT dynamic_mapping_test/_mapping
{
"dynamic":"strict"
}
#寫入數(shù)據(jù)出錯蔬咬,HTTP Code 400
PUT dynamic_mapping_test/_doc/12
{
"lastField":"value"
}
7 顯示Mapping
設(shè)置與常見參數(shù)
7.1 如何顯示定義一個Mapping
image
7.2 自定義Mapping
的一些建議
- 可以參考
API
手冊,純手寫 - 為了減少輸入的工作量沐寺,減少出錯概率林艘,可以依照以下步驟
- 創(chuàng)建一個臨時的
index
,寫入一些樣本數(shù)據(jù) - 通過訪問
Mapping API
獲得該臨時文件的動態(tài)Mapping
定義 - 修改后用,使用該配置創(chuàng)建你的索引
- 刪除臨時索引
- 創(chuàng)建一個臨時的
7.3 控制當(dāng)前字段是否被索引
-
Index
:控制當(dāng)前字段是否被索引混坞。默認(rèn)為true
狐援。如果設(shè)置成false
,該字段不可被搜索
image
image
image
DELETE dynamic_mapping_test
#設(shè)置 index 為 false
DELETE users
PUT users
{
"mappings": {
"properties": {
"firstName":{
"type": "text"
},
"lastName":{
"type": "text"
},
"mobile":{
"type": "text",
"index": false
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName":"Yiming",
"mobile":"123456789"
}
POST /users/_search
{
"query": {
"match": {
"mobile": "123456789"
}
}
}
7.3 index Options
- 對于倒排索引的建立,es提供了四種不同級別的
index Options
配置究孕,可以控制倒排索引記錄的內(nèi)容-
docs
:記錄doc id
-
freqs
:記錄doc id
和term frequencies
-
positions
:記錄doc id
/term frequencies
/term position
-
offsets
:記錄doc id
/term frequencies
/term position
/character offects
-
-
Text
類型默認(rèn)記錄postions
,其他默認(rèn)為docs
-
記錄內(nèi)容越多啥酱,占用存儲空間越大
image
7.4 null_value
- 需要對
null
值實現(xiàn)搜索 - 只有
keyword
類型支持設(shè)定Null_Value
image
image
#設(shè)定Null_value
DELETE users
PUT users
{
"mappings": {
"properties": {
"firstName":{
"type": "text"
},
"lastName":{
"type": "text"
},
"mobile":{
"type": "keyword",
"null_value": "NULL"
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName":"Yiming",
"mobile":null
}
GET users/_search
{
"query": {
"match": {
"mobile": "NULL"
}
}
}
7.5 copy_to
設(shè)置
-
_all
在7
中被copy_to
所替代 - 滿足一些特定的搜索需求
-
copy_to
將字段的數(shù)值拷貝到目標(biāo)字段,實現(xiàn)類似_all
的作用 -
copy_to
的目標(biāo)字段不出現(xiàn)在_source
中
image
image
#設(shè)置 Copy to
DELETE users
PUT users
{
"mappings": {
"properties": {
"firstName":{
"type": "text",
"copy_to": "fullName"
},
"lastName":{
"type": "text",
"copy_to": "fullName"
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName":"Yiming"
}
GET users/_search
{
"query": {
"match": {
"fullName": {
"query": "Ruan Yiming",
"operator": "and"
}
}
}
}
當(dāng)你索引一個文檔的時候厨诸,它如果包含了
firstName
和lastName
镶殷,我們都會把這個值拷貝到fullName
字段上去,當(dāng)你要查詢時泳猬,就可以用fullName
來查詢了
7.6 數(shù)組類型
-
Elasticsearch
中不提供專門的數(shù)組類型批钠。但是任何字段宇植,都可以包含多個相同類型的數(shù)值
image
image
image
#數(shù)組類型
PUT users/_doc/1
{
"name":"onebird",
"interests":"reading"
}
PUT users/_doc/1
{
"name":"twobirds",
"interests":["reading","music"]
}
POST users/_search
{
"query": {
"match_all": {}
}
}
GET users/_mapping
8. 多字段特性及Mapping
中配置自定義Analyzer
8.1 多字段類型
image
- 多字段特性
- 廠商名字實現(xiàn)精確匹配
- 增加一個
keyword
的<font size = 3 color = red>子字段</font>
- 增加一個
- 使用不同的
analyzer
- 不同語言
- pingyin字段的搜索
- 還支持為搜索和索引指定不同的
analyzer
- 廠商名字實現(xiàn)精確匹配
8.2 Exact Values
(精確值) VS Full Text
(全文本)
-
Exact values vs Full Text
-
Exact Value
:包括數(shù)字/日期/具體一個字符串(例如:"Apple Store")-
Elasticsearch
中的keyword
-
- 全文本得封,非結(jié)構(gòu)化的文本數(shù)據(jù)
-
Elasticsearch
中的text
-
-
image
8.2.1 Exact Values
不需要被分詞(Exact Values和Full Text最大的區(qū)別)
-
Elasticsearch
為每一個字段創(chuàng)建一個倒排索引-
Exact Value
在索引時,不需要做特殊的分詞處理
-
image
8.3 自定義分詞
- 當(dāng)
Elasticsearch
自帶的分詞器無法滿足時指郁,可以自定義分詞器忙上。通過組合不同的<font size = 3 color = red>組件</font>實現(xiàn)(下面這三個組件是Analyzer
的組成,可看上面4.2)Character Filter
Tokenizer
Token Filter
8.3.1 Character Filters
- 在
Tokenizer
之前對文本進行處理,例如增加刪除及替換字符闲坎∫咧啵可以配置多個Character Filters
。會影響Tokenizer
的position
和offset
信息 - 一些自帶的
Character Filter
-
HTML strip
: 去除html
標(biāo)簽 -
Mapping
: 字符串替換 -
Pattern replace
: 正則匹配替換
-
image
image
image
POST _analyze
{
"tokenizer": "keyword",
"char_filter": ["html_strip"],
"text": "<b>hello world</b>"
}
//使用 char filter進行替換(將'-'替換為'_')
POST _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type":"mapping",
"mappings":["- => _"]
}
],
"text": "123-456,I-test! test-990 650-555-1234"
}
//char filter替換表情符號
POST _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type":"mapping",
"mappings": [":) => happy",":( => sad"]
}
],
"text": ["I am felling :)","Felling :( today"]
}
//正則表達(dá)式
GET _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type": "pattern_replace",
"pattern": "http://(.*)",
"replacement": "$1"
}
],
"text": ["http://www.elastic.co"]
}
8.3.2 Tokenizer
- 將原始的文本按照一定的規(guī)則腰懂,切分為詞(
term or token
) -
Elasticsearch
內(nèi)置的Tokenizers
-
whitespace
/standard
/uax_url_email
/pattern
/keyword
/path hierarchy
-
- 可以用
java
開發(fā)插件梗逮,實現(xiàn)自己的Tokenizer
image
# Tokenizer
POST _analyze
{
"tokenizer": "path_hierarchy",
"text": "/user/ymruan/a/b/c/d/e"
}
8.3.3 Token Filters
- 將
Tokenizer
輸出的單詞(term
),進行增加,修改绣溜,刪除 - 自帶的
Token Filters
-
Lowercase
/stop
/synonym
(添加近義詞)
-
image
image
image
image
# Token Filters
#whitespace與stop
#按照空格切分慷彤,且將一些特定詞過濾,比如 on in the ,這些詞稱之為stop怖喻,但是這里的第一個大寫的The不會被去除
GET _analyze
{
"tokenizer": "whitespace",
"filter": ["stop"],
"text": ["The rain in Spain falls mainly on the plain."]
}
#加入 lowercase后底哗,The被當(dāng)成 stopword 刪除
#先做了小寫處理
GET _analyze
{
"tokenizer": "whitespace",
"filter": ["lowercase","stop"],
"text": ["The girls in China are playing this game!"]
}
# 如何自定義一個 analyzer 去滿足自己特定的需求
DELETE my_index
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"typpe":"custom",
"char_filter": [
"emoticons"
],
"tokenizer": "punctuation",
"filter": [
"lowercase",
"english_stop"
]
}
},
"tokenizer": {
"punctuation":{
"type": "pattern",
"pattern": "[.,!?]"
}
},
"char_filter": {
"emoticons": {
"type":"mapping",
"mappings": [
":) => _happy_",
":( => _sad_"
]
}
},
"filter": {
"english_stop": {
"type":"stop",
"stopwords": "_english_"
}
}
}
}
}
POST my_index/_analyze
{
"analyzer": "my_custom_analyzer",
"text": "I'm a :) person,and you?"
}
9. Index Template
和Dynamic Template
9.1 什么是Index Template
- 隨著時間的推移,你的集群上會有越來越多的索引锚沸,那比如說你的集群是用來做日志管理的跋选,你會每天為這些日志生成一個新的索引,因為這樣的方式可以讓你的數(shù)據(jù)管理更加合理哗蜈,另外集群會有一個更好的性能前标;
-
Index Template
:幫助你設(shè)定Mappings
和Settings
坠韩,并按照一定的規(guī)則,自動匹配到新創(chuàng)建的索引之上- 模板僅在一個索引被創(chuàng)建時炼列,才會產(chǎn)生作用同眯。修改模板不會影響已創(chuàng)建的索引
- 你可以設(shè)定多個索引模板,這些設(shè)置會被
merge
在一起 - 你可以指定
order
的數(shù)值唯鸭,控制merging
的過程
image
9.1.1 Index Template
的工作方式
- 當(dāng)一個索引被創(chuàng)建時
- 應(yīng)用
Elasticsearch
默認(rèn)的settings
和mappings
- 應(yīng)用
order
數(shù)值低的Index Template
中的設(shè)定 - 應(yīng)用
order
高的Index Template
中的設(shè)定须蜗,之前的設(shè)定會被覆蓋 - 應(yīng)用創(chuàng)建索引時,用戶指定的
Settings
和Mappings
,并覆蓋之前模板中的設(shè)定
- 應(yīng)用
9.1.2 Demo
- 創(chuàng)建2個
Index Template
- 查看根據(jù)名字查看
Template
- 查看所有
templates,_tmplate/*
- 創(chuàng)建一個臨時索引目溉,查看
replica
和數(shù)據(jù)類型推斷 - 將所有名字設(shè)為能與
Index Template
匹配時明肮,查看所生成的Index
的Mappings
和Settings
image
image
image
image
image
image
image
image
# 創(chuàng)建一個模板
PUT _template/template_default
{
"index_patterns": ["*"],
"order" : 0,
"version" : 1,
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
}
}
# 創(chuàng)建第二個模板
PUT /_template/template_test
{
"index_patterns": ["test*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
},
"mappings" : {
"date_detection" : false,
"numeric_detection" : true
}
}
# 查看template信息
GET /_template/template_default
GET /_template/temp*
# 寫入新的數(shù)據(jù),index以test開頭
PUT testtemplate/_doc/1
{
"someNumber" : "1",
"someDate" : "2020/12/05"
}
GET testtemplate/_mapping
GET testtemplate/_settings
# 自己設(shè)定的settings的優(yōu)先級高于template
PUT testmy
{
"settings": {
"number_of_replicas": 5
}
}
PUT testmy/_doc/1
{
"key" : "value"
}
GET testmy/_settings
9.2 什么是Dynamic Template
- 根據(jù)
Elasticseach
識別的數(shù)據(jù)類型缭付,結(jié)合字段名稱柿估,來動態(tài)設(shè)定字段類型- 所有的字符串類型都設(shè)定成
Keyword
,或者關(guān)閉keyword
字段 -
is
開頭的字段都設(shè)置成boolean
-
long_
開頭的都設(shè)置成long
類型
- 所有的字符串類型都設(shè)定成
9.2.1 Dynamic Template
image
-
Dynamic Template
是定義在某個索引的Mapping
中 -
Template
有一個名稱 - 匹配規(guī)則是一個數(shù)組
- 為匹配到字段設(shè)置
Mapping
image
image
GET my_index/_mapping
DELETE my_index
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"string_as_boolean": {
"match_mapping_type": "string",
"match" : "is*",
"mapping": {
"type": "boolean"
}
}
},
{
"string_as_keywords": {
"match_mapping_type" : "string",
"mapping" : {
"type" : "keyword"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"firstName" : "Ruan",
"isVip" : "true"
}
GET my_index/_mapping
10. Elasticsearch
聚合分析簡介
10.1 什么是聚合?
-
Elasticsearch
除了搜索之外陷猫,提供的針對ES
數(shù)據(jù)進行統(tǒng)計分析的功能- 實時性高
Hadoop(T+1)
- 通過聚合秫舌,我們會得到一個數(shù)據(jù)的概覽,是分析和總結(jié)全套的數(shù)據(jù)绣檬,而不是尋找單個文檔
- 尖沙咀和香港島的客房數(shù)量
- 不同的價格區(qū)間足陨,可預(yù)定的經(jīng)濟型酒店和五星級酒店的數(shù)量
- 高性能,只需要一條語句娇未,就可以從
Elasticsearch
得到分析結(jié)果- 無需再客戶端自己去實現(xiàn)分析邏輯
-
Kibana
中大量的可視化報表墨缘,都是聚合分析來的
10.2 聚合的分類
-
Bucket Aggregation
:一些列滿足特定條件的文檔的集合(<font size = 3 color = red>類似Group BY</font>) -
Metric Aggregation
:一些數(shù)學(xué)運算,可以對文檔字段進行統(tǒng)計分析(最大值零抬、最小值镊讼、平均值) -
Pipeline Aggregation
:對其他的聚合結(jié)果進行二次聚合 -
Matrix Aggregation
:支持多個字段的操作并提供一個結(jié)果矩陣
10.3 Bucket
& Metric
-
Bucket
: 可以理解為SQL
中的Group
-
Metric
: 可以理解為SQL
中的Count
,可以執(zhí)行一系列的統(tǒng)計方法
10.3.1 Bucket
image
- 一些例子
- 杭州屬于浙江/一個演員屬于 男或女
- 嵌套關(guān)系:杭州屬于浙江屬于中國屬于亞洲
-
Elasticsearch
提供了很多類型的Bucket
,幫助你用多種方式劃分文檔-
Term & Range
(時間/年齡區(qū)間/地理位置)
-
image
10.3.2 Metric
-
Metric
會基于數(shù)據(jù)集計算結(jié)果平夜,除了支持在字段上進行計算蝶棋,同樣也支持在腳本(painless script
)產(chǎn)生的結(jié)果之上進行計算 - 大多數(shù)
Metric
是數(shù)學(xué)計算,僅輸出一個值-
min
/max
/sum
/avg
/cardinality
-
- 部分
metric
支持輸出多個數(shù)值-
stats
/percentiles
/percentile_ranks
-
image
image