日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
別只會(huì)搜日志了,求你懂點(diǎn)原理吧

本篇主要內(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 ) 下載鏡像文件

 
 
 
 
  1. docker pull elasticsearch:7.4.2

2 ) 創(chuàng)建實(shí)例

映射配置文件

 
 
 
 
  1. 配置映射文件夾
  2. mkdir -p /mydata/elasticsearch/config
  3. 配置映射文件夾
  4. mkdir -p /mydata/elasticsearch/data
  5. 設(shè)置文件夾權(quán)限任何用戶可讀可寫
  6. chmod 777 /mydata/elasticsearch -R
  7. 配置 http.host
  8. echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml

啟動(dòng) elasticsearch 容器

 
 
 
 
  1. docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
  2. -e "discovery.type"="single-node" \
  3. -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
  4. -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
  5. -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
  6. -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
  7. -d elasticsearch:7.4.2

訪問(wèn) elasticsearch 服務(wù)

訪問(wèn):http://192.168.56.10:9200

返回的 reponse

 
 
 
 
  1. {
  2.   "name" : "8448ec5f3312",
  3.   "cluster_name" : "elasticsearch",
  4.   "cluster_uuid" : "xC72O3nKSjWavYZ-EPt9Gw",
  5.   "version" : {
  6.     "number" : "7.4.2",
  7.     "build_flavor" : "default",
  8.     "build_type" : "docker",
  9.     "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
  10.     "build_date" : "2019-10-28T20:40:44.881551Z",
  11.     "build_snapshot" : false,
  12.     "lucene_version" : "8.2.0",
  13.     "minimum_wire_compatibility_version" : "6.8.0",
  14.     "minimum_index_compatibility_version" : "6.0.0-beta1"
  15.   },
  16.   "tagline" : "You Know, for Search"
  17. }

訪問(wèn):http://192.168.56.10:9200/_cat 訪問(wèn)節(jié)點(diǎn)信息

 
 
 
 
  1. 127.0.0.1 62 90 0 0.06 0.10 0.05 dilm * 8448ec5f3312

3.2. 搭建 Kibana 環(huán)境

 
 
 
 
  1. docker pull kibana:7.4.2
  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 用法

 
 
 
 
  1. GET /_cat/nodes: 查看所有節(jié)點(diǎn)
  2. GET /_cat/health: 查看 es 健康狀況
  3. GET /_cat/master: 查看主節(jié)點(diǎn)
  4. GET /_cat/indices: 查看所有索引
  5. 查詢匯總:
  6. /_cat/allocation
  7. /_cat/shards
  8. /_cat/shards/{index}
  9. /_cat/master
  10. /_cat/nodes
  11. /_cat/tasks
  12. /_cat/indices
  13. /_cat/indices/{index}
  14. /_cat/segments
  15. /_cat/segments/{index}
  16. /_cat/count
  17. /_cat/count/{index}
  18. /_cat/recovery
  19. /_cat/recovery/{index}
  20. /_cat/health
  21. /_cat/pending_tasks
  22. /_cat/aliases
  23. /_cat/aliases/{alias}
  24. /_cat/thread_pool
  25. /_cat/thread_pool/{thread_pools}
  26. /_cat/plugins
  27. /_cat/fielddata
  28. /_cat/fielddata/{fields}
  29. /_cat/nodeattrs
  30. /_cat/repositories
  31. /_cat/snapshots/{repository}
  32. /_cat/templates

4.2. 索引一個(gè)文檔 ( 保存 )

例子:在 customer 索引下的 external 類型下保存標(biāo)識(shí)為 1 的數(shù)據(jù)。

使用 Kibana 的 Dev Tools 來(lái)創(chuàng)建

 
 
 
 
  1. PUT member/external/1
  2. {
  3. "name":"jay huang"
  4. }

Reponse:

 
 
 
 
  1. {
  2.     "_index": "member", //在哪個(gè)索引
  3.     "_type": "external",//在那個(gè)類型
  4.     "_id": "2",//記錄 id
  5.     "_version": 7,//版本號(hào)
  6.     "result": "updated",//操作類型
  7.     "_shards": {
  8.         "total": 2,
  9.         "successful": 1,
  10.         "failed": 0
  11.     },
  12.     "_seq_no": 9,
  13.     "_primary_term": 1
  14. }

