# Aggregation mixed with query?

**URL:** <https://discuss.elastic.co/t/aggregation-mixed-with-query/297008>\
**Category:** Elasticsearch\
**Created:** [February 11, 2022, 5:45pm UTC](https://discuss.elastic.co/t/aggregation-mixed-with-query/297008 "2022-02-11T17:45:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karol\_Radomski](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karol_radomski/32/97648_2.png) [@Karol\_Radomski](https://discuss.elastic.co/u/Karol_Radomski)\
**Post date:** [February 11, 2022, 5:45pm UTC](https://discuss.elastic.co/t/aggregation-mixed-with-query/297008/1 "2022-02-11T17:45:50Z")

</div>

Hello,

I don't know how to create such aggregation with query inside:

for given documents:

```auto
    "hours": "4",
    "class": "yellow",
    "name": "andy",
    "mark": "A"
}
{
    "hours": "6",
    "class": "yellow",
    "name": "randy",
    "mark": "B"
}
{
    "hours": "3",
    "class": "blue",
    "name": "candy",
    "mark": "A"
}
{
    "hours": "1",
    "class": "blue",
    "name": "wendy",
    "mark": "B"
}
{
    "hours": "10",
    "class": "blue",
    "name": "joe",
    "mark": "F"
}

```

I'd like to recieve something like this (my pseudo response):

```auto

(...)
"aggregations": {
    "classes": {
        "buckets": [
            {
                "key": "yellow",
                "doc_count": 2,
                "sumOfHours": {
                    "value": 10,
                },
                "hits":[
                    {
                        "_source": {
                            "name": "andy",
                            "mark": "A"
                        }
                    },
                    {
                        "_source": {
                            "name": "randy",
                            "mark": "B"
                        }
                    }
                ]
            },
            {
                "key": "blue",
                "doc_count": 3,
                "sumOfHours": {
                    "value": 14,
                },
                "hits":[
                    {
                        "_source": {
                            "name": "candy",
                            "mark": "A"
                        }
                    },
                    {
                        "_source": {
                            "name": "wendy",
                            "mark": "B"
                        }
                    },
                    {
                        "_source": {
                            "name": "joe",
                            "mark": "F"
                        }
                    }
                ]
            }
        ]
    }
}
(...)

```

To be more precise I have no problem with creating aggregation where each class is one bucked and summing hours. But Is it possible to also list all students inside of each bucket?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 12, 2022, 12:49pm UTC](https://discuss.elastic.co/t/aggregation-mixed-with-query/297008/2 "2022-02-12T12:49:01Z")

</div>

How about [top hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)?  
It is not for all hits but it may work if the required size is small.

---

<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:** [March 12, 2022, 12:49pm UTC](https://discuss.elastic.co/t/aggregation-mixed-with-query/297008/3 "2022-03-12T12:49:13Z")

</div>

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