How to get distinct documents in elasticsearch

时间:2015-11-12 11:09:43

标签: elasticsearch unique distinct aggregation cardinality

I have a documents with fields campaign_id (not unique), clicks (same for each campaign_id). How to count sum of unique campaign_ids clicks. e.g.

campaign_id=1, clicks=2;
campaign_id=2, clicks=3;
campaign_id=1, clicks=2;

Sum of unique campaign_id's clicks will be 5.

Main idea is to get documents with unique campaign_ids and then sum.
How to create such query in Elasticsearch?

Tried to achieve this using filters inside aggregation but filter should be a bucket instead of metrics.

1 个答案:

答案 0 :(得分:2)

最简单的方法是在ES 2.0中使用pipeline aggregations,首先使用max聚合来获得单值(因为您说的是{{1} }}具有相同的campaign_id值,然后是clicks管道聚合以获得最终总和:

sum_bucket