# [Elasticsearch] 在查询 Elasticsearch 时始终返回精确的查询结果数

Elasticsearch 的较早版本在查询文档数据时，始终会返回查询结果的精确(命中)总数：

GET _search?size=0
{
"hits": {
"total": 1267474995831,
"max_score": 0,
"hits": []
}
}

total就是一个标量，直接表明结果的总数。

GET _search?size=0
{
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
}
}

total变成了一个对象。value是一个模糊的值，relation是这个精确值与模糊值的关系。 比如这里的意思是表示：大概有10000个查询结果，精确的值比它大于或等于/不少于(gte, greater than or equal to)

## Track total hits

Generally the total hit count can’t be computed accurately without visiting all matches, which is costly for queries that match lots of documents.

The track_total_hits parameter allows you to control how the total number of hits should be tracked. Given that it is often enough to have a lower bound of the number of hits, such as "there are at least 10000 hits", the default is set to 10,000. This means that requests will count the total hit accurately up to 10,000 hits.

It’s is a good trade off to speed up searches if you don’t need the accurate number of hits after a certain threshold.

GET _search?size=0
{
"track_total_hits": true
}
{
"hits" : {
"total" : {
"value" : 11002,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}