Elasticsearch:如何在搜索时得到精确的总 hits 数
從 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 accuratelywithout visiting all matches, which is costly for queries that match lots of documents. Thetrack_total_hitsparameter 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
}
}
}
}
總結
以上是生活随笔為你收集整理的Elasticsearch:如何在搜索时得到精确的总 hits 数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言调用tensorflow模型,C+
- 下一篇: win7无声音显示“未插入扬声器或耳机”