# Terms Aggregation and get latest document

**URL:** <https://discuss.elastic.co/t/terms-aggregation-and-get-latest-document/304453>\
**Category:** Elasticsearch\
**Created:** [May 11, 2022, 10:42am UTC](https://discuss.elastic.co/t/terms-aggregation-and-get-latest-document/304453 "2022-05-11T10:42:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liam\_B](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@Liam\_B](https://discuss.elastic.co/u/Liam_B)\
**Post date:** [May 11, 2022, 10:42am UTC](https://discuss.elastic.co/t/terms-aggregation-and-get-latest-document/304453/1 "2022-05-11T10:42:48Z")

</div>

Hi

I am currently performing a search api call and that aggregates on a field and this works great, it gives me a list of terms and a count of those terms.

How would I go about supplementing the search response, with a selected number of fields from the latest document that was within the aggregate.

To get something roughly equivalent to

Term: TermA, Count: 50, Latest Field A: sunny, Latest Field B: 15degrees

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 13, 2022, 6:40am UTC](https://discuss.elastic.co/t/terms-aggregation-and-get-latest-document/304453/2 "2022-05-13T06:40:31Z")

</div>

Hi,  
If you need latest document in the bucket, how about using [top\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) aggregation? It is also possible to filter source by `_source` option.

---

<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:** [June 10, 2022, 6:41am UTC](https://discuss.elastic.co/t/terms-aggregation-and-get-latest-document/304453/3 "2022-06-10T06:41:20Z")

</div>

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