一 序
本文屬於極客時間Elasticsearch核心技術與實戰學習筆記系列。
二 聚合的精準度問題
2.1分佈式系統的近似統計算法
2.2 Min 聚合分析的執行流程
2.3 Terms Aggregation 的返回值
在 Terms Aggregation 的返回中有兩個特殊的數值
- doc_count_error_upper_bound:被遺漏的 term 分桶,包含的文檔,有可能的最大值
- sum_other_doc_count: 處理返回結果 bucket 的 terms 以外,其他 terms 的文檔總數(總數 - 返回的總數)
2.4 Terms 聚合分析的執行流程
這種情況下返回的結果不一定能準確:
Terms 不正確的案例
這裏返回的錯誤數據是:A:12,B:6,C:4,但實際上D是3+3=6.
上面的總數=29,返回的12+6+4=22,29-22=7
2.5如何解決 Terms 不準的問題:提升 shard_size 的參數
Terms 聚合分析不準的原因,數據分散在多個分片上,Coordinating Node 無法獲取數據全貌
- 解決方案 1:當數據量不大時,設置 Primary Shard 爲 1;實現準確性(分片導致的)
- 解決方案 2:在分佈式數據上,設置 shard_size 參數,提高精確度
原理:每次從 Shard 上額外多獲取數據,提升準確率(上面的錯誤的例子抓取3個丟失數據)
打開 show_term_doc_count_error
shard_size 設定
調整 shard size 大小,降低 doc_count_error_upper_bound 來提升準確度
- 增加整體計算量,提高了準確率,但會降低相應時間
Shard Size 默認大小設定
- shard size = size * 1.5 +10
demo
數據準備:依賴測試數據
DELETE my_flights
PUT my_flights
{
"settings": {
"number_of_shards": 20
},
"mappings" : {
"properties" : {
"AvgTicketPrice" : {
"type" : "float"
},
"Cancelled" : {
"type" : "boolean"
},
"Carrier" : {
"type" : "keyword"
},
"Dest" : {
"type" : "keyword"
},
"DestAirportID" : {
"type" : "keyword"
},
"DestCityName" : {
"type" : "keyword"
},
"DestCountry" : {
"type" : "keyword"
},
"DestLocation" : {
"type" : "geo_point"
},
"DestRegion" : {
"type" : "keyword"
},
"DestWeather" : {
"type" : "keyword"
},
"DistanceKilometers" : {
"type" : "float"
},
"DistanceMiles" : {
"type" : "float"
},
"FlightDelay" : {
"type" : "boolean"
},
"FlightDelayMin" : {
"type" : "integer"
},
"FlightDelayType" : {
"type" : "keyword"
},
"FlightNum" : {
"type" : "keyword"
},
"FlightTimeHour" : {
"type" : "keyword"
},
"FlightTimeMin" : {
"type" : "float"
},
"Origin" : {
"type" : "keyword"
},
"OriginAirportID" : {
"type" : "keyword"
},
"OriginCityName" : {
"type" : "keyword"
},
"OriginCountry" : {
"type" : "keyword"
},
"OriginLocation" : {
"type" : "geo_point"
},
"OriginRegion" : {
"type" : "keyword"
},
"OriginWeather" : {
"type" : "keyword"
},
"dayOfWeek" : {
"type" : "integer"
},
"timestamp" : {
"type" : "date"
}
}
}
}
POST _reindex
{
"source": {
"index": "kibana_sample_data_flights"
},
"dest": {
"index": "my_flights"
}
}
因爲es7默認的主分片是1.所以doc_count_error_upper_bound是0
改變shard_size爲5,錯誤數就下降了,改爲10就爲0了。
*******
注意:size是最終返回多少個buckt的數量。
shard_size是每個bucket在一個shard上取回的bucket的總數。然後,每個shard上的結果,會在coordinate節點上在做一次彙總,返回總數。