# Aggregation topHits problem

**URL:** <https://discuss.elastic.co/t/aggregation-tophits-problem/58022>\
**Category:** Elasticsearch\
**Created:** [August 15, 2016, 11:39am UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022 "2016-08-15T11:39:01Z")\
**Posts on this page:** 8\
**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 15, 2016, 11:39am UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/1 "2016-08-15T11:39:02Z")

</div>

Hi all!

I need to do aggregation by field and return all relevant hits, not only 10.  
But now I have a strange problem: I have 8 relevant hits (according to count), but topHits returns me only 3. Why?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [August 15, 2016, 1:00pm UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/2 "2016-08-15T13:00:57Z")

</div>

For any further investigation it would be good to know what you are doing. Can you post the aggregation, maybe some sample data, maybe also your mappings if they might play a role in identifying your issue?

---

<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 15, 2016, 1:07pm UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/3 "2016-08-15T13:07:07Z")

</div>

Thanks for answer.

In original I have to do aggregation by sevaral fields, but ES doesn't support it.  
So, I create not analyzed field which contains json representation of all needed fields and group by it.

Mappings I can't provide, I can provide only code if needed.  
I use Java API, ES version is 1.5.2

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [August 15, 2016, 3:12pm UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/4 "2016-08-15T15:12:52Z")

</div>

Just a guess, but by default the top hits aggregation returns 3 hits, so set the size on the top hits builder?

---

<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 15, 2016, 3:14pm UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/5 "2016-08-15T15:14:16Z")

</div>

By default it returns 10 records.  
If I specify size: MAX\_INT, it returns me nothing.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 15, 2016, 4:43pm UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/6 "2016-08-15T16:43:54Z")

</div>

The default for the overall search size is 10, but the default size for top  
hits is 3. Two different settings:

[https://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-aggregations-metrics-top-hits-aggregation.html#\_options\_2](https://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-aggregations-metrics-top-hits-aggregation.html#_options_2)

This low limit is due to the fact that top hits is not highly performant  
and can lead to large responses when larger values are used.

Ivan

---

<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 15, 2016, 4:49pm UTC](https://discuss.elastic.co/t/aggregation-tophits-problem/58022/7 "2016-08-15T16:49:53Z")

</div>

Great, thank you.

I have to get two hits from aggregation results, which contains min and max date field respectively.  
Can I do this in one terms aggregation using subAggregations? Or I need to do two terms aggregations, in fist I need to get topHit with min date, in second - topHit with max date?

---

<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/aggregation-tophits-problem/58022/8 "2017-07-05T22:27:43Z")

</div>


