日期直方图聚合
date_histogram
聚合使用日期数学为时间序列数据生成直方图。
例如,您可以查找网站每月获得多少点击量。
GET opensearch_dashboards_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 | 必需 | 对象 | 一个对象,指定日期时间文档字段、间隔以及可选的格式和时区。 |
calendar_interval | 必需 | 时间间隔 | 用于构建每个桶的字段日期跨度。 |
format | 可选 | 字符串 | 日期格式字符串。如果省略,日期将输出为 64 位自纪元以来的毫秒数整数。 |
time_zone | 可选 | 字符串 | 一个字符串,表示与 UTC 的时间偏移量,可以是 ISO 8601 UTC 偏移量(“-07:00”)或时区数据库标识符(“America/Los_Angeles”)。 |