How to get documents from the buckets in terms aggregation?

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.

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
[2] https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

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.

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.