Search document with empty array field, on ElasticSearch Search document with empty array field, on ElasticSearch elasticsearch elasticsearch

Search document with empty array field, on ElasticSearch


If address is a mandatory field in location array you can modify your query:

"must_not": {  "missing": {    "field": "locations.address"  }}

AFAIK, in ES you cannot query non-leaf elements (like your location field) (see issue), and in case object types ES flattens the nested fields (see nested type, object type). That's why I suggested to query for one of the leaf elements instead. But it requires that one of them is mandatory (which is unfortunately not satisfied in your case).

Anyway I found the solution using the _source parameter inside the source_filtering:

"must_not": {  "script": {    "script": "_source.locations.size() > 0"  }}

Note that using "lang":"groovy" you should write: "script": "_source.locations.size > 0"


If you don't want to enable scripting, you can combine the Exists Query with a must_not bool query, e.g:

{  "query":{    "bool":{      "must_not":[        {          "exists":{            "field":"tags"          }        }      ]    }  }}


it looks like a similar question has been answered, I did not test the solution, but you could give it a try: >> elasticsearch filtering by the size of a field that is an array.