[Elasticsearch] ES聚合場景下部分結果數據未返回問題分析
- 2021 年 12 月 28 日
- 筆記
- [Elasticsearch], ES, 聚合, 聚合檢索
背景
在對ES某個篩選欄位聚合查詢,類似groupBy操作後,發現該欄位新增的數據,聚合結果沒有展示出來,但是用戶在全文檢索新增的篩選數據後,又可以查詢出來, 針對該問題進行了相關排查。
排查思路
首先要明確我們數據的寫入流程, 下圖:
在檢查Mysql庫的數據沒有問題之後,開始檢查ES是否有問題,根據現象我們知道既然在全文檢索中都能搜索到,說明數據肯定是寫入ES里了,但是又如何確定聚合結果呢?
首先添加日誌將程式碼最終生成DSL語句列印出來
LOGGER.info("\n{}", searchRequestBuilder);
這樣就很方便地使用curl命令進行調試了
下面是對生成的DSL語句執行查詢:
curl -XGET '//ip:9200/es_data_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query":{
"bool":{
"must":[
{
"term":{
"companyId":{
"value":1,
"boost":1
}
}
},
{
"term":{
"yn":{
"value":1,
"boost":1
}
}
},
{
"match_all":{
"boost":1
}
}
],
"must_not":[
{
"term":{
"table_sentinel":{
"value":2,
"boost":1
}
}
}
],
"disable_coord":false,
"adjust_pure_negative":true,
"boost":1
}
},
"aggregations":{
"group_by_topics":{
"terms":{
"field":"topic",
"size":10,
"min_doc_count":1,
"shard_min_doc_count":0,
"show_term_doc_count_error":false,
"order":[
{
"_count":"desc"
},
{
"_term":"asc"
}
]
}
}
}
}'
上圖group_by_topics 就是我們要聚合的欄位, 下面是執行該DSL語句的結果:
"aggregations" : {
"group_by_topics" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 14,
"buckets" : [
{
"key" : 1,
"doc_count" : 35
},
{
"key" : 19,
"doc_count" : 25
},
{
"key" : 18,
"doc_count" : 17
},
{
"key" : 29,
"doc_count" : 15
},
{
"key" : 20,
"doc_count" : 12
},
{
"key" : 41,
"doc_count" : 8
},
{
"key" : 161,
"doc_count" : 5
},
{
"key" : 2,
"doc_count" : 3
},
{
"key" : 3,
"doc_count" : 2
},
{
"key" : 21,
"doc_count" : 2
}
]
}
}
經過觀察發現聚合結果確實沒有我們新增的篩選項, 同時返回的數據只有10條
"sum_other_doc_count" : 14,
這項是關鍵項,從字面意思看還有有其他的文檔,於是查詢具體在ES中的意義是什麼?
經過查詢發現有段描述:
就是只會返回top結果, 部分結果不響應返回
那如何讓這部分結果返回呢?
帶著問題, 發現使用桶聚合,默認會根據doc_count 降序排序,同時默認只返回10條聚合結果.
可以通過在聚合查詢增大屬性size來解決,如下
curl -XGET '//ip:9200/es_data_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query":{
"bool":{
"must":[
{
"term":{
"companyId":{
"value":1,
"boost":1
}
}
},
{
"term":{
"yn":{
"value":1,
"boost":1
}
}
},
{
"match_all":{
"boost":1
}
}
],
"must_not":[
{
"term":{
"table_sentinel":{
"value":2,
"boost":1
}
}
}
],
"disable_coord":false,
"adjust_pure_negative":true,
"boost":1
}
},
"aggregations":{
"group_by_topics":{
"terms":{
"field":"topic",
"size":100,
"min_doc_count":1,
"shard_min_doc_count":0,
"show_term_doc_count_error":false,
"order":[
{
"_count":"desc"
},
{
"_term":"asc"
}
]
}
}
}
}'
下面是查詢結果:
"aggregations" : {
"group_by_topics" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"doc_count" : 35
},
{
"key" : 19,
"doc_count" : 25
},
{
"key" : 18,
"doc_count" : 17
},
{
"key" : 29,
"doc_count" : 15
},
{
"key" : 20,
"doc_count" : 12
},
{
"key" : 41,
"doc_count" : 8
},
{
"key" : 161,
"doc_count" : 5
},
{
"key" : 2,
"doc_count" : 3
},
{
"key" : 3,
"doc_count" : 2
},
{
"key" : 21,
"doc_count" : 2
},
{
"key" : 81,
"doc_count" : 2
},
{
"key" : 801,
"doc_count" : 2
},
{
"key" : 0,
"doc_count" : 1
},
{
"key" : 4,
"doc_count" : 1
},
{
"key" : 5,
"doc_count" : 1
},
{
"key" : 6,
"doc_count" : 1
},
{
"key" : 7,
"doc_count" : 1
},
{
"key" : 11,
"doc_count" : 1
},
{
"key" : 23,
"doc_count" : 1
},
{
"key" : 28,
"doc_count" : 1
},
{
"key" : 201,
"doc_count" : 1
},
{
"key" : 241,
"doc_count" : 1
}
]
}
把ES所有的篩選項數據都統計返回來.
程式碼里設置size:
TermsAggregationBuilder termAgg1 = AggregationBuilders.terms("group_by_topics")
.field("topic").size(100);
我們解決了問題, 現在思考下ES為什麼不一下子返回所有統計項的結果數據呢?
答案是由ES聚合機制決定, ES怎麼聚合呢
接受客戶端的節點是協調節點
協調節點上,搜索任務會被分解成兩個階段: query和fetch
真正搜索或聚合任務的節點為數據節點,如圖 2, 3, 4
聚合步驟:
- 客戶端發請求到協調節點
- 協調節點將請求推送到各數據節點
- 各數據節點指定分片參與數據彙集工作
- 協調節點進行總結果匯聚
es 出於效率和性能原因等,聚合的結果其實是不精確的.什麼意思? 以我們上面遇到的場景為例:
默認返回top 10 聚合結果, 首先在各節點分片取自己的topic 10 返回給協調節點,然後協調節點進行匯總. 這樣就會導致全量的實際聚合結果跟預期的不一致.
雖然有很多辦法提高ES聚合精準度,但是如果對於大數據量的精準聚合,響應速度要快場景,es並不擅長,需要使用類似clickhouse這樣的產品來解決這樣的場景.
總結
本文主要針對實際工作的應用問題,來排查解決ES聚合數據部分數據未展示問題, 同時對ES的聚合檢索原理進行講解 .在數據量大、聚合精度要求高、響應速度快的業務場景ES並不擅長.
參考
//discuss.elastic.co/t/what-does-sum-other-doc-count-mean-exactly/159687