I have a documents with fields campaign_id
(not unique), clicks
(same for each campaign_id
).
How to count sum of unique campaign_id
s 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.
答案 0 :(得分:2)
最简单的方法是在ES 2.0中使用pipeline aggregations,首先使用max
聚合来获得单值(因为您说的是{{1} }}具有相同的campaign_id
值,然后是clicks
管道聚合以获得最终总和:
sum_bucket