Aggregations using distinct query

Hi, I am using ES to build term aggregations and was trying to see if there is some way of doing a "get distinct" based on a field as the first step of the aggregation. Here is what I am trying to do :

Records
{"my_id":"a", "someField":"X"}
{"my_id":"a", "someField":"X"}
{"my_id":"b", "someField":"Y"}
{"my_id":"c", "someField":"Z"}
{"my_id":"d", "someField":"Z"}

And then I want to do a term aggregation by "someField" where all records having the same "my_id" are considered only once, thus resulting in something like :
{ "someField": { "buckets": [{ "key": "X", "count": 1 }, { "key": "Y", "count": 1 }, { "key": "Z", "count": 2 }] } }

Let me underline that the bucket for X has count 1

Any way/ideas on how to achieve this ?

Thanks!

Given your example I expect you want to use the cardinality aggregation on the my_id field - put this under your terms agg for some_field

1 Like

Nice, this worked great, thanks!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.