user2813807
user2813807

Reputation: 309

Elasticsearch: search for field null OR in list

I would like to write something like this in ElasticSearch:

SELECT * 
FROM ...
WHERE name IS NULL OR name IN ("a","b","c");

I can write the "IS NULL" part using:

{
    "query" :
    {
      "bool" : {
      "must_not": {
        "exists": {
          "field": "name"
        }
      }
    }
  }     
}

The "IN list" part:

{
    "query" :
    {
      "bool" : {
          "should" : [
            {
              "terms" : {
                "name" : [
                  "a", "b", "c"
                ]
              }
            }
          ]
      }
   }    
}

But I can't find a way to merge these two queries using a OR (and not a AND of course).

Thanks

Upvotes: 2

Views: 636

Answers (1)

Val
Val

Reputation: 217594

You can use bool/should in order to combine both

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "name": [
              "a",
              "b",
              "c"
            ]
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "name"
              }
            }
          }
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions