ElasticSearch基数问题
问题描述:
基数聚合计算不同值的近似计数。但是为什么即使是存储在单个分片中的索引,它显示的值也不正确?ElasticSearch基数问题
GET /jobs/_settings { "jobs": { "settings": { "index": { "number_of_shards": "1", ... position_id is long GET /jobs/_search { "size": 0, "aggs": { "count_position_id": { "value_count": { "field": "position_id" } }, "unique_position_id": { "cardinality": { "field": "position_id", "precision_threshold": 40000 } } } } { "took": 44, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 52836, "max_score": 0, "hits": [] }, "aggregations": { "unique_position_id": { "value": 52930 }, "count_position_id": { "value": 52836 } } }
答
它更多的是用来计算基数的算法,而不是单张碎片的图片。
ES基数AGG工程,并使用HLL(hyperloglog),这是近似计数算法(它依赖于观察散列的二进制表示近似唯一值计算)
您可以通过定义这种日益增长的precision_threshold。所以控制精度是“近似计数” - 并非真的不正确。