#### 前言
早在elasticsearch6.x版本时,es查询时默认会显示hits total总数,当然聚合场景下是不会的。目前我们项目将es6.8升级到7.6,一些普通查询,总数显示不正常了,最大只显示10000条。
比如查询语句如下:
```json
GET bd_frequent_test/_search
{
"query": {
"match_all": {}
}
}
```
查询结果如下:
```json
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 20,
"successful": 20,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 1.0,
"hits": [{
"_index": "bd_frequent_test",
"_type": "_doc",
"_id": "33030011",
"_score": 1.0,
"_routing": "506030",
"_source": {
"ape_id": "81",
"appear_num": 1,
"create_time": "2019-11-18 01:04:39",
"event_time": "2019-11-18",
"id": 33030011,
"last_event_time": "2019-11-18 01:04:39",
"pid": "506030",
"storage_date": "2019-11-18"
}
}]
}
}
```
es7版本只显示10000条总数,而实际命中文档数应该是100000000。
#### 解决办法
原因是7做了修改,默认最大只命中10000条,如果要让普通查询显示出全部命中的文档数,需要在查询中设置:
"track_total_hits":true
语句如下:
```json
GET bd_frequent_test/_search
{
"query": {
"match_all": {}
},
"track_total_hits":true
}
```
显示结果:
```json
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 20,
"successful": 20,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 100000000,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": "bd_frequent_test",
"_type": "_doc",
"_id": "33030011",
"_score": 1.0,
"_routing": "506030",
"_source": {
"ape_id": "81",
"appear_num": 1,
"create_time": "2019-11-18 01:04:39",
"event_time": "2019-11-18",
"id": 33030011,
"last_event_time": "2019-11-18 01:04:39",
"pid": "506030",
"storage_date": "2019-11-18"
}
}]
}
}
```
命中数就会显示出实际查询命中的文档数了。
hits响应体如下:
```json
"total": {
"value": 100000000,
"relation": "eq"
}
```