Default value of minimum_should_match?

后端 未结 1 748
半阙折子戏
半阙折子戏 2021-02-07 07:43

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-s

相关标签:
1条回答
  • 2021-02-07 08:28

    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.

    0 讨论(0)
提交回复
热议问题