Skip to content

Reported hits count are inconsistent between _search and _search/template #52801

@consulthys

Description

@consulthys

Elasticsearch version (bin/elasticsearch --version): 7.5.0

Plugins installed: []

JVM version (java -version): Elastic Cloud

OS version (uname -a if on a Unix-like system): Elastic Cloud

Description of the problem including expected versus actual behavior:

Since ES 7, one must use rest_total_hits_as_int=true in order to revert to the old behavior of getting an exact number of total hits in the search response. I feel there is a discrepancy in how the search and _search/template endpoints behave regarding the reported number of hits.

In my tests below, I'm querying an index with more than 10000 documents with the exact same JSON query (as a normal query and as a template query depending on which endpoint I'm targeting).

{
  "query": {
    "match_all": {}
  }
}

A. When using the _search endpoint, I get this:

"total" : {
  "value" : 10000,
  "relation" : "gte"
},

B. When using the _search?rest_total_hits_as_int=true endpoint, I get this:

"total" : 173175,

C. When using the _search/template endpoint, I get this:

"total" : {
  "value" : 10000,
  "relation" : "gte"
},

So far, so good, everything is consistent.

D. But when I hit the _search/template?rest_total_hits_as_int=true endpoint, I get this:

"total" : 10000,

The only way I found to get the exact total with the _search/template endpoint is by adding the "track_total_hits": true parameter to the template query.

E. When doing so, I get this when hitting the _search/template endpoint

"total" : {
  "value" : 173175,
  "relation" : "eq"
},

F. and this when when hitting the _search/template?rest_total_hits_as_int=true endpoint

"total" : 173175,

There are two take-aways here:

  1. Since A and C are consistent, I feel that B and D should also be consistent.
  2. I also think that B is wrong and should require "track_total_hits": true in the query in order to spit out the exact number of hits (like in cases E and F)

Steps to reproduce:

It's easy to reproduce this on any index that has more than 10K documents and creating a simple match_all template query.

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Search/SearchSearch-related issues that do not fall into other categories>bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions