【问题标题】:Elasticsearch query with multiple conditions and time range具有多个条件和时间范围的 Elasticsearch 查询
【发布时间】:2017-08-09 17:19:20
【问题描述】:

我正在尝试创建一个查询来计算在最后一天满足两个条件的实例。

这个查询显示了两个条件的计数,但是当我尝试添加一个范围时,它似乎匹配所有文档:

 GET logstash-*/_count
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "rawmsg": {
                "query": "Could not send Message.",
                "type": "phrase"
              }
              }
            },
            {
              "match": {
                "stack_trace": {
                  "query": "*WebServiceException*",
                  "type": "phrase"
                  }
              }
            }
          ]
        }
      }

}

这是我尝试添加日期范围的方式:

GET logstash-*/_count
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "rawmsg": {
            "query": "Could not send Message.",
            "type": "phrase"
          }
          }
        },
        {
          "match": {
            "stack_trace": {
              "query": "*WebServiceException*",
              "type": "phrase"
              }
          }
        },
        {

          "range" : {
            "@timestamp" : {
                "gte" : "now-1d/d",
                "lt" :  "now/d"
            }
        }
        }
      ]
    }
  }

}

【问题讨论】:

    标签: elasticsearch date-range


    【解决方案1】:

    我最终找到了两种方法来完成我所需要的:

    GET logstash-*/tcp_input/_count?q=stack_trace: *WebServiceException* AND rawmsg: "Could not send Message" AND @timestamp: [ now-30d TO now ]
    
    and
    
    GET logstash-*/_count
    {
      "query": {
        "query_string": {
          "query": """stack_trace: *WebServiceException* AND rawmsg: "Could not send Message" AND @timestamp: [  now-3d  TO now]""",
          "analyze_wildcard": true
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您使用的是should 子句而不是must 子句,它有效地将您的条件与OR 而不是AND 结合起来。

      对于范围查询,时间戳也应该是now/d-1d

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-24
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        • 2020-09-21
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        相关资源
        最近更新 更多