0

Hi I have this document in ES with nested type:

{
         "id": "92210f7f-b8a4-4d55-877d-8708154aa004",
          "additionalData": {

              "devices_nested": [
                {
                  "version_string": "1"
                },
                {
                  "os_string": "Windows",
                  "version_string": "3"
                },
                {
                  "os_string": "Centos"
                }
              ]

          }

I want to do query that additionalData.devices_nested does not contain any element where os_string property does not exist that means I want to avoid such documents where some entries could have or not os_string property. Here is my query:

{
  "query": {
    "nested": {
      "query": {
        "bool": {
          "must": {
            "exists": {
              "field": "additionalData.devices_nested.os_string"
            }
          }
        }
      },
      "path": "additionalData.devices_nested"
    }
  }
}

But I always get the example document as result because at least one element satisfies query that there is os_string property. Is it possible to make query which will return document where all elements in devices_nested has os_string property?

1 Answer 1

1

Is it possible to make query which will return document where all elements in devices_nested has os_string property?

Yes, it is posible. Instead of must exist, you have to use must_not missing approach.

In following query, the bool condition inside nested will match all documents that do not have os_string field in at least one of the nested objects, and then, the outside must_not query will exclude these documents. As a result, you'll get only documents that include os_string field in all nested objects:

{
  "query": {
    "bool": {
        "must_not": {
            "nested": {
              "query": {
                "bool": {
                    "must_not": {
                        "exists": {
                            "field": "additionalData.devices_nested.os_string"
                        }
                    }
                }
              },
              "path": "additionalData.devices_nested"
            }
        }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.