Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value of minimum_should_match?

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?

like image 694
lapinkoira Avatar asked Feb 26 '18 09:02

lapinkoira


1 Answers

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.

like image 74
Nikolay Vasiliev Avatar answered Nov 15 '22 19:11

Nikolay Vasiliev