# How to get documents from the buckets in terms aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239>\
**Category:** Elasticsearch\
**Created:** [September 14, 2015, 10:30am UTC](https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239 "2015-09-14T10:30:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![siddharth\_gupta](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@siddharth\_gupta](https://discuss.elastic.co/u/siddharth_gupta)\
**Post date:** [September 14, 2015, 10:30am UTC](https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239/1 "2015-09-14T10:30:55Z")

</div>

Hello,

I am using terms aggregation for only one field.  
I do get the proper results.

But the thing is I want to get the documents present in every bucket. (right now i am getting only the count)

For example:  
key\_1 (577)  
key\_2 (287)  
key\_3 (265)  
key\_4 (166)  
key\_5 (165)  
key\_6 (163)

What if I want the source of every document in each bucket ?  
Can somebody please help ?  
Thanks,  
Siddharth.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 14, 2015, 10:45am UTC](https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239/2 "2015-09-14T10:45:31Z")

</div>

The `top_hits` aggregation [1] is designed for returning samples of the best matching documents under an aggregation branch.  
However, it is not recommended for large-scale exports if the number of documents are high in which case you will need to use another API [2]

[1] [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)  
[2] [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html)

---

<div class="post-metadata">

**Author:** ![siddharth\_gupta](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@siddharth\_gupta](https://discuss.elastic.co/u/siddharth_gupta)\
**Post date:** [September 15, 2015, 12:05pm UTC](https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239/3 "2015-09-15T12:05:04Z")

</div>

Hi,

Thanks for reply, it was very helpful.  
Just tell me one more thing, isn't it somehow possible to use aggregations with scan and scroll ?  
Because I want to rank the documents on my own instead of using elastic search scoring.

So to switch off the scoring and sorting I’ll have to use scan and scroll.  
But then I won't be able to use aggregations later.

Can you Please suggest something ?

Thanks  
Siddharth.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 15, 2015, 1:33pm UTC](https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239/4 "2015-09-15T13:33:56Z")

</div>

> isn't it somehow possible to use aggregations with scan and scroll ?

No - scan and scroll is about accessing the detail of individual documents, paging through large numbers of them whereas aggregations are about summarising documents in a single hit.

---

<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:50pm UTC](https://discuss.elastic.co/t/how-to-get-documents-from-the-buckets-in-terms-aggregation/29239/5 "2017-07-05T23:50:17Z")

</div>


