首页/数据/NoSQL 速查/terms 聚合:按字段分组统计
本包目录(322 条)
MongoDB99 条
Elasticsearch89 条
ClickHouse56 条时序与列式28 条选型与架构50 条

terms 聚合:按字段分组统计

terms 聚合按字段值分桶计数,字段必须是 keyword 或数值(text 会因 fielddata 关闭而报错);size 控制返回桶数量,结果含误差提示字段。

NoSQL 速查数据更新于 2026-10-12
接口"aggs": { "name": { "terms": { "field": "user.id", "size": 10 } } }
排序order 可指定 _count 或子聚合指标,默认按文档数降序
误差提示doc_count_error_upper_bound 与 sum_other_doc_count 反映被裁掉的桶

示例

示例
GET /my-index/_search
{
  "size": 0,
  "aggs": {
    "by_status": {
      "terms": { "field": "status", "size": 10, "missing": "unknown" }
    }
  }
}
搜索体里把 size 设为 0 可跳过 hits 返回,只取聚合结果,减少无用序列化。

易错点

  • 返回桶数量受 size 与 shard_size 影响,多个分片时前 N 名可能不完全精确,需要精确排名时提高 shard_size
  • 对 text 字段直接聚合报 Fielddata is disabled,先确认字段是不是 keyword

数据来源与参考

  • Elasticsearch Guide: Terms aggregation
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