新聞中心
本篇主要內(nèi)容如下:

前言
項(xiàng)目中我們總是用Kibana 界面來(lái)搜索測(cè)試或生產(chǎn)環(huán)境下的日志,來(lái)看下有沒(méi)有異常信息。Kibana 就是 我們常說(shuō)的 ELK 中的 K。
Kibana 界面如下圖所示:
Kibana 界面
但這些日志檢索原理是什么呢?這里就該我們的 Elasticsearch 搜索引擎登場(chǎng)了。
一、Elasticsearch 簡(jiǎn)介
1.1 什么是 Elasticsearch?
Elasticsearch 是一個(gè)分布式的開(kāi)源搜索和分析引擎,適用于所有類型的數(shù)據(jù),包括文本、數(shù)字、地理空間、結(jié)構(gòu)化和非結(jié)構(gòu)化數(shù)據(jù)。簡(jiǎn)單來(lái)說(shuō)只要涉及搜索和分析相關(guān)的, ES 都可以做。
1.2 Elasticsearch 的用途?
Elasticsearch 在速度和可擴(kuò)展性方面都表現(xiàn)出色,而且還能夠索引多種類型的內(nèi)容,這意味著其可用于多種用例:
比如一個(gè)在線網(wǎng)上商店,您可以在其中允許客戶搜索您出售的產(chǎn)品。在這種情況下,您可以使用Elasticsearch 存儲(chǔ)整個(gè)產(chǎn)品目錄和庫(kù)存,并為它們提供搜索和自動(dòng)完成建議。
搜索手機(jī)
比如收集日志或交易數(shù)據(jù),并且要分析和挖掘此數(shù)據(jù)以查找趨勢(shì),統(tǒng)計(jì)信息,摘要或異常。在這種情況下,您可以使用 Logstash(Elasticsearch / Logstash / Kibana堆棧的一部分)來(lái)收集,聚合和解析數(shù)據(jù),然后讓 Logstash 將這些數(shù)據(jù)提供給 Elasticsearch。數(shù)據(jù)放入 Elasticsearch 后,您可以運(yùn)行搜索和聚合以挖掘您感興趣的任何信息。
1.3 Elasticsearch 的工作原理?
ELK 原理圖
Elasticsearch 是在 Lucene 基礎(chǔ)上構(gòu)建而成的。ES 在 Lucence 上做了很多增強(qiáng)。
Lucene 是apache軟件基金會(huì) 4 的 jakarta 項(xiàng)目組的一個(gè)子項(xiàng)目,是一個(gè)開(kāi)放源代碼的全文檢索引擎工具包,但它不是一個(gè)完整的全文檢索引擎,而是一個(gè)全文檢索引擎的架構(gòu),提供了完整的查詢引擎和索引引擎,部分文本分析引擎(英文與德文兩種西方語(yǔ)言)。Lucene的目的是為軟件開(kāi)發(fā)人員提供一個(gè)簡(jiǎn)單易用的工具包,以方便的在目標(biāo)系統(tǒng)中實(shí)現(xiàn)全文檢索的功能,或者是以此為基礎(chǔ)建立起完整的全文檢索引擎。(來(lái)自百度百科)
Elasticsearch 的原始數(shù)據(jù)從哪里來(lái)?
原始數(shù)據(jù)從多個(gè)來(lái)源 ( 包括日志、系統(tǒng)指標(biāo)和網(wǎng)絡(luò)應(yīng)用程序 ) 輸入到 Elasticsearch 中。
Elasticsearch 的數(shù)據(jù)是怎么采集的?
數(shù)據(jù)采集指在 Elasticsearch 中進(jìn)行索引之前解析、標(biāo)準(zhǔn)化并充實(shí)這些原始數(shù)據(jù)的過(guò)程。這些數(shù)據(jù)在 Elasticsearch 中索引完成之后,用戶便可針對(duì)他們的數(shù)據(jù)運(yùn)行復(fù)雜的查詢,并使用聚合來(lái)檢索自身數(shù)據(jù)的復(fù)雜匯總。這里用到了 Logstash,后面會(huì)介紹。
怎么可視化查看想要檢索的數(shù)據(jù)?
這里就要用到 Kibana 了,用戶可以基于自己的數(shù)據(jù)進(jìn)行搜索、查看數(shù)據(jù)視圖等。
1.4 Elasticsearch 索引是什么?
Elasticsearch 索引指相互關(guān)聯(lián)的文檔集合。Elasticsearch 會(huì)以 JSON 文檔的形式存儲(chǔ)數(shù)據(jù)。每個(gè)文檔都會(huì)在一組鍵 ( 字段或?qū)傩缘拿Q ) 和它們對(duì)應(yīng)的值 ( 字符串、數(shù)字、布爾值、日期、數(shù)值組、地理位置或其他類型的數(shù)據(jù) ) 之間建立聯(lián)系。
Elasticsearch 使用的是一種名為倒排索引的數(shù)據(jù)結(jié)構(gòu),這一結(jié)構(gòu)的設(shè)計(jì)可以允許十分快速地進(jìn)行全文本搜索。倒排索引會(huì)列出在所有文檔中出現(xiàn)的每個(gè)特有詞匯,并且可以找到包含每個(gè)詞匯的全部文檔。
在索引過(guò)程中,Elasticsearch 會(huì)存儲(chǔ)文檔并構(gòu)建倒排索引,這樣用戶便可以近實(shí)時(shí)地對(duì)文檔數(shù)據(jù)進(jìn)行搜索。索引過(guò)程是在索引 API 中啟動(dòng)的,通過(guò)此 API 您既可向特定索引中添加 JSON 文檔,也可更改特定索引中的 JSON 文檔。
1.5 Logstash 的用途是什么?
Logstash 就是 ELK 中的 L。
Logstash 是 Elastic Stack 的核心產(chǎn)品之一,可用來(lái)對(duì)數(shù)據(jù)進(jìn)行聚合和處理,并將數(shù)據(jù)發(fā)送到 Elasticsearch。Logstash 是一個(gè)開(kāi)源的服務(wù)器端數(shù)據(jù)處理管道,允許您在將數(shù)據(jù)索引到 Elasticsearch 之前同時(shí)從多個(gè)來(lái)源采集數(shù)據(jù),并對(duì)數(shù)據(jù)進(jìn)行充實(shí)和轉(zhuǎn)換。
1.6 Kibana 的用途是什么?
Kibana 是一款適用于 Elasticsearch 的數(shù)據(jù)可視化和管理工具,可以提供實(shí)時(shí)的直方圖、線性圖等。
1.7 為什么使用 Elasticsearch
ES 很快,近實(shí)時(shí)的搜索平臺(tái)。
ES 具有分布式的本質(zhì)特質(zhì)。
ES 包含一系列廣泛的功能,比如數(shù)據(jù)匯總和索引生命周期管理。
官方文檔:https://www.elastic.co/cn/what-is/elasticsearch
二、ES 基本概念
2.1 Index ( 索引 )
動(dòng)詞:相當(dāng)于 Mysql 中的 insert
名詞:相當(dāng)于 Mysql 中的 database
與 mysql 的對(duì)比
| 序號(hào) | Mysql | Elasticsearch |
|---|---|---|
| 1 | Mysql 服務(wù) | ES 集群服務(wù) |
| 2 | 數(shù)據(jù)庫(kù) Database | 索引 Index |
| 3 | 表 Table | 類型 Type |
| 4 | 記錄 Records ( 一行行記錄 ) | 文檔 Document ( JSON 格式 ) |
2.2 倒排索引
假如數(shù)據(jù)庫(kù)有如下電影記錄:
1-大話西游
2-大話西游外傳
3-解析大話西游
4-西游降魔外傳
5-夢(mèng)幻西游獨(dú)家解析
分詞:將整句分拆為單詞
| 序號(hào) | 保存到 ES 的詞 | 對(duì)應(yīng)的電影記錄序號(hào) |
|---|---|---|
| A | 西游 | 1,2, 3,4, 5 |
| B | 大話 | 1,2, 3 |
| C | 外傳 | 2,4, 5 |
| D | 解析 | 3,5 |
| E | 降魔 | 4 |
| F | 夢(mèng)幻 | 5 |
| G | 獨(dú)家 | 5 |
檢索:獨(dú)家大話西游
將 獨(dú)家大話西游 解析拆分成 獨(dú)家、大話、西游
ES 中 A、B、G 記錄 都有這三個(gè)詞的其中一種, 所以 1,2, 3,4, 5 號(hào)記錄都有相關(guān)的詞被命中。
1 號(hào)記錄命中 2 次, A、B 中都有 ( 命中 2 次 ) ,而且 1 號(hào)記錄有 2 個(gè)詞,相關(guān)性得分:2 次/2 個(gè)詞=1
2 號(hào)記錄命中 2 個(gè)詞 A、B 中的都有 ( 命中 2 次 ) ,而且 2 號(hào)記錄有 2 個(gè)詞,相關(guān)性得分:2 次/3 個(gè)詞= 0.67
3 號(hào)記錄命中 2 個(gè)詞 A、B 中的都有 ( 命中 2 次 ) ,而且 3 號(hào)記錄有 2 個(gè)詞,相關(guān)性得分:2 次/3 個(gè)詞= 0.67
4 號(hào)記錄命中 2 個(gè)詞 A 中有 ( 命中 1 次 ) ,而且 4 號(hào)記錄有 2 個(gè)詞,相關(guān)性得分:1 次/3 個(gè)詞= 0.33
5 號(hào)記錄命中 2 個(gè)詞 A 中有 ( 命中 2 次 ) ,而且 4 號(hào)記錄有 4 個(gè)詞,相關(guān)性得分:2 次/4 個(gè)詞= 0.5
所以檢索出來(lái)的記錄順序如下:
? 1-大話西游 ( 相關(guān)性得分:1 )
? 2-大話西游外傳 ( 想關(guān)性得分:0.67 )
? 3-解析大話西游 ( 想關(guān)性得分:0.67 )
? 5-夢(mèng)幻西游獨(dú)家解析 ( 想關(guān)性得分:0.5 )
? 4-西游降魔 ( 想關(guān)性得分:0.33 )
三、Docker 搭建環(huán)境
3.1. 搭建 Elasticsearch 環(huán)境
1 ) 下載鏡像文件
- docker pull elasticsearch:7.4.2
2 ) 創(chuàng)建實(shí)例
映射配置文件
- 配置映射文件夾
- mkdir -p /mydata/elasticsearch/config
- 配置映射文件夾
- mkdir -p /mydata/elasticsearch/data
- 設(shè)置文件夾權(quán)限任何用戶可讀可寫
- chmod 777 /mydata/elasticsearch -R
- 配置 http.host
- echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
啟動(dòng) elasticsearch 容器
- docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
- -e "discovery.type"="single-node" \
- -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
- -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
- -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
- -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
- -d elasticsearch:7.4.2
訪問(wèn) elasticsearch 服務(wù)
訪問(wèn):http://192.168.56.10:9200
返回的 reponse
- {
- "name" : "8448ec5f3312",
- "cluster_name" : "elasticsearch",
- "cluster_uuid" : "xC72O3nKSjWavYZ-EPt9Gw",
- "version" : {
- "number" : "7.4.2",
- "build_flavor" : "default",
- "build_type" : "docker",
- "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
- "build_date" : "2019-10-28T20:40:44.881551Z",
- "build_snapshot" : false,
- "lucene_version" : "8.2.0",
- "minimum_wire_compatibility_version" : "6.8.0",
- "minimum_index_compatibility_version" : "6.0.0-beta1"
- },
- "tagline" : "You Know, for Search"
- }
訪問(wèn):http://192.168.56.10:9200/_cat 訪問(wèn)節(jié)點(diǎn)信息
- 127.0.0.1 62 90 0 0.06 0.10 0.05 dilm * 8448ec5f3312
3.2. 搭建 Kibana 環(huán)境
- docker pull kibana:7.4.2
- docker run --name kibana -e ELASTICSEARCH_HOSTS=http://192.168.56.10:9200 -p 5601:5601 -d kibana:7.4.2
訪問(wèn) kibana: http://192.168.56.10:5601/
四、初階檢索玩法
4.1._cat 用法
- GET /_cat/nodes: 查看所有節(jié)點(diǎn)
- GET /_cat/health: 查看 es 健康狀況
- GET /_cat/master: 查看主節(jié)點(diǎn)
- GET /_cat/indices: 查看所有索引
- 查詢匯總:
- /_cat/allocation
- /_cat/shards
- /_cat/shards/{index}
- /_cat/master
- /_cat/nodes
- /_cat/tasks
- /_cat/indices
- /_cat/indices/{index}
- /_cat/segments
- /_cat/segments/{index}
- /_cat/count
- /_cat/count/{index}
- /_cat/recovery
- /_cat/recovery/{index}
- /_cat/health
- /_cat/pending_tasks
- /_cat/aliases
- /_cat/aliases/{alias}
- /_cat/thread_pool
- /_cat/thread_pool/{thread_pools}
- /_cat/plugins
- /_cat/fielddata
- /_cat/fielddata/{fields}
- /_cat/nodeattrs
- /_cat/repositories
- /_cat/snapshots/{repository}
- /_cat/templates
4.2. 索引一個(gè)文檔 ( 保存 )
例子:在 customer 索引下的 external 類型下保存標(biāo)識(shí)為 1 的數(shù)據(jù)。
使用 Kibana 的 Dev Tools 來(lái)創(chuàng)建
- PUT member/external/1
- {
- "name":"jay huang"
- }
Reponse:
- {
- "_index": "member", //在哪個(gè)索引
- "_type": "external",//在那個(gè)類型
- "_id": "2",//記錄 id
- "_version": 7,//版本號(hào)
- "result": "updated",//操作類型
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 9,
- "_primary_term": 1
- }
也可以通過(guò) Postman 工具發(fā)送請(qǐng)求來(lái)創(chuàng)建記錄。
創(chuàng)建一條記錄
注意:
PUT 和 POST 都可以創(chuàng)建記錄。
POST:如果不指定 id,自動(dòng)生成 id。如果指定 id,則修改這條記錄,并新增版本號(hào)。
PUT:必須指定 id,如果沒(méi)有這條記錄,則新增,如果有,則更新。
4.3 查詢文檔
- 請(qǐng)求:http://192.168.56.10:9200/member/external/2
- Reposne:
- {
- "_index": "member", //在哪個(gè)索引
- "_type": "external", //在那個(gè)類型
- "_id": "2", //記錄 id
- "_version": 7, //版本號(hào)
- "_seq_no": 9, //并發(fā)控制字段,每次更新就會(huì)+1,用來(lái)做樂(lè)觀鎖
- "_primary_term": 1, //同上,主分片重新分配,如重啟,就會(huì)變化
- "found": true,
- "_source": { //真正的內(nèi)容
- "name": "jay huang"
- }
- }
_seq_no 用作樂(lè)觀鎖
每次更新完數(shù)據(jù)后,_seq_no 就會(huì)+1,所以可以用作并發(fā)控制。
當(dāng)更新記錄時(shí),如果_seq_no 與預(yù)設(shè)的值不一致,則表示記錄已經(jīng)被至少更新了一次,不允許本次更新。
用法如下:
- 請(qǐng)求更新記錄 2: http://192.168.56.10:9200/member/external/2?if_seq_no=9&&if_primary_term=1
- 返回結(jié)果:
- {
- "_index": "member",
- "_type": "external",
- "_id": "2",
- "_version": 9,
- "result": "updated",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 11,
- "_primary_term": 1
- }
_seq_no 等于 10,且_primary_term=1 時(shí)更新數(shù)據(jù),執(zhí)行一次請(qǐng)求后,再執(zhí)行上面的請(qǐng)求則會(huì)報(bào)錯(cuò):版本沖突
- {
- "error": {
- "root_cause": [
- {
- "type": "version_conflict_engine_exception",
- "reason": "[2]: version conflict, required seqNo [10], primary term [1]. current document has seqNo [11] and primary term [1]",
- "index_uuid": "CX6uwPBKRByWpuym9rMuxQ",
- "shard": "0",
- "index": "member"
- }
- ],
- "type": "version_conflict_engine_exception",
- "reason": "[2]: version conflict, required seqNo [10], primary term [1]. current document has seqNo [11] and primary term [1]",
- "index_uuid": "CX6uwPBKRByWpuym9rMuxQ",
- "shard": "0",
- "index": "member"
- },
- "status": 409
- }
4.4 更新文檔
用法
POST 帶 _update 的更新操作,如果原數(shù)據(jù)沒(méi)有變化,則 repsonse 中的 result 返回 noop ( 沒(méi)有任何操作 ) ,version 也不會(huì)變化。
請(qǐng)求體中需要用 doc 將請(qǐng)求數(shù)據(jù)包裝起來(lái)。
- POST 請(qǐng)求:http://192.168.56.10:9200/member/external/2/_update
- {
- "doc":{
- "name":"jay huang"
- }
- }
- 響應(yīng):
- {
- "_index": "member",
- "_type": "external",
- "_id": "2",
- "_version": 12,
- "result": "noop",
- "_shards": {
- "total": 0,
- "successful": 0,
- "failed": 0
- },
- "_seq_no": 14,
- "_primary_term": 1
- }
使用場(chǎng)景:對(duì)于大并發(fā)更新,建議不帶 _update。對(duì)于大并發(fā)查詢,少量更新的場(chǎng)景,可以帶_update,進(jìn)行對(duì)比更新。
更新時(shí)增加屬性
請(qǐng)求體重增加 age 屬性
- http://192.168.56.10:9200/member/external/2/_update
- request:
- {
- "doc":{
- "name":"jay huang",
- "age": 18
- }
- }
- response:
- {
- "_index": "member",
- "_type": "external",
- "_id": "2",
- "_version": 13,
- "result": "updated",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 15,
- "_primary_term": 1
- }
4.5 刪除文檔和索引
刪除文檔
- DELETE 請(qǐng)求:http://192.168.56.10:9200/member/external/2
- response:
- {
- "_index": "member",
- "_type": "external",
- "_id": "2",
- "_version": 2,
- "result": "deleted",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 1,
- "_primary_term": 1
- }
刪除索引
- DELETE 請(qǐng)求:http://192.168.56.10:9200/member
- repsonse:
- {
- "acknowledged": true
- }
沒(méi)有刪除類型的功能
4.6 批量導(dǎo)入數(shù)據(jù)
使用 kinaba 的 dev tools 工具,輸入以下語(yǔ)句
- POST /member/external/_bulk
- {"index":{"_id":"1"}}
- {"name":"Jay Huang"}
- {"index":{"_id":"2"}}
- {"name":"Jackson Huang"}
執(zhí)行結(jié)果如下圖所示:
拷貝官方樣本數(shù)據(jù)
- https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
在 kibana 中執(zhí)行腳本
- POST /bank/account/_bulk
- {"index":{"_id":"1"}}
- {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
- {"index":{"_id":"6"}}
- ......
批量插入樣本數(shù)據(jù)的執(zhí)行結(jié)果
查看所有索引
查看所有索引
可以從返回結(jié)果中看到 bank 索引有 1000 條數(shù)據(jù),占用了 440.2kb 存儲(chǔ)空間。
五、高階檢索玩法
5.1 兩種查詢方式
5.1.1 URL 后接參數(shù)
GET bank/_search?q=*&sort=account_number: asc
```/_search?q=*&sort=account_number: asc`
查詢出所有數(shù)據(jù),共 1000 條數(shù)據(jù),耗時(shí) 1ms,只展示 10 條數(shù)據(jù) ( ES 分頁(yè) )
URL 后接參數(shù)
屬性值說(shuō)明:
- took – ES 執(zhí)行搜索的時(shí)間 ( 毫秒 )
- timed_out – ES 是否超時(shí)
- _shards – 有多少個(gè)分片被搜索了,以及統(tǒng)計(jì)了成功/失敗/跳過(guò)的搜索的分片
- max_score – 最高得分
- hits.total.value - 命中多少條記錄
- hits.sort - 結(jié)果的排序 key 鍵,沒(méi)有則按 score 排序
- hits._score - 相關(guān)性得分
- 參考文檔:
- https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-search.html
5.1.2 URL 加請(qǐng)求體進(jìn)行檢索 ( QueryDSL )
請(qǐng)求體中寫查詢條件
語(yǔ)法:
- GET bank/_search
- {
- "query":{"match_all": {}},
- "sort": [
- {"account_number": "asc" }
- ]
- }
示例:查詢出所有,先按照 accout_number 升序排序,再按照 balance 降序排序
URL 加請(qǐng)求體進(jìn)行檢索
5.2 詳解 QueryDSL 查詢
DSL: Domain Specific Language
5.2.1 全部匹配 match_all
示例:查詢所有記錄,按照 balance 降序排序,只返回第 11 條記錄到第 20 條記錄,只顯示 balance 和 firstname 字段。
- GET bank/_search
- {
- "query": {
- "match_all": {}
- },
- "sort": [
- {
- "balance": {
- "order": "desc"
- }
- }
- ],
- "from": 10,
- "size": 10,
- "_source": ["balance", "firstname"]
- }
5.2.2 匹配查詢 match
基本類型 ( 非字符串 ) ,精確匹配
- GET bank/_search
- {
- "query": {
- "match": {"account_number": "30"}
- }
- }
字符串,全文檢索
- GET bank/_search
- {
- "query": {
- "match": {
- "address": "mill road"
- }
- }
- }
字符串全文檢索
全文檢索按照評(píng)分進(jìn)行排序,會(huì)對(duì)檢索條件進(jìn)行分詞匹配。
查詢 address 中包含 mill 或者 road 或者 mill road 的所有記錄,并給出相關(guān)性得分。
查到了 32 條記錄,最高的一條記錄是 Address = "990 Mill Road",得分:8.926605. Address="198 Mill Lane" 評(píng)分 5.4032025,只匹配到了 Mill 單詞。
5.2.3 短語(yǔ)匹配 match_phase
將需要匹配的值當(dāng)成一個(gè)整體單詞 ( 不分詞 ) 進(jìn)行檢索
- GET bank/_search
- {
- "query": {
- "match_phrase": {
- "address": "mill road"
- }
- }
- }
查出 address 中包含 mill road 的所有記錄,并給出相關(guān)性得分
5.2.4 多字段匹配 multi_match
- GET bank/_search
- {
- "query": {
- "multi_match": {
- "query": "mill land",
- "fields": [
- "state",
- "address"
- ]
- }
- }
- }
multi_match 中的 query 也會(huì)進(jìn)行分詞。
查詢 state 包含 mill 或 land 或者 address 包含 mill 或 land 的記錄。
5.2.5 復(fù)合查詢 bool
復(fù)合語(yǔ)句可以合并任何其他查詢語(yǔ)句,包括復(fù)合語(yǔ)句。復(fù)合語(yǔ)句之間可以相互嵌套,可以表達(dá)復(fù)雜的邏輯。
搭配使用 must,must_not,should
must: 必須達(dá)到 must 指定的條件。 ( 影響相關(guān)性得分 )
must_not: 必須不滿足 must_not 的條件。 ( 不影響相關(guān)性得分 )
should: 如果滿足 should 條件,則可以提高得分。如果不滿足,也可以查詢出記錄。 ( 影響相關(guān)性得分 )
示例:查詢出地址包含 mill,且性別為 M,年齡不等于 28 的記錄,且優(yōu)先展示 firstname 包含 Winnie 的記錄。
- GET bank/_search
- {
- "query": {
- "bool": {
- "must": [
- {
- "match": {
- "address": "mill"
- }
- },
- {
- "match": {
- "gender": "M"
- }
- }
- ],
- "must_not": [
- {
- "match": {
- "age": "28"
- }
- }],
- "should": [
- {
- "match": {
- "firstname": "Winnie"
- }
- }]
- }
- }
- }
5.2.6 filter 過(guò)濾
不影響相關(guān)性得分,查詢出滿足 filter 條件的記錄。
在 bool 中使用。
- GET bank/_search
- {
- "query": {
- "bool": {
- "filter": [
- {
- "range": {
- "age": {
- "gte":18,
- "lte":40
- }
- }
- }]
- }
- }
- }
5.2.7 term 查詢
匹配某個(gè)屬性的值。
全文檢索字段用 match,其他非 text 字段匹配用 term
keyword:文本精確匹配 ( 全部匹配 )
match_phase:文本短語(yǔ)匹配
- 非 text 字段精確匹配
- GET bank/_search
- {
- "query": {
- "term": {
- "age": "20"
- }
- }
- }
5.2.8 aggregations 聚合
聚合:從數(shù)據(jù)中分組和提取數(shù)據(jù)。類似于 SQL GROUP BY 和 SQL 聚合函數(shù)。
Elasticsearch 可以將命中結(jié)果和多個(gè)聚合結(jié)果同時(shí)返回。
聚合語(yǔ)法:
- "aggregations" : {
- "<聚合名稱 1>" : {
- "<聚合類型>" : {
- <聚合體內(nèi)容>
- }
- [,"元數(shù)據(jù)" : { [
] }]? - [,"aggregations" : { [
]+ }]? - }
- ["聚合名稱 2>" : { ... }]*
- }
示例 1:搜索 address 中包含 big 的所有人的年齡分布 ( 前 10 條 ) 以及平均年齡,以及平均薪資
- GET bank/_search
- {
- "query": {
- "match": {
- "address": "mill"
- }
- },
- "aggs": {
- "ageAggr": {
- "terms": {
- "field": "age",
- "size": 10
- }
- },
- "ageAvg": {
- "avg": {
- "field": "age"
- }
- },
- "balanceAvg": {
- "avg": {
- "field": "balance"
- }
- }
- }
- }
檢索結(jié)果如下所示:
hits 記錄返回了,三種聚合結(jié)果也返回了,平均年齡 34 歲,平均薪資 25208.0,品駿年齡分布:38 歲的有 2 個(gè),28 歲的有一個(gè),32 歲的有一個(gè)
示例 1
如果不想返回 hits 結(jié)果,可以在最后面設(shè)置 size:0
- GET bank/_search
- {
- "query": {
- "match": {
- "address": "mill"
- }
- },
- "aggs": {
- "ageAggr": {
- "terms": {
- "field": "age",
- "size": 10
- }
- }
- },
- "size": 0
- }
示例 2:按照年齡聚合,并且查詢這些年齡段的平均薪資
從結(jié)果可以看到 31 歲的有 61 個(gè),平均薪資 28312.9,其他年齡的聚合結(jié)果類似。
示例 2
示例 3:按照年齡分組,然后將分組后的結(jié)果按照性別分組,然后查詢出這些分組后的平均薪資
- GET bank/_search
- {
- "query": {
- "match_all": {
- }
- },
- "aggs": {
- "ageAggr": {
- "terms": {
- "field": "age",
- "size": 10
- },
- "aggs": {
- "genderAggr": {
- "terms": {
- "field": "gender.keyword",
- "size": 10
- },
- "aggs": {
- "balanceAvg": {
- "avg": {
- "field": "balance"
- }
- }
- }
- }
- }
- }
- },
- "size": 0
- }
從結(jié)果可以看到 31 歲的有 61 個(gè)。其中性別為 M 的 35 個(gè),平均薪資 29565.6,性別為 F 的 26 個(gè),平均薪資 26626.6。其他年齡的聚合結(jié)果類似。
聚合結(jié)果
5.2.9 Mapping 映射
Mapping 是用來(lái)定義一個(gè)文檔 ( document ) ,以及它所包含的屬性 ( field ) 是如何存儲(chǔ)和索引的。
- 定義哪些字符串屬性應(yīng)該被看做全文本屬性 ( full text fields )
- 定義哪些屬性包含數(shù)字,日期或地理位置
- 定義文檔中的所有屬性是否都能被索引 ( _all 配置 )
- 日期的格式
- 自定義映射規(guī)則來(lái)執(zhí)行動(dòng)態(tài)添加屬性
Elasticsearch7 去掉 tpye 概念:
關(guān)系型數(shù)據(jù)庫(kù)中兩個(gè)數(shù)據(jù)庫(kù)表示是獨(dú)立的,即使他們里面有相同名稱的列也不影響使用,但 ES 中不是這樣的。elasticsearch 是基于 Lucence 開(kāi)發(fā)的搜索引擎,而 ES 中不同 type 與名稱相同的 field 最終在 Lucence 中的處理方式是一樣的。
為了區(qū)分不同 type 下的同一名稱的字段,Lucence 需要處理沖突,導(dǎo)致檢索效率下降
ES7.x 版本:URL 中的 type 參數(shù)為可選。
ES8.x 版本:不支持 URL 中的 type 參數(shù)
所有類型可以參考文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
查詢索引的映射
如查詢 my-index 索引的映射
- GET /my-index/_mapping
- 返回結(jié)果:
- {
- "my-index" : {
- "mappings" : {
- "properties" : {
- "age" : {
- "type" : "integer"
- },
- "email" : {
- "type" :
標(biāo)題名稱:別只會(huì)搜日志了,求你懂點(diǎn)原理吧
URL標(biāo)題:http://www.dlmjj.cn/article/dppcoes.html


咨詢
建站咨詢
