3

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

Thank you.

1 Answer 1

6

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"
                     }
                  }
               ]
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.