Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 907
rodney757 Avatar asked Nov 22 '16 16:11

rodney757


1 Answers

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

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "nestedArray",
            "query": {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "nestedArray"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
like image 182
ChintanShah25 Avatar answered Sep 20 '22 19:09

ChintanShah25