20

Cant find which is the default value of minimum_should_match in the docs

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html

Is it 0 or 1, or depends if the query has just should or filter context?

lapinkoira
  • 8,320
  • 9
  • 51
  • 94

1 Answers1

49

The default value of minimum_should_match depends on the query and on the context:

  • 1: in query context and should is alone (no must or filter)
  • 1: in filter context (e.g. inside a filter part of a bool query; true until ES 6.7)
  • 0: in filter context (e.g. inside a filter part of a bool query; true since ES 7.0, see notes below)
  • 0: in query context and there are must and should (or filter and should)

Can be found in the documentation of the bool query:

If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.

A few examples

query context and should is alone:

POST _search
{
  "query": {
    "bool" : {
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ]
      # default:
      # "minimum_should_match" : 1
    }
  }
}

query context and must together with should:

POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ]
      # default:
      # "minimum_should_match" : 0
    }
  }
}

filter context:

POST _search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": {
            "term" : { "user" : "kimchy" }
          },
          "should": [
            { "term" : { "tag" : "wow" } },
            { "term" : { "tag" : "elasticsearch" } }
          ]
          # default (until ES 6.7):
          # "minimum_should_match" : 1
        }
      }
    }
  }
}

Update: ES 7.0 related changes

In Elasticsearch 7.0 the filter context has been removed, which means effectively that in filter context its default value now is 0.

Thanks to this answer which helped me to discover this.

Nikolay Vasiliev
  • 5,656
  • 22
  • 31
  • Hi Nikolay, thanks for your answer! Would you have any clue as to how the score would be calculated in a bool query with must and should clauses? I mean the score for ranking the result as per https://www.elastic.co/guide/en/elasticsearch/guide/current/practical-scoring-function.html – s1d Dec 11 '20 at 14:50
  • @s1d No, unfortunately I don't have such knowledge, I would also be curious to learn this. I do believe that the should be no difference in scoring between `should` and `must`, the latter filter only those results that match its query in any way, and should does not filter (only increases the score for those also matching the `should`). – Nikolay Vasiliev Dec 13 '20 at 20:20
  • Thank you for posting this, saved me a lot of time. – Nikolay Klimchuk Apr 20 '21 at 19:01