rodney757
rodney757

Reputation: 583

ElasticSearch: check if nested object array is empty

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

Upvotes: 17

Views: 16250

Answers (2)

Kunwar Shukla
Kunwar Shukla

Reputation: 25

Try this

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

Upvotes: 0

ChintanShah25
ChintanShah25

Reputation: 12672

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

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

Upvotes: 32

Related Questions