Aggregation mixed with query?

Hello,

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

for given documents:

    "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):


(...)
"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?

How about top hits aggregation?
It is not for all hits but it may work if the required size is small.

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