Aggregations using distinct query

(Nicolas Emiliani) #1

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 :

{"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 ?


(Mark Harwood) #2

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

(Nicolas Emiliani) #3

Nice, this worked great, thanks!

(system) #4

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