首页/数据/NoSQL 速查/_count 统计命中数量
Elasticsearch89 条

_count 统计命中数量

_count 只返回匹配文档数,不取 hits、不做聚合;配合查询条件可在批量操作前核对影响范围,比 search 更轻量。

NoSQL 速查数据更新于 2026-10-12
接口GET /<index>/_count 或 POST,请求体只放 query
返回count 与 _shards 信息
绕过参数_count 不支持 from/size 与聚合

示例

示例
GET /my-index/_count
{
  "query": { "term": { "status": "open" } }
}

GET /logs/_count?q=status:error
执行 delete_by_query 或 reindex 前先用 _count 核对数量,是避免误操作的常规习惯。

易错点

  • _count 的 count 直接来自各分片统计,非常轻量,但结果随刷新节奏变化,写入高峰期两次结果可能不同
  • 不支持 routing 之外的复杂参数,需要分页信息时只能用 search

数据来源与参考

  • Elasticsearch Guide: Count API
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