# Collect first document from each bucket

**URL:** <https://discuss.elastic.co/t/collect-first-document-from-each-bucket/244152>\
**Category:** Elasticsearch\
**Created:** [August 7, 2020, 10:13am UTC](https://discuss.elastic.co/t/collect-first-document-from-each-bucket/244152 "2020-08-07T10:13:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stwrt](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stwrt](https://discuss.elastic.co/u/stwrt)\
**Post date:** [August 7, 2020, 10:13am UTC](https://discuss.elastic.co/t/collect-first-document-from-each-bucket/244152/1 "2020-08-07T10:13:50Z")

</div>

Hi,

I need some help with a query:

I have a "students" index, and each student has a "classroomId" and a "grade" fields.

I need to get the best student in each class.

I'm using "aggs", but what I get is the list of all students, and in the aggregations section, the number of students of each class.

Maybe "aggs" is not designed for this purpose. Whay I need to use instead of it?

Regards

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 12, 2020, 9:59am UTC](https://discuss.elastic.co/t/collect-first-document-from-each-bucket/244152/2 "2020-08-12T09:59:48Z")

</div>

This is impossible to answer without a concrete example that includes index creation, index mapping, sample documents and the query you have been tested with. Without those everything else would be just trying to guess things. See [https://www.elastic.co/help](https://www.elastic.co/help)

Also being more exact what the best student is would help. The one where the sum of all grades is the highest? The one with the single highest grade? Hard to tell until now

---

<div class="post-metadata">

**Author:** ![stwrt](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stwrt](https://discuss.elastic.co/u/stwrt)\
**Post date:** [August 12, 2020, 11:14am UTC](https://discuss.elastic.co/t/collect-first-document-from-each-bucket/244152/3 "2020-08-12T11:14:59Z")

</div>

Sorry, my fault. I 've an insane clear schema on my mind and I don't think in external points of view.

I get the solution, so I post here:

```auto
    {
        "aggs" : {
            "classroom_agg" : {
                "terms" : { 
                    "field" : "classRoomId"
                },
                "aggs": {
                    "best_student": {
                        "top_hits": {
                            "size": 1,
                            "sort": [
                                {
                                    "grade": {
                                        "order": "desc"
                                    }
                                }
                            ]
                        }
                    }   
                }
            }
        }
    }

```

What I missed in my first attempts was the 'top\_hits' sections. I didn't know about this query term, and I was becoming crazy.

---

<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:** [September 9, 2020, 11:15am UTC](https://discuss.elastic.co/t/collect-first-document-from-each-bucket/244152/4 "2020-09-09T11:15:15Z")

</div>

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