17

How do I go about fetching all documents w/o any objects in a field?

I have the following mapping:

"properties" : {
  "name": {
    "type": "text"
  },
  "nestedArray": {
    "type": "nested",
    "properties": {
      "prop1": {
        "type": "text"
      },
      "prop2": {
        "type": "text"
      }
    }
  }
}

and I want to get all documents where "nestedArray" is empty or doesn't exist.

I'm using elasticSearch 5.0

2 Answers 2

32

I think exists query would solve this problem. Try following query

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "nestedArray",
            "query": {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "nestedArray"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I was trying the exists query, but I had the nested and bool switched.
glad I could help
Thank you! I spent almost an hour looking for this answer.
thank you. To me it is pretty confusing to use the nested attribute here as I just want to check whether the array is empty and not if one the inside objects value.
0

Try this

{
  "size": 5000,
  "query": {
    "bool": {
      "filter": [],
      "must_not": [
        {
          "nested": {
            "path": "nestedArray",
            "query": {
              "exists": {
                "field": "nestedArray"
              }
            }
          }
        }
      ]
    }
  },
  "from": 0
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.