ElasticSearch 过滤器不能使用 must AND must_not

ElasticSearch Filter not working with must AND must_not

我正在尝试过滤掉 Kibana 中的错误,但不是所有错误,只是消息字段中没有特定字符串的错误。我的查询如下:

GET search-apps/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "@timestamp": {
                  "gte": "now-120m",
                  "lte": "now"
                }
              }
            },
            {
              "term": {
                "level": "error"
              }
            }
          ],
          "must_not": [
            {
              "term": {
                "message": "Exception thrown while fetching records from Kinesis"
              }
            }
          ]
        }
      }
    }
  }
}

过滤器似乎忽略了 "must_not",因为我仍然收到消息字段中包含该字符串的错误。是否存在某种忽略 must_not 的执行顺序? must 工作正常,我只得到 level:error returned,但我也得到消息中包含该字符串的结果。 如果我只想 return 结果与消息字段:

{
  "query": {
    "match": {
      "message": {
        "query": "Exception thrown while fetching records from Kinesis",
        "type": "phrase"
      }
    }
  }
}

但是,相反的方法并不像建议的那样工作。

您的问题已包含 "problem" 的解决方案。

在您的 must_not-子句中,您使用的是 term-查询,它测试完全匹配,而在您的 "test"-查询中,您使用 phrase-询问。如果您的消息包含 短语 ,但也包含一些额外的 bytes/text,则 term 查询不再匹配。

解决方案 只需在 bool-查询中用 match_phrase 替换 term