2

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.

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
Igor
  • 149
  • 2
  • 9

1 Answers1

2

The simplest approach to this is with pipeline aggregations in ES 2.0, using a max aggregation first to get a single value (since you said that the same campaign_id has the same clicks value) and then a sum_bucket pipeline aggregation to get the final sum:

{
  "size": 0,
  "aggs": {
    "unique_ids": {
      "terms": {
        "field": "campaign_id",
        "size": 10
      },
      "aggs": {
        "one_value": {
          "max": {
            "field": "clicks"
          }
        }
      }
    },
    "sum_uniques": {
      "sum_bucket": {
        "buckets_path": "unique_ids>one_value"
      }
    }
  }
}
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • thanks for your answer. yes, its possible in version 2.0, but we are still using 1.7. – Igor Nov 12 '15 at 17:05
  • I don't think it's possible in 1.7. Pipeline aggregations is one of the main features in 2.0 and users for a long time wanted something like this. – Andrei Stefan Nov 13 '15 at 20:17