【发布时间】:2021-07-22 14:03:51
【问题描述】:
我在 elasticsearch 的索引中有如下文档
[
{
"Id": 1,
"start": "2021-04-20T00:00:00.000000Z",
"end": "2021-04-22T22:45:20.000000Z",
"event_type": "A"
},
{
"Id": 2,
"start": "2021-04-23T00:01:00.000000Z",
"end": "2021-04-26T21:50:20.000000Z",
"event_type": "B"
},
{
"Id": 3,
"start": "2021-04-27T00:03:30.000000Z",
"end": "2021-04-29T04:15:30.000000Z",
"event_type": "A"
}
]
我想计算每个event_type 的总天数。例如,对于上述文件,
event_type A 在第一个文档中跨越了 3 天,"Id": 1 从 20 日(开始)到 22 日(结束),在第三个文档中又过了 3 天,"Id": 3 从 27 日(开始)到 29 日(结束)。所以我想为event_type A 计数 6。 event_type B 在第二个文档中跨越 4 天,"Id": 2 从 23 日(开始)到 26 日(结束)。所以,对于event_type B,我想算为 4。
| event_type | count |
|---|---|
| A | 6 |
| B | 4 |
我知道如何使用术语聚合来获取每种事件类型的文档数
GET /split_range/_search
{
"size": 0,
"aggs": {
"by_event_type": {
"terms": {
"field": "event_type"
}
}
}
}
有没有办法在开始和结束(包括开始和结束)之间的每一天将该文档进一步拆分为多个存储桶并获得该计数?
【问题讨论】:
标签: elasticsearch date-range elasticsearch-aggregation