Is there a way to do group by aggregations and get all the documents that belong to particular group aggregate?


(Kant Kodali) #1

Is there a way to do group by aggregations and get all the documents that belong to particular group aggregate ?

so this is not like group by aggregation where for each group you get some aggregate/metrics but also I want all the records that lead to a particular group aggregate in one query. Is that possible in ES today?

For Example:

Input Dataset

{"name": "foo", "amount": 5, "city":"san francisco",  "state": "CA"}
{"name": "foo", "amount": 10, "city":"Los angeles",  "state": "CA"}
{"name": "bar", "amount": 20, "city":"Austin",  "state": "TX"}

Now say I want to group by name and state and get sum of "amount" and count for each group and the records themselves that lead to aggregate results. so the expected output is like this

Expected Output:

[
    {group: {"name": "foo", "state": "CA"}, "amount": 15, "count": 2, "docs": [{"name": "foo", "amount": 5, "city":"san francisco",  "state": "CA"}, {"name": "foo", "amount": 10, "city":"Los angeles",  "state": "CA"}]}, 
    {group: {"name": "bar", "state": "TX"}, "amount": 20, "count": 1, "docs": [{"name": "bar", "amount": 20, "city":"Austin",  "state": "TX"}]}
]

(David Pilato) #2

Do a first level of agg on state field then a sub agg on city field and you should be fine.


(Kant Kodali) #3

Hi, I think you might have missed a good portion of my question. The problem here is to retrieve documents along with aggregates. Please see the expected output in my question.


(David Pilato) #4

So you want top hits?

See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html


(system) #5

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