katya de la Rosa
katya de la Rosa

Reputation: 1

How to search by an specific value or where field does not exists in Elasticsearch

Im trying to do a query where the field does not exists or is in the array of values, in mongo it would be something like this.

$or: [
    {
        field: {
            $exists: false
        }
    },
    {
        field: [val1, val2]
    }
]

I've tried with a couple of variations but cant seem to find the solution for this.

Edit1:

Basically on my mind, the query should look like.

query: {
  "bool": {
    "should": [
        "must": {...logic}
        "must_not": {...logic}
     ]
  }
}

this returns

"must_not" malformed

Upvotes: 0

Views: 717

Answers (1)

Val
Val

Reputation: 217594

You need to do it like this:

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "field": [
              "val1",
              "val2"
            ]
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "field"
              }
            }
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions