2

What is the elastic search query to search for documents where "field1 is null OR field2 is null"..

I am using elasticSearch 5.3...

Selva
  • 21
  • 1
  • 1
  • 4

2 Answers2

7

This query did work for me:

curl -XGET "http://localhost:9200/my_null_val/_search?pretty" -d '
{
  "query": {
    "bool":{
      "must_not":{
        "bool": {
          "must": [
            {"exists" : { "field" : "field1" }},
            {"exists" : { "field" : "field2" }}
          ]
        }
      }
    }
  }
}'

As a hint you can think of field1 is null **OR** field2 is null to be equivalent expression to NOT (field1 is not null **AND** field2 is not null).

It is also known as De_Morgan's law.

Pavel Vasilev
  • 1,002
  • 1
  • 10
  • 11
1

You could also get away with using a should clause in your bool filter.

    "bool": {
      "should": [{
        "bool": {
          "must_not": {
            "exists": {
              "field": "field1"
            }
          }
        }
      }, {
        "bool": {
          "must_not": {
            "exists": {
              "field": "field2"
            }
          }
        }
      }]
    }
Nicolás Fantone
  • 2,055
  • 1
  • 18
  • 24