首页/云/可观测性速查/聚合:从日志里做统计
本包目录(282 条)
监控体系与方法40 条Prometheus 与采集64 条Grafana 与看板53 条
日志平台52 条
链路追踪23 条告警与响应50 条

聚合:从日志里做统计

聚合把原始日志变成趋势与分布:date_histogram 出时间序列,terms 出分组 TopN,cardinality 近似去重;指标聚合嵌套在桶聚合里。

可观测性速查云更新于 2026-10-12

按分钟统计错误与去重用户

按分钟统计错误与去重用户
GET /logs-app/_search
{
  "size": 0,
  "aggs": {
    "per_minute": {
      "date_histogram": { "field": "@timestamp", "fixed_interval": "1m" },
      "aggs": {
        "errors": { "filter": { "term": { "level.keyword": "error" } } },
        "uv":  { "cardinality": { "field": "user_id.keyword" } }
      }
    }
  }
}
size: 0 表示不返回命中文档,只取聚合结果,适合纯统计查询。

易错点

  • cardinality 是近似去重(HyperLogLog 类算法),对账级精确计数不能依赖它
  • 高基数字段的 terms 聚合默认只返回前 10 组,需要显式设置 size 与排序

数据来源与参考

  • Elastic 官方文档 · Aggregations
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