也可以通過(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 查詢文檔

 
 
 
 
  1. 請(qǐng)求:http://192.168.56.10:9200/member/external/2
  2. Reposne:
  3. {
  4.     "_index": "member",   //在哪個(gè)索引
  5.     "_type": "external",  //在那個(gè)類型
  6.     "_id": "2",           //記錄 id
  7.     "_version": 7,        //版本號(hào)
  8.     "_seq_no": 9,         //并發(fā)控制字段,每次更新就會(huì)+1,用來(lái)做樂(lè)觀鎖
  9.     "_primary_term": 1,   //同上,主分片重新分配,如重啟,就會(huì)變化
  10.     "found": true,
  11.     "_source": { //真正的內(nèi)容
  12.         "name": "jay huang"
  13.  }
  14. }

_seq_no 用作樂(lè)觀鎖

每次更新完數(shù)據(jù)后,_seq_no 就會(huì)+1,所以可以用作并發(fā)控制。

當(dāng)更新記錄時(shí),如果_seq_no 與預(yù)設(shè)的值不一致,則表示記錄已經(jīng)被至少更新了一次,不允許本次更新。

用法如下:

 
 
 
 
  1. 請(qǐng)求更新記錄 2: http://192.168.56.10:9200/member/external/2?if_seq_no=9&&if_primary_term=1
  2. 返回結(jié)果:
  3. {
  4.     "_index": "member",
  5.     "_type": "external",
  6.     "_id": "2",
  7.     "_version": 9,
  8.     "result": "updated",
  9.     "_shards": {
  10.         "total": 2,
  11.         "successful": 1,
  12.         "failed": 0
  13.     },
  14.     "_seq_no": 11,
  15.     "_primary_term": 1
  16. }

_seq_no 等于 10,且_primary_term=1 時(shí)更新數(shù)據(jù),執(zhí)行一次請(qǐng)求后,再執(zhí)行上面的請(qǐng)求則會(huì)報(bào)錯(cuò):版本沖突

 
 
 
 
  1. {
  2.     "error": {
  3.         "root_cause": [
  4.  {
  5.                 "type": "version_conflict_engine_exception",
  6.                 "reason": "[2]: version conflict, required seqNo [10], primary term [1]. current document has seqNo [11] and primary term [1]",
  7.                 "index_uuid": "CX6uwPBKRByWpuym9rMuxQ",
  8.                 "shard": "0",
  9.                 "index": "member"
  10.  }
  11.         ],
  12.         "type": "version_conflict_engine_exception",
  13.         "reason": "[2]: version conflict, required seqNo [10], primary term [1]. current document has seqNo [11] and primary term [1]",
  14.         "index_uuid": "CX6uwPBKRByWpuym9rMuxQ",
  15.         "shard": "0",
  16.         "index": "member"
  17.     },
  18.     "status": 409
  19. }

4.4 更新文檔

用法

POST 帶 _update 的更新操作,如果原數(shù)據(jù)沒(méi)有變化,則 repsonse 中的 result 返回 noop ( 沒(méi)有任何操作 ) ,version 也不會(huì)變化。

請(qǐng)求體中需要用 doc 將請(qǐng)求數(shù)據(jù)包裝起來(lái)。

 
 
 
 
  1. POST 請(qǐng)求:http://192.168.56.10:9200/member/external/2/_update
  2. {
  3.     "doc":{
  4.         "name":"jay huang"
  5.  }
  6. }
  7. 響應(yīng):
  8. {
  9.     "_index": "member",
  10.     "_type": "external",
  11.     "_id": "2",
  12.     "_version": 12,
  13.     "result": "noop",
  14.     "_shards": {
  15.         "total": 0,
  16.         "successful": 0,
  17.         "failed": 0
  18.     },
  19.     "_seq_no": 14,
  20.     "_primary_term": 1
  21. }

使用場(chǎng)景:對(duì)于大并發(fā)更新,建議不帶 _update。對(duì)于大并發(fā)查詢,少量更新的場(chǎng)景,可以帶_update,進(jìn)行對(duì)比更新。

更新時(shí)增加屬性

請(qǐng)求體重增加 age 屬性

 
 
 
 
  1. http://192.168.56.10:9200/member/external/2/_update
  2. request:
  3. {
  4.     "doc":{
  5.         "name":"jay huang",
  6.         "age": 18
  7.  }
  8. }
  9. response:
  10. {
  11.     "_index": "member",
  12.     "_type": "external",
  13.     "_id": "2",
  14.     "_version": 13,
  15.     "result": "updated",
  16.     "_shards": {
  17.         "total": 2,
  18.         "successful": 1,
  19.         "failed": 0
  20.     },
  21.     "_seq_no": 15,
  22.     "_primary_term": 1
  23. }

4.5 刪除文檔和索引

刪除文檔

 
 
 
 
  1. DELETE 請(qǐng)求:http://192.168.56.10:9200/member/external/2
  2. response:
  3. {
  4.     "_index": "member",
  5.     "_type": "external",
  6.     "_id": "2",
  7.     "_version": 2,
  8.     "result": "deleted",
  9.     "_shards": {
  10.         "total": 2,
  11.         "successful": 1,
  12.         "failed": 0
  13.     },
  14.     "_seq_no": 1,
  15.     "_primary_term": 1
  16. }

刪除索引

 
 
 
 
  1. DELETE 請(qǐng)求:http://192.168.56.10:9200/member
  2. repsonse:
  3. {
  4.     "acknowledged": true
  5. }

沒(méi)有刪除類型的功能

4.6 批量導(dǎo)入數(shù)據(jù)

使用 kinaba 的 dev tools 工具,輸入以下語(yǔ)句

 
 
 
 
  1. POST /member/external/_bulk
  2. {"index":{"_id":"1"}}
  3. {"name":"Jay Huang"}
  4. {"index":{"_id":"2"}}
  5. {"name":"Jackson Huang"}

執(zhí)行結(jié)果如下圖所示:

拷貝官方樣本數(shù)據(jù)

 
 
 
 
  1. https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json

在 kibana 中執(zhí)行腳本

 
 
 
 
  1. POST /bank/account/_bulk
  2. {"index":{"_id":"1"}}
  3. {"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"}
  4. {"index":{"_id":"6"}}
  5. ......

 批量插入樣本數(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ō)明:

 
 
 
 
  1. took – ES 執(zhí)行搜索的時(shí)間 ( 毫秒 )
  2. timed_out – ES 是否超時(shí)
  3. _shards – 有多少個(gè)分片被搜索了,以及統(tǒng)計(jì)了成功/失敗/跳過(guò)的搜索的分片
  4. max_score – 最高得分
  5. hits.total.value - 命中多少條記錄
  6. hits.sort - 結(jié)果的排序 key 鍵,沒(méi)有則按 score 排序
  7. hits._score - 相關(guān)性得分
  8. 參考文檔:
  9. 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ǔ)法:

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query":{"match_all": {}},
  4.   "sort": [
  5.     {"account_number": "asc" }
  6.  ]
  7. }

