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

标签 elasticsearch

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

相关文章:

elasticsearch - Elasticsearch 数据最佳实践/关系数据优化

java - 为什么在 Spring Boot 中无法加载 Elasticsearch 节点会出现此错误?

elasticsearch - 在Logstash和Kafka中优先处理消息

docker - 如何记录对elasticsearch容器进行的所有查询?

elasticsearch - Elasticsearch中的条件过滤

Elasticsearch:长值最大聚合结果为双值

elasticsearch - Kibana Windows zip 分发包解压时间过长

Elasticsearch-如何在 Sense 或 Head 插件上进行多搜索请求

node.js - "Cannot read property ' 预签名-过期 ' of undefined"

elasticsearch - 按字段关联 ELK 中的消息