stefanobaldo
stefanobaldo

Reputation: 2063

Elasticsearch - Match string OR empty value

I have to check if a field matches a specific text OR is empty. Is it possible to do that?

Thank you.

Upvotes: 3

Views: 1147

Answers (1)

Mattias Nordberg
Mattias Nordberg

Reputation: 1778

You can achieve this by using the missing filter. For example:

POST /my_index/items
{
    "field1": "value1"
}

POST /my_index/items
{
    "field1": "value2"
}

POST /my_index/items
{
    "field1": ""
}

POST /my_index/_refresh

POST /my_index/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "or": {
               "filters": [
                  {
                     "term": {
                        "field1": "value1"
                     }
                  },
                  {
                     "missing": {
                        "field": "field1"
                     }
                  }
               ]
            }
         }
      }
   }
}

Upvotes: 6

Related Questions