示例:查詢出所有,先按照 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 字段。

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_all": {}
  5.   },
  6.   "sort": [
  7.  {
  8.       "balance": {
  9.         "order": "desc"
  10.  }
  11.  }
  12.   ],
  13.   "from": 10,
  14.   "size": 10,
  15.   "_source": ["balance", "firstname"]
  16. }

5.2.2 匹配查詢 match

基本類型 ( 非字符串 ) ,精確匹配

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {"account_number": "30"}
  5.  }
  6. }

字符串,全文檢索

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address": "mill road"
  6.  }
  7.  }
  8. }

 字符串全文檢索

全文檢索按照評(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)行檢索

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_phrase": {
  5.       "address": "mill road"
  6.  }
  7.  }
  8. }

查出 address 中包含 mill road 的所有記錄,并給出相關(guān)性得分

5.2.4 多字段匹配 multi_match

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "multi_match": {
  5.       "query": "mill land",
  6.       "fields": [
  7.         "state",
  8.         "address"
  9.  ]
  10.  }
  11.  }
  12. }

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 的記錄。

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "must": [
  6.        {
  7.           "match": {
  8.             "address": "mill"
  9.           }
  10.        },
  11.        {
  12.           "match": {
  13.             "gender": "M"
  14.           }
  15.       }
  16.      ],
  17.       "must_not": [
  18.       {
  19.           "match": {
  20.             "age": "28"
  21.       }
  22.      }],
  23.       "should": [
  24.       {
  25.           "match": {
  26.             "firstname": "Winnie"
  27.           }
  28.       }]
  29.    }
  30.  }
  31. }

5.2.6 filter 過(guò)濾

