在使用ES搜索的時候,或多或少都會面臨查詢數據總量的狀況,下面介紹三種查詢數據總量的方式。html
其中,方案二解決了當結果數據總量超過1w時,因爲ES默認設置(max_result_window:10000,出於性能問題考慮,用戶也不想放開這個限制),只能返回命中數等於1w的問題。elasticsearch
查詢所有索引下的文檔總數:ide
GET /_cat/count
查詢某個索引下的文檔總數(<target>爲索引名):性能
GET /_cat/count/<target>
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-count.htmlui
將 track_total_hits" 屬性設置爲 true(<target>爲索引名)code
GET <target>/_search { "track_total_hits": true, // 👇🏻如下能夠爲任意檢索條件 "query": { "match_all" : { } } }
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.htmlhtm
此命令是查詢索引狀態的,固然也包含了每一個索引下的文檔數量。可是在部分狀況下,是不夠準確的,由於這個數量包含了隱藏的嵌套文檔。參考官方文檔的解釋:
These metrics are retrieved directly from Lucene, which Elasticsearch uses internally to power indexing and search. As a result, all document counts include hidden nested documents.
To get an accurate count of Elasticsearch documents, use the cat count or count APIs.索引
GET /_cat/indices GET /_cat/indices/<target>
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-indices.html文檔