# Get one document per bucket in elasticsearch aggregation

**URL:** <https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086>\
**Category:** Elasticsearch\
**Created:** [August 13, 2018, 7:09am UTC](https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086 "2018-08-13T07:09:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tvvignesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tvvignesh/32/34417_2.png) [@tvvignesh](https://discuss.elastic.co/u/tvvignesh)\
**Post date:** [August 13, 2018, 7:09am UTC](https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086/1 "2018-08-13T07:09:55Z")

</div>

I had asked a question in Stack Overflow: [https://stackoverflow.com/questions/51816815/get-one-document-per-bucket-in-elasticsearch-aggregation](https://stackoverflow.com/questions/51816815/get-one-document-per-bucket-in-elasticsearch-aggregation)

Sharing it here. Please let me know if you know the solution for the same.

---

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [August 13, 2018, 7:12am UTC](https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086/2 "2018-08-13T07:12:07Z")

</div>

You can use top hits metric for that.

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

---

<div class="post-metadata">

**Author:** ![tvvignesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tvvignesh/32/34417_2.png) [@tvvignesh](https://discuss.elastic.co/u/tvvignesh)\
**Post date:** [August 13, 2018, 8:40am UTC](https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086/3 "2018-08-13T08:40:13Z")

</div>

Hi Peter. Thanks. I saw it earlier but assumed it wont solve the problem but it ended up working once I tried.

This is the query I used:

```auto
GET myindex/_search
{
    "size": 0,
    "aggs": {
        "clusters": {
            "terms": {
                "field": "myfield",
                "size": 100000
            },
        "aggs": {
            "mydoc": {
                "top_hits": {
                    "size" : 1
                }
            }
        }
        }
    },
    "query": {
                "bool": {
                    "must": [
                        {
                            "query_string": { "default_field": "field1", "query": "val1" }
                        },
                        {
                            "query_string": { "default_field": "field2", "query": "val2" }
                        }
                    ]
                }
            }
}

```

---

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [August 13, 2018, 8:52am UTC](https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086/4 "2018-08-13T08:52:32Z")

</div>

Good to hear it solved your problem!

---

<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 10, 2018, 8:52am UTC](https://discuss.elastic.co/t/get-one-document-per-bucket-in-elasticsearch-aggregation/144086/5 "2018-09-10T08:52:47Z")

</div>

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