不影響相關(guān)性得分,查詢出滿足 filter 條件的記錄。

在 bool 中使用。

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": [
  6.        {
  7.           "range": {
  8.             "age": {
  9.               "gte":18,
  10.               "lte":40
  11.             }
  12.          }
  13.       }]
  14.    }
  15.  }
  16. }

5.2.7 term 查詢

匹配某個(gè)屬性的值。

全文檢索字段用 match,其他非 text 字段匹配用 term

keyword:文本精確匹配 ( 全部匹配 )

match_phase:文本短語(yǔ)匹配

 
 
 
 
  1. 非 text 字段精確匹配
  2. GET bank/_search
  3. {
  4.   "query": {
  5.     "term": {
  6.       "age": "20"
  7.     }
  8.   }
  9. }

5.2.8 aggregations 聚合

聚合:從數(shù)據(jù)中分組和提取數(shù)據(jù)。類似于 SQL GROUP BY 和 SQL 聚合函數(shù)。

Elasticsearch 可以將命中結(jié)果和多個(gè)聚合結(jié)果同時(shí)返回。

聚合語(yǔ)法:

 
 
 
 
  1. "aggregations" : {
  2.     "<聚合名稱 1>" : {
  3.         "<聚合類型>" : {
  4.             <聚合體內(nèi)容>
  5.         }
  6.         [,"元數(shù)據(jù)" : {  [] }]?
  7.         [,"aggregations" : { []+ }]?
  8.     }
  9.     ["聚合名稱 2>" : { ... }]*
  10. }

示例 1:搜索 address 中包含 big 的所有人的年齡分布 ( 前 10 條 ) 以及平均年齡,以及平均薪資

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address": "mill"
  6.  }
  7.   },
  8.   "aggs": {
  9.     "ageAggr": {
  10.       "terms": {
  11.         "field": "age",
  12.         "size": 10
  13.  }
  14.     },
  15.     "ageAvg": {
  16.       "avg": {
  17.         "field": "age"
  18.  }
  19.     },
  20.     "balanceAvg": {
  21.       "avg": {
  22.         "field": "balance"
  23.    }
  24.   }
  25.  }
  26. }

檢索結(jié)果如下所示:

hits 記錄返回了,三種聚合結(jié)果也返回了,平均年齡 34 歲,平均薪資 25208.0,品駿年齡分布:38 歲的有 2 個(gè),28 歲的有一個(gè),32 歲的有一個(gè)

 示例 1

如果不想返回 hits 結(jié)果,可以在最后面設(shè)置 size:0

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address": "mill"
  6.  }
  7.   },
  8.   "aggs": {
  9.     "ageAggr": {
  10.       "terms": {
  11.         "field": "age",
  12.         "size": 10
  13.     }
  14.    }
  15.   },
  16.   "size": 0
  17. }

示例 2:按照年齡聚合,并且查詢這些年齡段的平均薪資

從結(jié)果可以看到 31 歲的有 61 個(gè),平均薪資 28312.9,其他年齡的聚合結(jié)果類似。

 示例 2

示例 3:按照年齡分組,然后將分組后的結(jié)果按照性別分組,然后查詢出這些分組后的平均薪資

 
 
 
 
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_all": {
  5.  }
  6.   },
  7.   "aggs": {
  8.     "ageAggr": {
  9.       "terms": {
  10.         "field": "age",
  11.         "size": 10
  12.       },
  13.       "aggs": {
  14.         "genderAggr": {
  15.           "terms": {
  16.             "field": "gender.keyword",
  17.             "size": 10
  18.           },
  19.           "aggs": {
  20.             "balanceAvg": {
  21.               "avg": {
  22.                 "field": "balance"
  23.             }
  24.          }
  25.        }
  26.       }
  27.      }
  28.    }
  29.   },
  30.   "size": 0
  31. }

從結(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 索引的映射

 
 
 
 
  1. GET /my-index/_mapping
  2. 返回結(jié)果:
  3. {
  4.   "my-index" : {
  5.     "mappings" : {
  6.       "properties" : {
  7.         "age" : {
  8.           "type" : "integer"
  9.         },
  10.         "email" : {
  11.           "type" :  標(biāo)題名稱:別只會(huì)搜日志了,求你懂點(diǎn)原理吧
    URL標(biāo)題:http://www.dlmjj.cn/article/dppcoes.html