gpt4 book ai didi

elasticsearch - Elasticsearch中的范围查询无法正常工作

转载 作者:行者123 更新时间:2023-12-02 23:32:27 25 4
gpt4 key购买 nike

我有一个包含对象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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com