我有一个包含对象eventvalue-eventtime的索引。我想编写一个查询,该查询将基于过去30秒的eventvalue返回汇总的事件计数。另外,如果在给定的几秒钟内没有事件,我需要空存储桶-我需要在图表上显示此数据。
所以我写了以下查询:
{
"query" : {
"bool" : {
"must" : [
{
"range" : {
"eventtime" : {
"gte" : "now-30s/s",
"lte" : "now/s",
"format" : "yyyy-MM-dd HH:mm:ss",
"time_zone": "+03:00"
}
}
},
{
"range" : {
"eventvalue" : {
"lte" : 3
}
}
}
]
}
},
"aggs": {
"values_agg": {
"terms": {
"field": "eventvalue",
"min_doc_count" : 0,
"order": {
"_term": "asc"
}
},
"aggs": {
"events_over_time" : {
"date_histogram" : {
"field" : "eventtime",
"interval" : "1s",
"min_doc_count" : 0,
"extended_bounds" : {
"min" : "now-30s/s",
"max" : "now/s"
},
"format" : "yyyy-MM-dd HH:mm:ss",
"time_zone": "+03:00"
}
}
}
}
}
}
此查询无法正常工作,我也不知道为什么。具体来说,第一个“范围”查询为我提供了所需的时间间隔(如果删除该时间间隔,则我将一直获取值)。但是第二个“范围”查询似乎没有效果。 Eventvalue可以是1到10之间的任何值,并且理想的效果是,我将为Eventvalue 1-3设置三个存储桶。但是,我得到所有事件的所有10个存储桶。
如何解决此查询,使其仍然返回空存储桶,但仅返回选定的偶数值?
最佳答案
我相信您需要从"min_doc_count": 0
聚合中删除terms
。要实现您想要的空存储桶,只需在min_doc_count
聚合中使用date_histogram
。
按照documentation进行术语汇总:
Setting min_doc_count=0 will also return buckets for terms that didn’t match any hit.
这说明了为什么您会看到大于3的事件值的存储桶。它们被查询过滤掉,但被术语聚合带回。
更新
由于在30秒的时间片中可能没有事件值,因此我建议的另一种方法是使用
filters
聚合手动指定要用作存储桶的离散值。参见documentation here。尝试将其用于聚合:
"aggs": {
"values_agg": {
"filters": {
"filters": {
"1": { "term": { "eventvalue": 1 }},
"2": { "term": { "eventvalue": 2 }},
"3": { "term": { "eventvalue": 3 }}
}
},
"aggs": {
"events_over_time" : {
"date_histogram" : {
"field" : "eventtime",
"interval" : "1s",
"min_doc_count" : 0,
"extended_bounds" : {
"min" : "now-30s/s",
"max" : "now/s"
},
"format" : "yyyy-MM-dd HH:mm:ss",
"time_zone": "+03:00"
}
}
}
}
}
关于elasticsearch - Elasticsearch中的范围查询无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34947555/