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

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266>\
**Category:** Elasticsearch\
**Created:** [October 9, 2017, 10:13pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266 "2017-10-09T22:13:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kant\_kodali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kant_kodali/32/50637_2.png) [@kant\_kodali](https://discuss.elastic.co/u/kant_kodali)\
**Post date:** [October 9, 2017, 10:13pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266/1 "2017-10-09T22:13:23Z")

</div>

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"}]}
]
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 10, 2017, 6:09am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266/2 "2017-10-10T06:09:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kant\_kodali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kant_kodali/32/50637_2.png) [@kant\_kodali](https://discuss.elastic.co/u/kant_kodali)\
**Post date:** [October 10, 2017, 6:36am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266/3 "2017-10-10T06:36:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 10, 2017, 7:23am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266/4 "2017-10-10T07:23:59Z")

</div>

So you want top hits?

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 7, 2017, 7:24am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-group-by-aggregations-and-get-all-the-documents-that-belong-to-particular-group-aggregate/103266/5 "2017-11-07T07:24:02Z")

</div>

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