Elasticsearch – combine ids query with must_not clause

2.1k views Asked by At

I’d like to combine ids query with must_not clause. I’m using (re)tire gem with rails application.

I’m doing something like this:

query do
  ids ["foo", "bar", "test"]
end

Which works. However, when I’m trying to combine it with must_not clause, I’m getting error – must_not works only withing boolean scope, which does not include ids query. Is there any way to combine that two queries?

1

There are 1 answers

0
Andrei Stefan On

The ES query that you need to use is this one:

GET /some_index/some_type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "values": ["foo", "bar", "test"]
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "name": "whatever"
          }
        }
      ]
    }
  }
}

So, you need boolean scope, a must with your ids and a must_not with your must not match queries.