Elasticsearch学习笔记:MUST_NOT not working with EXIST in NESTED query

环境

elasticsearch:6.7

嵌套查询中字段不存在

如下:

POST players/players/_search
{
   "query": {
      "bool": {
         "filter": [
            {
               "nested": {
                  "path": "features",
                  "query": {
                     "bool": {
                        "must_not": [
                           {
                              "exists": {
                                 "field": "features" // 或者"field": "features.comment"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

我们知道查询某个字段不存在,官方的文档:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "user"
                }
            }
        }
    }
}
'

所以我们也就会自然而然的在嵌套查询里把其加上类似的写法。

但是老外的评论里,给出的写法是:

If instead of putting the must_not inside your nested query, you put the nested query inside of a must_not, it should work:
中文是说:不是将must_not放到嵌套查询中里,而是将嵌套查询放到must_not中。

POST players/players/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "features",
            "query": {
              "exists": {
                "field": "features"
              }
            }
          }
        }
      ]
    }
  }
}

我的理解

must_not放到嵌套查询中,会有个逻辑问题。嵌套查询是通过path来指定路径的。我们可以抽象的理解为:你已经进入了房间,却要在房间里判断这个房间是否存在。要判断房间是否存在应该在房间的外面才行。

参考地址:

MUST_NOT not working with EXIST in NESTED query

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值