Equivalent of a maximum_should_match?


(Wong CS) #1

I have a "should" block in my bool query that contains 3 conditions. It would match anything that matches any 1 or more of the 3 conditions. But I would like to exclude anything that matches all 3 conditions, i.e. only match 1 or 2 of the conditions. Is there something like a maximum_should_match that would give me the equivalent of this?

Thanks
Wong


(Wong CS) #2

I've found out that I can construct my query using query_string, which looks something like this:

(ResponseQ1:0 OR ResponseQ2:0 OR ResponseQ3:<skipped>) AND NOT (ResponseQ1:0 AND ResponseQ2:0 AND ResponseQ3:<skipped>)


(Nik Everett) #3

You can also do the same with a bool query and a must_not clause containing a should clause. Like:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [ your queries ],
       "must_not": [
        "bool": {
          "minimum_should_match": 3, <---- this is your maximum_should_match
          "should": [ your queries again ]
        }
      ]
    }
  }
}

Its the same as your query_string way but keeps you in the DSL. Its not as good as having native maximum_should_match though because it has to evaluate the queries twice. But if it is fast enough then it is fast enough.


(Wong CS) #4

I got a parse error running this search in Kibana. I think the DSL doesn't allow you to embed another "bool" query inside any of the higher level "bool"'s subconditions... I did try some variation of that in my earlier attempts and that's what stopped me.


(system) #5