目錄
1. ElasticSearch之查詢返回結(jié)果各字段含義
2. match 查詢
3. term查詢
4. terms 查詢
5. range 范圍
6. 布爾查詢
6.1 filter加快查詢效率的原因
7. boosting query(提高查詢)
8. dis_max(最佳匹配查詢)
9. 分頁
10. 聚合查詢【內(nèi)含實際的demo】
1. ElasticSearch之查詢返回結(jié)果各字段含義
執(zhí)行命令:索引庫名稱/_search
空搜索的結(jié)果為:
{
"took": 2, # 該命令請求花費了多長時間,單位:毫秒。
"timed_out": false, # 搜索是否超時
"_shards": { # 搜索分片信息
"total": 3, # 搜索分片總數(shù)
"successful": 3, # 搜索成功的分片數(shù)量
"skipped": 0, # 沒有搜索的分片,跳過的分片
"failed": 0 # 搜索失敗的分片數(shù)量
},
"hits": { # 搜索結(jié)果集。需要的一切數(shù)據(jù)都是從hits中獲取
"total": 21798, # 返回多少條數(shù)據(jù)
"max_score": 1, #返回結(jié)果中,最大的匹配度分值
"hits": [ # 默認查詢前十條數(shù)據(jù),根據(jù)分值降序排序,這里為了節(jié)省地方,把默認查詢的前十條數(shù)據(jù)刪了9條,只剩下一條數(shù)據(jù)
{
"_index": "", # 索引庫名稱
"_type": "", # 類型名稱
"_id": "", # 該條數(shù)據(jù)的id
"_score": 1, # 關(guān)鍵字與該條數(shù)據(jù)的匹配度分值
"_routing": "", # routing參數(shù)是一個可選參數(shù),默認使用文檔的_id值,用于計算文檔所屬分片
"_source": { # 索引庫中類型,返回結(jié)果字段,不指定的話,默認全部顯示出來
"id": 1,
"orderNo": "",
"appId": "",
"componentAppId": "",
"settleNo": "",
"outSettleNo": "",
"settleAmount": 5,
"orderAmount": 7,
"settleStatus": 3,
"paymentChannel": 1,
"version": 2,
"settleTime": ,
"createTime": ,
"updateTime": ,
"promotionAccountId": "",
"invoiceStatus": 1,
"promotionTypeValue": 0,
"commissionRateFeeCentAmount": 0,
"commissionChargeFeeCentAmount": 0,
"promotionFeeCentAmount": 2,
"developerPromotionFeeCentAmount": 0,
"promotionType": ""
}
}
]
}
}
- 按照從上到下的順序,一共四個返回值,took,timed_out,_shards,hits。
- took
- 該命令請求花費了多長時間,單位:毫秒。
- timed_out
- 搜索是否超時。
- shards
- 搜索分片信息。
- total
- 搜索分片總數(shù)。
- successful
- 搜索成功的分片數(shù)量。
- skipped
- 沒有搜索的分片,跳過的分片。
- failed
- 搜索失敗的分片數(shù)量。
- hits
- 搜索結(jié)果集,需要的一切數(shù)據(jù)都是從hits中獲取。
- total
- 返回多少條數(shù)據(jù)。
- max_score
- 返回結(jié)果中,最大的匹配度分值。
- hits
- 默認查詢前十條數(shù)據(jù),根據(jù)分值降序排序。
- _index
- 索引庫名稱。
- _type
- 類型名稱。
- _id
- 該條數(shù)據(jù)的id。
- _score
- 關(guān)鍵字與該條數(shù)據(jù)的匹配度分值。
- _source
- 索引庫中類型,返回結(jié)果字段,不指定的話,默認全部顯示出來。
2. match 查詢
-
匹配查詢 match 是個 核心 查詢。無論需要查詢什么字段, match 查詢都應該會是首選的查詢方式。它是一個高級 全文查詢 ,這表示它既能處理全文字段(包括支持分詞的字段),又能處理精確字段
-
match 查詢主要的應用場景就是進行全文搜索
{
"query": {
"match": {
"appId": "xxxx"
}
}
}
match本質(zhì)上是對term組合,所以上面的語句換成term依然能夠執(zhí)行
{
"query": {
"term": {
"appId": "xxxx"
}
}
}
3. term查詢
-
term 查詢, 可以用它處理數(shù)字(numbers)、布爾值(Booleans)、日期(dates)以及文本(text)
-
用 trem 搜索字符串時 要將字段設置成 not_analyzed 無需分析的。不然es會將字符串進行分詞,分詞結(jié)果建立索引,在用trem進行精確查找時找不到任何文檔
-
對應的 QueryBuilder class 是TermQueryBuilder
-
具體方法是 QueryBuilders.termQuery()
{
"query": {
"term": {
"appId": "xxxx"
}
}
}
4. terms 查詢
-
terms 查詢允許指定多個值進行匹配。如果這個字段包含了指定值中的任何一個值,就表示該文檔滿足條件。 比如我們想要查找價格字段值為 $20 或 $30 的文檔則可以使用trems;
-
按照讀個分詞term匹配,它們是or的關(guān)系
-
對應的 QueryBuilder class 是 TermsQueryBuilder
- 具體方法是 QueryBuilders.termsQuery()
{
"query": {
"terms": {
"appId": ["xxxx", "xxxx"]
}
}
}
5. range 范圍
-
常常被用在數(shù)字或者日期范圍的查詢
Search Query |
QueryBuilder Class |
Method in QueryBuilders |
Range |
RangeQueryBuilder |
QueryBuilders.rangeQuery() |
{
"query": {
"range": {
"createTime": {
"gte": 1661409996661,
"lte": 1661409996661
}
}
}
}
6. 布爾查詢
- 通過布爾邏輯將較小的查詢組合成較大的查詢。
- 概念
- Bool查詢語法有以下特點
- 子查詢可以任意順序出現(xiàn)
- 可以嵌套多個查詢,包括bool查詢
- 如果bool查詢中沒有must條件,should中必須至少滿足一條才會返回結(jié)果。
- bool查詢包含四種操作符,分別是must,should,must_not,filter。他們均是一種數(shù)組,數(shù)組里面是對應的判斷條件。
- must: 必須匹配。貢獻算分
- must_not:過濾子句,必須不能匹配,但不貢獻算分
- should: 選擇性匹配,至少滿足一條。貢獻算分
- filter: 過濾子句,必須匹配,但不貢獻算分,所以比must會更快!
- Bool查詢語法有以下特點
{
"query": {
"bool": {
"must": [
{
"term": {
"appId": "xxxx"
}
},
{
"term": {
"paymentChannel": 1
}
},
{
"term": {
"settleStatus": 3
}
},
{
"term": {
"promotionAccountId": ""
}
},
{
"range": {
"createTime": {
"from": 1658741630780,
"to": 1661420030780,
"include_lower": true,
"include_upper": true
}
}
}
]
}
}
}
6.1 filter加快查詢效率的原因
-
query context
-
query context關(guān)注的是,文檔到底有多匹配查詢的條件,這個匹配的程度是由相關(guān)性分數(shù)決定的,分數(shù)越高自然就越匹配。所以這種查詢除了關(guān)注文檔是否滿足查詢條件,還需要額外的計算相關(guān)性分數(shù).
-
-
filter context
-
filter context關(guān)注的是,文檔是否匹配查詢條件,結(jié)果只有兩個,是和否。沒有其它額外的計算。它常用的一個場景就是過濾時間范圍。
-
并且filter context會自動被ES緩存結(jié)果,效率進一步提高。
-
對于bool查詢,must使用的就是query context,而filter使用的就是filter context。
-
我們可以通過一個示例驗證下。繼續(xù)使用第一節(jié)的例子,我們通過kibana自帶的search profiler來看看ES的查詢的詳細過程。
-
-
那么 filter 的 cache 是怎么做的呢?
-
ES 會構(gòu)建一個文檔匹配過濾器的位集 bitset(用來標識一個文檔對一個 filter 條件是否匹配,如果匹配就是 1,不匹配就是 0),下次再有這個 filter 條件過來的時候就不用重新掃描倒排索引,反復生成 bitset,可以大幅度提升性能,另外當添加或更新文檔時,這個 filter 的位集 bitset 也會更新。
-
{
"query": {
"bool": {
"must": [
{
"term": {
"appId": "xxxx"
}
},
{
"term": {
"paymentChannel": 1
}
},
{
"term": {
"settleStatus": 3
}
},
{
"term": {
"promotionAccountId": ""
}
}
],
"filter": {
"range": {
"createTime": {
"from": 1658741630780,
"to": 1661420030780,
"include_lower": true,
"include_upper": true
}
}
}
}
}
}
7. boosting query(提高查詢)
-
用來控制(提高或降低)復合查詢中子查詢的權(quán)重。
-
不同于bool查詢,bool查詢中只要一個子查詢條件不匹配那么搜索的數(shù)據(jù)就不會出現(xiàn)。而boosting query則是降低顯示的權(quán)重/優(yōu)先級(即score)。
-
比如搜索邏輯是 name = 'apple' and type ='fruit',對于只滿足部分條件的數(shù)據(jù),不是不顯示,而是降低顯示的優(yōu)先級(即score)
-
~positive(積極的,加分):
-
只有匹配上positive的查詢的內(nèi)容,才會被放到返回的結(jié)果集中。
-
-
~negative(消極的,減分):
-
如果匹配上positive并且也匹配上了negative,就可以降低這樣的文檔score。
-
-
~negative_boost:
-
指定系數(shù),必須小于1.0 ,那么匹配到的內(nèi)容會將分數(shù)乘以當前系數(shù);(這是個系數(shù),因為你要控制分數(shù),那要怎么控制呢?就是乘以系數(shù)來控制分數(shù)大小)
-
{
"query": {
"boosting": {
"positive": {
"term": {
"appId": "xxxx"
}
},
"negative": {
"term": {
"orderNo": "xxxx"
}
},
"negative_boost": 0.5
}
}
}
8. dis_max(最佳匹配查詢)
-
dis_max query
-
叫做分離最大化查詢,它會將任何與查詢匹配的文檔都作為結(jié)果返回,但是只是將其中最佳匹配的評分作為最終的評分返回。
-
-
dis_max 條件的計算分數(shù)
-
分數(shù) = 第一個匹配條件分數(shù) + tie_breaker * 第二個匹配的條件的分數(shù) ...
-
"query": {
"dis_max": {
"queries": [
{
"term": {
"appId": "xxxx"
}
},
{
"term": {
"paymentChannel": 1
}
},
{
"range": {
"createTime": {
"from": 1658741630780,
"to": 1661420030780,
"include_lower": true,
"include_upper": true
}
}
}
],
"tie_breaker": 0
}
}
9. 分頁
-
通過 from 和 size 就可以執(zhí)行分頁查詢。from 指明了分頁查詢返回的結(jié)果的起始位置,而size參數(shù)則指明了分頁查詢的頁容量。文章來源:http://www.zghlxwxcb.cn/news/detail-407489.html
{
"from": 0,
"size": 1,
"query": {
"bool": {
"must": [
{
"term": {
"appId": "xxxx"
}
},
{
"term": {
"paymentChannel": 1
}
},
{
"term": {
"settleStatus": 3
}
},
{
"term": {
"promotionAccountId": ""
}
},
{
"range": {
"createTime": {
"from": 1658741630780,
"to": 1661420030780,
"include_lower": true,
"include_upper": true
}
}
}
]
}
}
}
10. 聚合查詢【內(nèi)含實際的demo】
根據(jù)appId查詢昨日結(jié)算成功的指定支付渠道的結(jié)算總金額文章來源地址http://www.zghlxwxcb.cn/news/detail-407489.html
{
"query": {
"bool": {
"must": [
{
"term": {
"appId": "xxxx"
}
},
{
"term": {
"paymentChannel": 1
}
},
{
"term": {
"settleStatus": 3
}
},
{
"term": {
"promotionAccountId": ""
}
},
{
"range": {
"createTime": {
"from": 1658741630780,
"to": 1661420030780,
"include_lower": true,
"include_upper": true
}
}
}
]
}
},
"aggs": {
"total_amount": {
"sum": {
"field": "settleAmount"
}
}
},
"size": 0
}
到了這里,關(guān)于ES查詢常用語法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!