日期直方图聚合

日期直方图聚合 #

date_histogram 日期直方图聚合使用日期计算来为时间序列数据生成直方图。

例如,您可以找到您的网站每月有多少次访问:

GET sample_data_logs/_search
{
  "size": 0,
  "aggs": {
    "logs_per_month": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "month"
      }
    }
  }
}

返回内容

...
"aggregations" : {
  "logs_per_month" : {
    "buckets" : [
      {
        "key_as_string" : "2020-10-01T00:00:00.000Z",
        "key" : 1601510400000,
        "doc_count" : 1635
      },
      {
        "key_as_string" : "2020-11-01T00:00:00.000Z",
        "key" : 1604188800000,
        "doc_count" : 6844
      },
      {
        "key_as_string" : "2020-12-01T00:00:00.000Z",
        "key" : 1606780800000,
        "doc_count" : 5595
      }
    ]
  }
}

返回内容包含三个月的日志。如果你绘制这些值,你可以看到你的网站每月请求流量的峰值和低谷。

参数说明 #

date_histogram 聚合支持以下参数。

参数必需/可选数据类型描述
date_histogram必填Object一个指定日期时间文档字段、间隔、可选格式和时区的对象。
calendar_interval必填时间间隔构建每个分组所使用的日期字段。
format可选String日期格式字符串。如果省略,日期将输出为 64 位自纪元以来的毫秒整数。
time_zone可选String表示 UTC 时间偏移的字符串,可以是 ISO 8601 UTC 偏移("-07:00")或 tz 数据库标识符(“America/Los_Angeles”)。