我在 Elasticsearch 中处理代表警报的文档。这些警报会激活一段时间,然后停用。它们类似于此模式。
{
"id": 189393,
"sensorId": "1111111",
"activationTime": 1462569310000,
"deactivationTime": 1462785524876,
}
我想知道每天的活跃警报数量。为实现这一点,我想执行一个日期直方图聚合,返回激活和停用之间的天数以及每天的事件警报数。
到目前为止我尝试过的是这个查询。
{
"query" : {
...
},
"aggs": {
"active_alerts": {
"date_histogram": {
"field": "timestamp",
"interval": "day"
}
}
}
}
但是,它仅在激活当天返回。
"aggregations": {
"active_alerts": {
"buckets": [
{
"key_as_string": "2016-05-06T00:00:00.000Z",
"key": 1462492800000,
"doc_count": 1
}
]
}
}
我要返回的是激活和停用时间之间的天数以及每天的事件警报数,如下所示。
"aggregations": {
"active_alerts": {
"buckets": [
{
"key_as_string": "2016-05-06T00:00:00.000Z",
"key": 1462492800000,
"doc_count": 1
},
{
"key_as_string": "2016-05-07T00:00:00.000Z",
"key": 1462579200000,
"doc_count": 1
},
{
"key_as_string": "2016-05-08T00:00:00.000Z",
"key": 1462665600000,
"doc_count": 1
}
]
}
}
谢谢。
最佳答案
最后,我通过脚本找到了一个解决方案,创建了一个发出从激活日期到停用日期的日期数组的解决方案。
"aggs": {
"active_alerts": {
"date_histogram": {
"interval": "day",
"script": "Date d1 = new Date(doc['activationTime'].value); Date d2 = new Date(doc['deactivationTime'].value); List<Date> dates = new ArrayList<Date>(); (d1..d2).each { date-> dates.add(date.toTimestamp().getTime())}; return dates;"
}
}
}
谢谢。
关于考虑文档范围内的日期的 ElasticSearch 日期直方图聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39121407/