從 Elasticsearch 7.0之后,為了提高搜索的性能,在 hits 字段中返回的文檔數有時不是最精確的數值。Elasticsearch 限制了最多的數值為10000。
-
{
-
"took" : 1,
-
"timed_out" : false,
-
"_shards" : {
-
"total" : 1,
-
"successful" : 1,
-
"skipped" : 0,
-
"failed" : 0
-
},
-
"hits" : {
-
"total" : {
-
"value" : 10000,
-
"relation" : "gte"
-
},
-
...
-
}
當文檔的數值大於10000時,返回的 total 數值為10000,並在 relation 中指出 gte。
我們可以做如下的一個實驗。啟動Kibana:
然后選中“Add data”:
這樣我們就把Sample flight data的數據加載到Elasticsearch中去了。
我們在Dev tools中來查詢我們的文檔個數:
我們可以看到有13059個數值。假如我們使用如下的方式來進行搜索的話:
顯然我們得到的文檔的數目是10000個,但是它並不是我們的實際的滿足條件的所有文檔數。假如我們想得到所有的文檔數,那么我們可以做如下的方式:
我們在請求的參數中加入 track_total_hits,並設置為true,那么我們可以看到在返回的參數中,它正確地顯示了所有滿足條件的文檔個數。
=========================================================================================================================
ElasticSearch Count API 和 track_total_hits 兩者的區別是什么?
我想計算:某個查詢條件(比如exists-query)下文檔的總數,看了下ES官方文檔:count api :
Gets the number of matches for a search query.
返回符合查詢條件的文檔數量,應該能滿足需求。
但是,我又看到了另一個參數:track-total-hits,這里面提到:某個查詢條件下的total hits是不准確的,因為它沒有:visiting all matches。而 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.
我的疑問是:
count api 得到的 符合查詢條件的文檔 數量一定是准確的吧?如果是准確的話,那么它應該 visiting all matches了,那是不是說明:count api 是一個很耗時的操作吧?那么 我要計算:符合某個查詢條件下的文檔的准確數量時,使用 count api 好呢?還是 使用 track_total_hits 好呢?有什么坑要注意的么?
參考鏈接:Do not compute hit counts by default=
===========================================================================================================================================
es7.x在查詢時,必須加上track_total_hits,不然就只顯示10000
{
"track_total_hits": true,
"query": {
"range": {
"ts": {
"gte": 0
}
}
}
}