# How to return all documents for each bucket?

**URL:** <https://discuss.elastic.co/t/how-to-return-all-documents-for-each-bucket/38149>\
**Category:** Elasticsearch\
**Created:** [December 30, 2015, 5:19am UTC](https://discuss.elastic.co/t/how-to-return-all-documents-for-each-bucket/38149 "2015-12-30T05:19:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sonia](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@sonia](https://discuss.elastic.co/u/sonia)\
**Post date:** [December 30, 2015, 5:19am UTC](https://discuss.elastic.co/t/how-to-return-all-documents-for-each-bucket/38149/1 "2015-12-30T05:19:51Z")

</div>

bucket aggregation return the count of documents for each bucket. I want to retrieve the documents within each bucket that matched that bucket criteria, then tried to add the following top\_hits sub aggregation:

```
{
    "aggregations": {
        "myaggregation": {
            "terms": {
                       "field": "title",
                      "size": 0,
                      "shard_size" :0
             },
            "aggregations": {
                "hits": {
                    "top_hits": { "size": 100000 }
                }
            }
        }
    }
}

```

but This top\_hits works very slow.  
Is there any proper way to fetch the actual documents for each bucket after doing the terms aggregation quickly?

---

<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:** [July 5, 2017, 11:27pm UTC](https://discuss.elastic.co/t/how-to-return-all-documents-for-each-bucket/38149/2 "2017-07-05T23:27:54Z")

</div>


