本文介绍了获取所有不包含密钥的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有2种类型的文档,例如

For example,i have 2 type docs,such as

{
    "field2":"xx",
    "field1","x"
}
{
    "field1","x"
}

一个具有2个字段( field1 field2 ),另一个只有1个字段( field1 ).

The one has 2 fields(field1 and field2),another one just has 1 field(field1).

现在,我想查询所有没有 field2 字段的文档吗?

Now,i want to query all docs which do not have field2 field?

EIDT dsl:

 {
    "query": {
        "bool": {
            "filter": [
                {
                    "exists": {
                        "field": "LableToMember"
                    }
                }
            ]
        }
    }
}

文档:

{
    "LableToMember": [
        {
            "xxx": "xxx",
            "id": "1"
        }
    ],
    "field2":"xxx"
}  

LableToMember 是一个嵌套字段.我发现 exists api不能用于嵌套字段?

LableToMember is a nested field.I find exists api can't be used for nested field?

推荐答案

请注意,在ES 5.x中, missing 查询已删除,而支持 exists .

Note that in ES 5.x the missing query has been removed in favor of the exists one.

因此,如果要实现前向兼容,则应首选使用以下方法:

So if you want to be forward compatible, you should prefer using this:

POST /_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "field2"
                }
            }
        }
    }
}

更新

如果要检索所有没有 field2 或具有给定值的 field2 的文档,则可以这样操作:

If you want to retrieve all docs which don't have field2 or have field2 with a given value, you can do it like this:

POST /_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "field2"
              }
            }
          }
        },
        {
          "term": {
            "field2": "somevalue"
          }
        }
      ]
    }
  }
}

这篇关于获取所有不包含密钥的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:57