# numHits must be \> 0; please use TotalHitCountCollector if you just need the total hit count

**URL:** <https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516>\
**Category:** Elasticsearch\
**Created:** [April 4, 2020, 2:28pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516 "2020-04-04T14:28:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casper\_Skydt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casper_skydt/32/48956_2.png) [@Casper\_Skydt](https://discuss.elastic.co/u/Casper_Skydt)\
**Post date:** [April 4, 2020, 2:28pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516/1 "2020-04-04T14:28:51Z")

</div>

Once in a while i get the following error:

> numHits must be \> 0; please use TotalHitCountCollector if you just need the total hit count

The query is the following, with varying 'from', but never larger than 750:

```auto
 {
    	"track_total_hits": true,
    	"size": 15,
    	"from": 750,
    	"sort": [
    		{
    			"_score": {
    				"order": "desc"
    			}
    		},
    		{
    			"sortDate": {
    				"order": "desc"
    			}
    		}
    	],
    	"query": {
    		"function_score": {
    			"boost_mode": "sum",
    			"query": {
    				"bool": {
    					"filter": [
    						{
    							"match_all": {}
    						}
    					],
    					"must": []
    				}
    			},
    			"functions": [
    				{
    					"weight": 5.0
    				},
    				{
    					"weight": 1.0
    				},
    				{
    					"filter": {
    						"nested": {
    							"path": "addOns",
    							"query": {
    								"bool": {
    									"filter": [
    										{
    											"terms": {
    												"addOns.addOnID": [
    													8,
    													10
    												]
    											}
    										},
    										{
    											"range": {
    												"addOns.startDate": {
    													"lte": "2020-04-04T15:56:16.8498491+02:00"
    												}
    											}
    										},
    										{
    											"range": {
    												"addOns.finishDate": {
    													"gte": "2020-04-04T15:56:16.8498491+02:00"
    												}
    											}
    										}
    									]
    								}
    							}
    						}
    					},
    					"weight": 10.0
    				}
    			]
    		}
    	}
    }

```

I have increased the index to "max\_result\_window": "50000".  
It is a standalone installation v. 7.5.2  
The index has 1 shard and 0 replica.  
The index never have more than 30.000 documents.

Why do the server throw Bad Request: "numHits must be \> 0; please use TotalHitCountCollector if you just need the total hit count" ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 6, 2020, 1:59pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516/2 "2020-04-06T13:59:44Z")

</div>

can you share a full stack trace? You can use the `error_trace=true` parameter, see [https://www.elastic.co/guide/en/elasticsearch/reference/7.6/common-options.html#common-options-error-options](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/common-options.html#common-options-error-options)

Why did you change the max result window, if your `from` never exceeds 750?

Also, is the query you are running or just a part of it?

---

<div class="post-metadata">

**Author:** ![Casper\_Skydt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casper_skydt/32/48956_2.png) [@Casper\_Skydt](https://discuss.elastic.co/u/Casper_Skydt)\
**Post date:** [April 6, 2020, 5:03pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516/3 "2020-04-06T17:03:21Z")

</div>

The query is the complete query.

I increased "max\_result\_window" because i needed to support some legacy views, where the user can go to page 1300. It is not optimal, but it is a move away from a sql solution, so not all can be changed at once, and has to be gradually rewritten. Since we are not talking millions of documents, i was not worried.

I'm guessing, this is the part i am hitting:

> <https://github.com/apache/lucene-solr/blob/62f5bd50cdd5ce8dd47267d39c02a0595f944064/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java#L427>

It is only, once in a while i get these errors. I will enable the stacktrace option in production.

---

<div class="post-metadata">

**Author:** ![Casper\_Skydt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casper_skydt/32/48956_2.png) [@Casper\_Skydt](https://discuss.elastic.co/u/Casper_Skydt)\
**Post date:** [April 6, 2020, 5:30pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516/4 "2020-04-06T17:30:39Z")

</div>

D'oh, i found the error.

It is somehow possible for some users to set a negative "from". in a specific view.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 7, 2020, 2:10pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516/5 "2020-04-07T14:10:24Z")

</div>

Good find, I think the handling there should be improved to avoid confusion... I'll open an issue for that.

---

<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:** [May 5, 2020, 2:10pm UTC](https://discuss.elastic.co/t/numhits-must-be-0-please-use-totalhitcountcollector-if-you-just-need-the-total-hit-count/226516/6 "2020-05-05T14:10:30Z")

</div>

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