一 序
本文属于极客时间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节点上在做一次汇总,返回总数。