# Getting max and min topHits from ElasticSearch terms aggregation

**URL:** <https://discuss.elastic.co/t/getting-max-and-min-tophits-from-elasticsearch-terms-aggregation/58119>\
**Category:** Elasticsearch\
**Created:** [August 16, 2016, 10:14am UTC](https://discuss.elastic.co/t/getting-max-and-min-tophits-from-elasticsearch-terms-aggregation/58119 "2016-08-16T10:14:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinevic](https://avatars.discourse-cdn.com/v4/letter/x/dc4da7/32.png) [@xinevic](https://discuss.elastic.co/u/xinevic)\
**Post date:** [August 16, 2016, 10:14am UTC](https://discuss.elastic.co/t/getting-max-and-min-tophits-from-elasticsearch-terms-aggregation/58119/1 "2016-08-16T10:14:33Z")

</div>

Hi all!

I have terms aggregation by one field and I have max and 2 top hits: with max and min date.  
How can I do this with subAggregation?

Can I do this in one terms aggregation, or i need to do terms aggregtion twice?

Now I can do only like this: do terms aggregation, then min aggregation and it's topHits;  
do terms aggregation, then max aggregation and it's topHits.

I am using ES Java API.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 17, 2016, 10:56pm UTC](https://discuss.elastic.co/t/getting-max-and-min-tophits-from-elasticsearch-terms-aggregation/58119/2 "2016-08-17T22:56:19Z")

</div>

> [@xinevic](#):
>
> Now I can do only like this: do terms aggregation, then min aggregation and it's topHits;do terms aggregation, then max aggregation and it's topHits.

Pretty sure this is the only way to do it.

---

<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, 10:27pm UTC](https://discuss.elastic.co/t/getting-max-and-min-tophits-from-elasticsearch-terms-aggregation/58119/3 "2017-07-05T22:27:07Z")

</div>


