栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch学习笔记:MUST

Elasticsearch学习笔记:MUST

环境

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/707531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号