ES查询must_not异常排查

问题:

"error": { "root_cause": [ { "type": "x_content_parse_exception", "reason": "[71:9] [bool] failed to parse field [must_not]" } ], "type": "x_content_parse_exception", "reason": "[71:9] [bool] failed to parse field [must_not]", "caused_by": { "type": "illegal_state_exception", "reason": "expected value but got [START_ARRAY]

根据提示,发现查询条件中,must_not 下有如下写法:

"must_not": [[{"term": { "hotelId": 3255071 }}]]

背景:

最近运维新建了es集群

结论:

1、排查发现最新的es版本是7.10.0  , 而之前的是7.0.1 版本。

2、换成如下写法就没问题了

 "must_not": [
           {
            "term": {
              "hotelId": 325507
            }
          }
      ]

3、在 Elasticsearch 的最新版本中,[must_not] 子句需要使用范围查询或布尔查询来指定一个或多个条件。 

————————————————————————————

Elasticsearch 是一个流行的搜索引擎,它支持复杂的查询和过滤功能。在 Elasticsearch 的查询语句中,[must_not] 子句用于否定一个或多个查询子句。

以下是一些关于 [must_not] 子句的问题和答案:

问题1:[must_not] 子句是否支持数组嵌套数组?

答案1:是的,[must_not] 子句在 Elasticsearch 的所有版本中都支持数组嵌套数组。例如,以下查询语句将返回所有包含 "foo" 或 "bar" 字段值的文档,但不包含任何包含 "baz" 字段值的文档:

```
GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "field1": "foo" } },
        { "match": { "field2": "bar" } }
      ],
      "must_not": {
        "terms": { "field3": ["baz", "qux"] }
      }
    }
  }
}
```

在这个查询语句中,[must_not] 子句使用了 terms 查询来匹配一个包含多个元素的数组。这个查询将返回所有包含 "foo" 或 "bar" 字段值的文档,但不包含任何包含 "baz" 或 "qux" 字段值的文档。

问题2:[must_not] 子句是否支持范围查询或布尔查询?

答案2:是的,[must_not] 子句需要使用范围查询或布尔查询来指定一个或多个条件。以下是一个示例查询,它将返回所有不包含 "hotelId": 3255071 的文档:

```
GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "field1": "foo" } },
        { "match": { "field2": "bar" } }
      ],
      "must_not": {
        "bool": {
          "must_not": [
            { "term": { "hotelId": 3255071 } }
          ]
        }
      }
    }
  }
}
```

在这个查询语句中,[must_not] 子句使用了 bool 查询来指定一个条件。这个查询将返回所有不包含 "hotelId": 3255071 的文档。

GET /my_index/_search
{
  "query": {
    "bool": {
     "must": [
        { "match": { "field1": "foo" } },
        { "match": { "field2": "bar" } }
      ],
     "must_not": {
        "bool": {
         "must_not": [
            { "term": { "hotelId": 3255071 } },
            { "term": { "hotelId": 325507 } }
          ]
        }
      }
    }
  }
}

或者:

"must_not": [
           {
            "term": {
              "hotelId": 325507
            }
          },
           {
            "term": {
              "hotelId": 14550
            }
          }
      ]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值