Querying for a empty array

I have a ES index with a nested field called “relations”. In the mapping, it is defined as:

 "relations": {
          "type": "nested",
          "properties": {
            "primaryWordIndex": {
              "type": "long"
            },
            "secondaryWordIndex": {
              "type": "long"
            },
            "score": {
              "type": "float"
            }
          }
        },

In my index I have 3 types of documents:

  • Documents that do not have the “relations” field at all
  • Documents that have a “relations” field filled with a empty array
  • Documents that have a properly filled “relations” field.

I now want to query for the first and second occurrence. I was using the following query:

GET /myindex/_search

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

But that returns both records that do not have a relation fields and records that have an empty array as relations field.

I tried a filter like this to get records where the "relations" field is a empty array:

"filter" : {
    "script" : {
        "script" : "doc['relations'].values.length == 0"
    }
}

but that returned. error:

"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "No field found for [relations] in mapping"
}

Any idea how I should change my query?
I'm using Elasticsearch 7.17.0

Hi!!

You want only docs with empty array?

Yes, I want to find out what records have a empty array

An indexed value may not exist for a document’s field due to a variety of reasons:

  • The field in the source JSON is null or []

That's why when you filter through "exists" you will get doc with null field and empty array.

Maybe you need check empty values in your application.

Check this:

The issue is that I have an application the retrieves the records from ES, adds the relations fields and writes them back to ES. Somewhere in that process, a issue occurred and some records got an empty array as relations field.

I would like to query those records with a empty array so I can figure out how many it are and find the cause of the issue.

From the documentation you will have the total number of documents with an empty array and who does not have the field. In your application you will have to know who has the empty and null array.