elasticsearch ElasticSearch/Kibana:获取在某个日期之前的条目中找不到的值
我有一组设备,可以定期(比如说每10分钟)推送ElasticSearch此表单的条目:elasticsearch ElasticSearch/Kibana:获取在某个日期之前的条目中找不到的值,elasticsearch,kibana,elasticsearch,Kibana,我有一组设备,可以定期(比如说每10分钟)推送ElasticSearch此表单的条目: { "deviceId": "unique-device-id", "timestamp": 1586390031, "payload" : { various data } } 我通常通过Kibana查看这一点,方法是过滤最后7天的数据,然后通过设备id或有效负载中的其他数据进行向下搜索 现在,我试图通过查找在过去一小时内没有报告任何情况的设备来了解这支车队的健康状况。我一直在搞各
{
"deviceId": "unique-device-id",
"timestamp": 1586390031,
"payload" : { various data }
}
我通常通过Kibana查看这一点,方法是过滤最后7天的数据,然后通过设备id或有效负载中的其他数据进行向下搜索
现在,我试图通过查找在过去一小时内没有报告任何情况的设备来了解这支车队的健康状况。我一直在搞各种各样的过滤器和可视化,最接近的是一个数据表,上面有设备ID和每个设备的最后一个条目的时间戳,按时间戳排序。这是有用的,但有点难以使用,因为我有几千台设备
我梦想的是得到上面提到的表格,只包含在过去一小时内没有报告的设备ID,或者只得到两个数字:在过去7天内看到的不同设备ID的总数和在过去一小时内没有看到的设备ID的总数
你能给我指出正确的方向吗,如果其中任何一个都有可能的话?我将跳过表格,采取第二种方法——只获取计数。我认为你可以从计数中向后走到行
注意:我将使用人类可读的时间格式,而不是时间戳,但是
epoch\u seconds
在您的实际用例中也同样适用。此外,我还添加了comment
字段,为每个文档提供一些背景信息
首先,设置一个索引:
PUT fleet
{
"mappings": {
"properties": {
"timestamp": {
"type": "date",
"format": "epoch_second||yyyy-MM-dd HH:mm:ss"
},
"comment": {
"type": "text"
},
"deviceId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
同步一些文档--我在UTC+2,所以我选择了以下时间戳:
POST fleet/_doc
{
"deviceId": "asdjhfa343",
"timestamp": "2020-04-05 10:00:00",
"comment": "in the last week"
}
POST fleet/_doc
{
"deviceId": "asdjhfa343",
"timestamp": "2020-04-10 13:05:00",
"comment": "#asdjhfa343 in the last hour"
}
POST fleet/_doc
{
"deviceId": "asdjhfa343",
"timestamp": "2020-04-10 12:05:00",
"comment": "#asdjhfa343 in the 2 hours"
}
POST fleet/_doc
{
"deviceId": "asdjhfa343sdas",
"timestamp": "2020-04-07 09:00:00",
"comment": "in the last week"
}
POST fleet/_doc
{
"deviceId": "asdjhfa343sdas",
"timestamp": "2020-04-10 12:35:00",
"comment": "in last 2hrs"
}
我们总共有5个文档和2个不同的设备ID,并且满足以下条件
deviceId
,它在过去2小时内出现,但在过去1小时内没有出现
使用(用于范围过滤器),(用于不同计数)和(用于计数差异)聚合的组合
GET fleet/_search
{
"size": 0,
"aggs": {
"distinct_devices_last7d": {
"filter": {
"range": {
"timestamp": {
"gte": "now-7d"
}
}
},
"aggs": {
"uniq_device_count": {
"cardinality": {
"field": "deviceId.keyword"
}
}
}
},
"not_seen_last1h": {
"filter": {
"range": {
"timestamp": {
"gte": "now-2h"
}
}
},
"aggs": {
"device_ids_per_hour": {
"date_histogram": {
"field": "timestamp",
"calendar_interval": "day",
"format": "'disregard' -- yyyy-MM-dd"
},
"aggs": {
"total_uniq_count": {
"cardinality": {
"field": "deviceId.keyword"
}
},
"in_last_hour": {
"filter": {
"range": {
"timestamp": {
"gte": "now-1h"
}
}
},
"aggs": {
"uniq_count": {
"cardinality": {
"field": "deviceId.keyword"
}
}
}
},
"uniq_difference": {
"bucket_script": {
"buckets_path": {
"in_last_1h": "in_last_hour>uniq_count",
"in_last2h": "total_uniq_count"
},
"script": "params.in_last2h - params.in_last_1h"
}
}
}
}
}
}
}
}
date\u直方图
聚合只是一个占位符,它使我们能够使用bucket脚本
获得最终差异,而不必进行任何后期处理
因为我们通过了size:0
,所以我们对点击数
部分不感兴趣。因此,仅考虑聚合,以下是带注释的结果:
...
"aggregations" : {
"not_seen_last1h" : {
"doc_count" : 3,
"device_ids_per_hour" : {
"buckets" : [
{
"key_as_string" : "disregard -- 2020-04-10",
"key" : 1586476800000,
"doc_count" : 3, <-- 3 device messages in the last 2hrs
"total_uniq_count" : {
"value" : 2 <-- 2 distinct IDs
},
"in_last_hour" : {
"doc_count" : 1,
"uniq_count" : {
"value" : 1 <-- 1 distict ID in the last hour
}
},
"uniq_difference" : {
"value" : 1.0 <-- 1 == final result !
}
}
]
}
},
"distinct_devices_last7d" : {
"meta" : { },
"doc_count" : 5, <-- 5 device messages in the last 7d
"uniq_device_count" : {
"value" : 2 <-- 2 unique IDs
}
}
}
。。。
“聚合”:{
“最近1小时没有看到”:{
“单据计数”:3,
“每小时设备ID”:{
“桶”:[
{
“键作为字符串”:“忽略--2020-04-10”,
“密钥”:1586476800000,
“单据计数”:3,