概述
继续跟中华石杉老师学习ES,第46篇
课程地址: https://www.roncoo.com/view/55
官方说明
Cardinality Aggregation:戳这里
es中的去重,cartinality metric,对每个bucket中的指定的field进行去重,取去重后的count,类似于count(distcint)
示例
GET /tvs/sales/_search { "aggs": { "month":{ "date_histogram": { "field": "sold_date", "interval": "month" }, "aggs": { "distinct_brand_cnt": { "cardinality": { "field": "brand" } } } } }, "size": 0 }
返回:
{ "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "month": { "buckets": [ { "key_as_string": "2016-05-01T00:00:00.000Z", "key": 1462060800000, "doc_count": 1, "distinct_brand_cnt": { "value": 1 } }, { "key_as_string": "2016-06-01T00:00:00.000Z", "key": 1464739200000, "doc_count": 0, "distinct_brand_cnt": { "value": 0 } }, { "key_as_string": "2016-07-01T00:00:00.000Z", "key": 1467331200000, "doc_count": 1, "distinct_brand_cnt": { "value": 1 } }, { "key_as_string": "2016-08-01T00:00:00.000Z", "key": 1470009600000, "doc_count": 1, "distinct_brand_cnt": { "value": 1 } }, { "key_as_string": "2016-09-01T00:00:00.000Z", "key": 1472688000000, "doc_count": 0, "distinct_brand_cnt": { "value": 0 } }, { "key_as_string": "2016-10-01T00:00:00.000Z", "key": 1475280000000, "doc_count": 1, "distinct_brand_cnt": { "value": 1 } }, { "key_as_string": "2016-11-01T00:00:00.000Z", "key": 1477958400000, "doc_count": 2, "distinct_brand_cnt": { "value": 1 } }, { "key_as_string": "2016-12-01T00:00:00.000Z", "key": 1480550400000, "doc_count": 0, "distinct_brand_cnt": { "value": 0 } }, { "key_as_string": "2017-01-01T00:00:00.000Z", "key": 1483228800000, "doc_count": 1, "distinct_brand_cnt": { "value": 1 } }, { "key_as_string": "2017-02-01T00:00:00.000Z", "key": 1485907200000, "doc_count": 1, "distinct_brand_cnt": { "value": 1 } } ] } } }
我们来看下原始数据:
来看下我们的统计结果,找个 2016-11
统计正确。