一、前言
?? 工作中幾乎每天都需要使用到ES查詢數(shù)據(jù),需要根據(jù)自己的查詢需求構(gòu)造DSL查詢語句來實(shí)現(xiàn),本文記錄并分享本人工作中常用的DSL語句以及所遇到問題的解決方案,DSL語句靈活多變,可進(jìn)行多種組合,任你搭配,讓我們一起往下看,希望對你有幫助。
二、DSL常用查看索引語句
2.1 查看所有索引的信息(indices?v)
GET _cat/indices
# 以表格形式展示,推薦*
GET _cat/indices?v
2.2 查看特定索引的信息(_stats)
index_name/_stats
2.3 查看索引的映射(_mapping)
index_name/_mapping
2.4 查看索引的設(shè)置(_settings)
GET index_name/_settings
2.5 查看索引的別名(aliases)
GET _cat/aliases
2.6 查看索引的文檔數(shù)量(_count)
GET index_name/_count
2.7 查看索引的存儲大?。╛stats/store)
GET index_name/_stats/store
2.8 查看索引的字段名(_search)
GET index_name/_search?size=0
2.9 查看索引的分片信息(_cat/shards)
GET index_name/_cat/shards
2.10 查看索引的健康狀態(tài)(_cluster/health)
GET _cluster/health/index_name
2.11 查看索引的索引速率(_cat/thread_pool)
GET _cat/thread_pool/index_name
2.12 查看索引的搜索速率(_cat/thread_pool/search)
GET _cat/thread_pool/search
2.13 查看索引的索引速率(_cat/thread_pool/index)
GET _cat/thread_pool/index
2.14 查看索引的刪除速率(_cat/thread_pool/delete)
GET _cat/thread_pool/delete
2.15 查看索引的刷新速率(_cat/thread_pool/refresh)
GET _cat/thread_pool/refresh
2.16 查看索引的合并速率(_cat/thread_pool/merge)
GET _cat/thread_pool/merge
2.17 查看索引的查詢速率(_cat/thread_pool/get)
GET _cat/thread_pool/get
2.18 查看索引的更新速率(_cat/thread_pool/update)
GET _cat/thread_pool/update
三、DSL常用查詢語句
3.1 查詢所有(match_all)
查詢所有字段,默認(rèn)顯示10條數(shù)據(jù)。
GET index_name/?search
{
"query": {
"match_all": {
}
}
}
3.2 關(guān)鍵詞全文查詢(match)
GET index_name/?search
{
"query": {
"match": {
"user": "YiShuoIT"
}
}
}
3.3 精確匹配查詢(term)
GET index_name/?search
{
"query": {
"term": {
"user": "YiShuoIT"
}
}
}
3.4 多值精確匹配查詢(terms)
GET index_name/?search
{
"query": {
"terms": {
"user": ["YiShuoIT", "YiShuo"]
}
}
}
3.5 通配符查詢(wildcard)
GET index_name/?search
{
"query": {
"wildcard": {
"user": "*YiShuo*"
}
}
}
3.6 多值通配符查詢(多wildcard)
GET index_name/?search
{
"query": {
"bool": {
"should": [
{
"wildcard": {
"command": "curl*password*"
}
},
{
"wildcard": {
"user": "YiShuo*"
}
}
]
}
}
}
3.7 字段前綴查詢(prefix)
GET index_name/?search
{
"query":{
"prefix": {"user": "Yi"}
}
}
3.8 多值字段前綴查詢(多prefix)
GET index_name/?search
{
"query": {
"bool": {
"should": [
{
"prefix": {
"user": "Yi"
}
},
{
"prefix": {
"command": "curl"
}
}
]
}
}
}
3.9 模糊匹配查詢(fuzzy)
?? fuzziness
參數(shù)用于指定模糊匹配的容忍度。它可以是一個(gè)數(shù)字(表示編輯距離)或一個(gè)字符串(表示模糊度)。例如,“2” 表示編輯距離為 2,“auto” 表示根據(jù)術(shù)語的長度自動(dòng)計(jì)算模糊度。
GET index_name/?search
{
"query": {
"fuzzy": {
"user": {
"value": "yi",
"fuzziness": "2"
}
}
}
}
3.10 正則表達(dá)式查詢(regexp)
GET index_name/?search
{
"query": {
"regexp": {
"user": ".*YiShuo.*"
}
}
}
3.11 根據(jù)當(dāng)天時(shí)間查詢(range + filter + now)
GET index_name/?search
{
"query": {
"bool": {
"filter": {
"range": {
"timestamp": {
"gte": "now/d",
"lte": "now"
}
}
}
}
}
}
3.12 根據(jù)自定義時(shí)間段查詢(range + filter + datetime)
GET index_name/?search
{
"query": {
"bool": {
"filter": {
"range": {
"timestamp": {
"gte": "2023-12-01 00:00:00",
"lt": "2023-12-02 23:59:59",
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
}
}
3.13 滿足條件查詢 (must)
GET index_name/?search
{
"query": {
"bool": {
"filter": {
"range": {
"timestamp": {
"gte": "2023-12-01 00:00:00",
"lt": "2023-12-02 23:59:59",
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
},
"must": [
{
"wildcard": {
"command": "*"
}
},
{
"wildcard": {
"ip": "192.*"
}
}
]
}
},
"size": 1
}
3.14 排除條件查詢(must_not)
GET index_name/?search
{
"query": {
"bool": {
"filter": {
"range": {
"timestamp": {
"gte": "2023-12-01 00:00:00",
"lt": "2023-12-02 23:59:59",
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
},
"must_not": [
{
"wildcard": {
"ip": "127.*"
}
},
{
"wildcard": {
"ip": "localhost*"
}
}
]
}
},
"size": 1
}
3.15 包含key查詢(must + exists)
GET index_name/?search
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"user": "Yi*"
}
},
{
"exists": {
"field": "address"
}
}
]
}
},
"size": 1
}
3.16 不包含key查詢(must_not + exists)
GET index_name/?search
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"user": "Yi*"
}
}
],
"not_must": [
{
"exists": {
"field": "address"
}
}
]
}
},
"size": 1
}
3.17 排序查詢(sort)
3.17.1 降序
GET index_name/?search
{
"query": {
"wildcard": {
"ip": "192.168.*"
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
]
}
3.17.2 升序
GET index_name/?search
{
"query": {
"wildcard": {
"ip": "192.168.*"
}
},
"sort": [
{
"timestamp": {
"order": "asc"
}
}
]
}
3.18 指定返回字段查詢(_source)
GET index_name/?search
{
"query": {
"wildcard": {
"ip": "192.168.*"
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"_source": [
"user",
"ip",
"phone",
"address"
]
}
3.19 指定條數(shù)查詢(size)
最多單次查詢10000條,超過10000條需要分頁查詢。
GET index_name/?search
{
"query": {
"wildcard": {
"ip": "192.168.*"
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"_source": [
"user",
"ip",
"phone",
"address"
],
"size": 10000
}
3.20 分頁查詢(from)
GET index_name/?search
{
"query": {
"wildcard": {
"ip": "192.168.*"
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"_source": [
"user",
"ip",
"phone",
"address"
],
"from": 1,
"size": 100
}
3.21 關(guān)聯(lián)多個(gè)index查詢
GET index_name_one,index_name_two/?search
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"user": "Yi*"
}
}
],
"must_not": [
{
"wildcard": {
"ip": "127.*"
}
},
{
"wildcard": {
"ip": "localhost*"
}
}
]
}
},
"size": 1
}
GET index_name_*/?search
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"user": "Yi*"
}
}
],
"must_not": [
{
"wildcard": {
"ip": "127.*"
}
},
{
"wildcard": {
"ip": "localhost*"
}
}
]
}
},
"size": 1
}
3.22 布爾查詢(bool)
?? bool語句是常用的用于構(gòu)建復(fù)雜查詢邏輯的語句。bool語句可以通過組合多個(gè)條件子句來實(shí)現(xiàn)邏輯運(yùn)算,包括must、must_not、should和filter。
3.22.1 bool+must查詢
GET index_name/?search
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"user": "Yi*"
}
}
],
"must_not": [
{
"wildcard": {
"ip": "127.*"
}
},
{
"wildcard": {
"ip": "localhost*"
}
}
]
}
},
"size": 1
}
3.22.2 bool+must_not查詢
GET index_name/?search
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"user": "Yi*"
}
}
],
"must_not": [
{
"wildcard": {
"ip": "127.*"
}
},
{
"wildcard": {
"ip": "localhost*"
}
}
]
}
},
"size": 1
}
3.22.3 bool+should查詢
GET index_name/?search
{
"track_total_hits": true,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"wildcard": {
"host": "www.baidu.com"
}
},
{
"wildcard": {
"host": "www.qq.com"
}
}
]
}
},
{
"bool": {
"should": [
{
"wildcard": {
"user": "*龍*"
}
},
{
"wildcard": {
"user": "*虎*"
}
}
]
}
}
]
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"size": 1
}
3.22.4 bool+filter查詢
GET index_name/?search
{
"track_total_hits": true,
"query": {
"bool": {
"filter": {
"range": {
"timestamp": {
"gte": "2023-11-27 00:00:00",
"lt": "2023-12-03 23:59:59",
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"size": 0
}
3.22.5 bool多種組合(bool+must+must_not+should+filter)
GET index_name/?search
{
"track_total_hits": true,
"query": {
"bool": {
"filter": {
"range": {
"timestamp": {
"gte": "2023-11-27 00:00:00",
"lt": "2023-12-03 23:59:59",
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
},
"must": [
{
"bool": {
"should": [
{
"wildcard": {
"host": "www.baidu.com"
}
},
{
"wildcard": {
"host": "www.qq.com"
}
}
]
}
},
{
"bool": {
"should": [
{
"wildcard": {
"user": "*龍*"
}
},
{
"wildcard": {
"user": "*虎*"
}
}
]
}
}
],
"must_not": [
{
"wildcard": {
"address": {
"value": "*廣東*"
}
}
}
]
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"size": 0
}
3.23 match、term、terms、should、must_not、bool、filter區(qū)別
match查詢是全文搜索查詢,根據(jù)相關(guān)性匹配查詢字符串,進(jìn)行分詞處理。
term查詢是精確匹配查詢,直接與文檔中的詞項(xiàng)進(jìn)行比較,不進(jìn)行分詞處理。
terms查詢是多值匹配查詢,匹配多個(gè)值中的任意一個(gè)。
should查詢表示滿足任意一個(gè)條件即可匹配成功,用于構(gòu)建OR邏輯關(guān)系。
must_not查詢表示條件不能滿足才能匹配成功,用于排除特定條件。
bool查詢是復(fù)合查詢,通過組合多個(gè)查詢子句構(gòu)建復(fù)雜的查詢邏輯,包括must、should和must_not。
filter查詢用于過濾文檔,不計(jì)算相關(guān)性分?jǐn)?shù),僅根據(jù)條件進(jìn)行精確匹配,提高查詢性能。
四、DSL常用統(tǒng)計(jì)語句
4.1 aggs terms
GET index_name/?search
{
"track_total_hits": true,
"query": {
"match_all": {}
},
"aggs": {
"NAME": {
"terms": {
"field": "user",
"size": 10
}
}
}
}
4.2 aggs cardinality
GET index_name/?search
{
"track_total_hits": true,
"query": {
"match_all": {}
},
"aggs": {
"NAME": {
"cardinality": {
"field": "user"
}
}
}
}
4.3 aggs value_count
GET index_name/?search
{
"track_total_hits": true,
"query": {
"match_all": {}
},
"aggs": {
"NAME": {
"value_count": {
"field": "user"
}
}
}
}
4.4 aggs terms+script
GET index_name/?search
{
"track_total_hits": true,
"query": {
"match_all": {}
},
"aggs": {
"NAME": {
"terms": {
"script": "doc['user'] +'####'+ doc['ip']",
"size": 1000
}
}
}
}
4.5 aggs多重組合
GET index_name/?search
{
"track_total_hits": true,
"query": {
"match_all": {}
},
"aggs": {
"NAME": {
"terms": {
"field": "user",
"size": 10
},
"aggs": {
"NAME": {
"terms": {
"script": "doc['ip'] +'####'+ doc['address']",
"size": 1000
}
}
}
}
}
}
4.6 aggs語句參數(shù)解析
?? 這些聚合語句可以根據(jù)具體的需求進(jìn)行組合和嵌套,以實(shí)現(xiàn)更復(fù)雜的統(tǒng)計(jì)和分析操作。通過使用這些聚合語句,可以從查詢結(jié)果中提取有用的統(tǒng)計(jì)信息,進(jìn)行數(shù)據(jù)分析、可視化和業(yè)務(wù)洞察。
terms聚合:按字段進(jìn)行分組,并統(tǒng)計(jì)每個(gè)分組的文檔數(shù)量。
date_histogram聚合:按時(shí)間間隔對日期字段進(jìn)行分組,并統(tǒng)計(jì)每個(gè)時(shí)間間隔內(nèi)的文檔數(shù)量。
range聚合:將字段的值劃分為不同的范圍,并統(tǒng)計(jì)每個(gè)范圍內(nèi)的文檔數(shù)量。
histogram聚合:將數(shù)值字段的值劃分為不同的區(qū)間,并統(tǒng)計(jì)每個(gè)區(qū)間內(nèi)的文檔數(shù)量。
avg聚合:計(jì)算數(shù)值字段的平均值。
sum聚合:計(jì)算數(shù)值字段的總和。
min聚合:找到數(shù)值字段的最小值。
max聚合:找到數(shù)值字段的最大值。
cardinality聚合:計(jì)算字段的基數(shù)(不重復(fù)值的數(shù)量)。
top_hits聚合:返回每個(gè)分組中的頂部文檔。
extended_stats聚合:計(jì)算數(shù)值字段的統(tǒng)計(jì)信息,包括平均值、標(biāo)準(zhǔn)差、最小值、最大值等。
percentiles聚合:計(jì)算數(shù)值字段的百分位數(shù)。
geo_distance聚合:按地理距離對地理坐標(biāo)字段進(jìn)行分組,并統(tǒng)計(jì)每個(gè)距離范圍內(nèi)的文檔數(shù)量。
filter聚合:根據(jù)指定的過濾條件對文檔進(jìn)行聚合。
nested聚合:在嵌套的文檔結(jié)構(gòu)中進(jìn)行聚合操作。
value_count聚合:計(jì)算某個(gè)字段的值的數(shù)量。
stats聚合:計(jì)算數(shù)值字段的統(tǒng)計(jì)信息,包括平均值、總和、最小值、最大值和文檔數(shù)量。
scripted_metric聚合:使用自定義腳本計(jì)算聚合結(jié)果。
五、所遇到問題的解決方案
5.1 解決統(tǒng)計(jì)顯示所有數(shù)量而不是10000問題
添加"track_total_hits": true
GET index_name/?search
{
"track_total_hits": true,
"query": {
"match_all": {
}
}
}
5.2 解決must與should同時(shí)使用must不生效問題
將should整個(gè)包裝成must一個(gè)條件就能解決文章來源:http://www.zghlxwxcb.cn/news/detail-781515.html
GET index_name/?search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"wildcard": {
"user": "*Yi*"
}
},
{
"wildcard": {
"user": "*龍*"
}
},
{
"wildcard": {
"user": "*虎*"
}
}
]
}
}
]
}
},
"size": 10000
}
六、總結(jié)
?? 無論您是數(shù)據(jù)分析師、開發(fā)人員還是與Elasticsearch相關(guān)的崗位,了解和掌握ES DSL查詢語句都是非常重要的,掌握這些強(qiáng)大的查詢工具,為您的工作帶來更多的效率和成果。微信公眾號搜索關(guān)注藝說IT,分享各種原創(chuàng)技術(shù)干貨文章,對你有用的話請一鍵三連,感謝??文章來源地址http://www.zghlxwxcb.cn/news/detail-781515.html
到了這里,關(guān)于工作常用ES DSL查詢語句(干貨滿滿)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!