在使用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文档