How to tell the "exists"-query to include empty fields

Hello =)

I am using elastic search in version 5.6.4 and want to find entries where a field "tags" is existent. One rule of this query is "At least one non- null value is required." However, I want to treat empty fields as existent too. Here is a tiny example:

DELETE days

POST days/day
{
  "name": "monday"
}

POST days/day
{
  "tags": []
}

POST days/day
{
  "tags": [""]
}

POST days/day
{
  "tags": ["sunny"]
}


GET days/_search
{
  "query": {
    "exists": {
      "field": "tags"
    }
  }
}

So basically I want to find all entries except the first one, where this field is really not there.

Is there another query to achieve this? Because I find it a bit odd to have empty strings as placeholders.

My advice is to compute this at index time.
You can use an ingest Script for example and compute a isEmpty field which you can index within your document.
Or a tag_size number field..

1 Like

Thank you, I am always thinking too complicated. Your ideas led me to the following solution: I fill those fields by myself and on this spot, I add a boolean saying if this field exists or not, lol >_> Silly me.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.