# Total hits with field collapsing

**URL:** https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061
**Category:** Elasticsearch
**Created:** [October 24, 2017, 12:22pm UTC](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061 "2017-10-24T12:22:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ons](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ons/32/18296_2.png) [@Ons](https://discuss.elastic.co/u/Ons)
#### Post date: [October 24, 2017, 12:22pm UTC](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061/1 "2017-10-24T12:22:04Z")

</div>

Hi all,

I am using the [field collapsing feature](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html#search-request-collapse) to group my results by a certain field and get only the top ranked hits.

In the documentation, it is mentioned that ''The total number of hits in the response indicates the number of matching documents without collapsing. The total number of distinct group is unknown.''

The problem is that i can not do correct pagination since the total hits count which is returned is the count of the collapsed top hits + the inner\_hits for each collapsed top hits.

Is there a recommended way to set the total hits as just the top hits (distinct group) count ?

Thanks in advance !

---

<div class="post-metadata">

### Author: ![andrewts](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@andrewts](https://discuss.elastic.co/u/andrewts)
#### Post date: [November 3, 2017, 2:50pm UTC](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061/2 "2017-11-03T14:50:18Z")

</div>

Thanks for posting, I'm running into the same issue of the total\_hits not being correct. We got around the paging issue by requesting +1 per page and using that to determine if there is a next page. It obviously doesn't work for jumping more than one page forward, but our users are OK with that. The bigger issue for us is the confusion caused when the count of results is drastically different than the number of actual results displayed.

Field collapsing is an awesome feature, but the top\_hits issue really limits it's usefulness for us.

---

<div class="post-metadata">

### Author: ![Adam\_Juraszek](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Adam\_Juraszek](https://discuss.elastic.co/u/Adam_Juraszek)
#### Post date: [November 8, 2017, 8:29pm UTC](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061/3 "2017-11-08T20:29:59Z")

</div>

It is possible to use aggregations to get the correct number of collapsed documents:  
"aggs": {  
"total": {  
"cardinality": {  
"field": "name-of-collapsed-field"  
}  
}  
}

---

<div class="post-metadata">

### Author: ![andrewts](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@andrewts](https://discuss.elastic.co/u/andrewts)
#### Post date: [November 8, 2017, 9:19pm UTC](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061/4 "2017-11-08T21:19:48Z")

</div>

Thank you so much for that advice! Looks like it works perfectly.

---

<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: [December 6, 2017, 9:20pm UTC](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061/5 "2017-12-06T21:20:18Z")

</div>

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