Count API

Count API #

返回匹配查询的文档数量,不返回文档内容。

请求格式 #

GET /<index>/_count
POST /<index>/_count
GET /_count
POST /_count

路径参数 #

参数必需说明
<index>目标索引,支持逗号分隔多索引和通配符

查询参数 #

参数类型默认值说明
qstring简单查询字符串
dfstringq 参数的默认搜索字段
default_operatorstringORq 参数的默认逻辑运算符:ANDOR
analyzerstringq 参数使用的分析器
analyze_wildcardbooleanfalse是否分析通配符
lenientboolean宽松解析模式
routingstring路由值
preferencestring查询偏好
min_scorefloat最低分数过滤
terminate_afterint0每分片最多计数的文档数(0 = 不限制)
expand_wildcardsstringopen通配符展开方式:openclosedhiddenallnone
ignore_unavailablebooleanfalse忽略不存在的索引
allow_no_indicesbooleantrue允许通配符不匹配任何索引

请求体(可选) #

{
  "query": {
    "term": { "status": "published" }
  }
}

示例 #

使用查询字符串:

GET /website/_count?q=title:blog

使用请求体查询:

POST /website/_count
{
  "query": {
    "range": {
      "date": { "gte": "2024-01-01" }
    }
  }
}

响应:

{
  "count": 42,
  "_shards": {
    "total":      5,
    "successful": 5,
    "skipped":    0,
    "failed":     0
  }
}

参考导航 #

需求参见
执行完整搜索全文搜索
检索单条文档Get API