6

I test with these 2 queries

Query with must

 {
  "size": 200,
  "from": 0,
  "query": {
  "bool": {
  "must": [ {
      "match": {
        "_all": "science"
      }
    },
    {
      "match": {
        "category": "fiction"
      }
    },
    {
      "match": {
        "country": "us"
      }
    }
   ]
 }
}

}

Query with should + minimum_should_match

  {
   "size": 200,
   "from": 0,
   "query": {
   "bool": {
   "should": [ {
       "match": {
         "_all": "science"
       }
     },
     {
      "match": {
        "category": "fiction"
        }
      },
      {
      "match": {
        "country": "us"
        }
      }
    ],
     minimum_should_match: 3
  }
 }
}

Both queries give me same result, I don't know the difference between these 2, when we should use minimum_should_match?

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
plchia
  • 113
  • 1
  • 1
  • 7

2 Answers2

10

I guess you mean minimum_number_should_match, right?

In both cases it would be the same because you have the same number of clauses in should. minimum_number_should_match usually is used when you have more clauses than the number you specify there.

For example if you have 5 should clauses, but for some reason you only need three of them to be fulfilled you would do something like this:

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tag": "wow"
          }
        },
        {
          "term": {
            "tag": "elasticsearch"
          }
        },
        {
          "term": {
            "tag": "tech"
          }
        },
        {
          "term": {
            "user": "plchia"
          }
        },
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      "minimum_should_match": 3
    }
  }
}
Antonio Val
  • 3,200
  • 1
  • 14
  • 27
  • Thanks for the explanation,but I still don't really get it why and when to use minimum_should_match, says I have 5 should clauses, and to set minimum_should_match = 2, what I understand those 2 to match I can put into must clause so that the should clause will leave those parameter that should match only, in what use cases will the minimum_should_match should be apply? – plchia Mar 16 '17 at 16:29
  • Well just suppose you need to fulfil only 2 of those 5 conditions; if you just make a must clause with those 2 conditions, your search result is going to be smaller. Imagine you are looking for users that are have skills in C, C++, Java, Javascript and PHP; but just fulfilling two of those is enough for you. – Antonio Val Mar 18 '17 at 06:37
6

That's correct and desired behavior. Let's decipher it a little bit:

  • Boolean query with must clauses means that all clauses under must section are required to match. Just like in English - it means strong obligation.
  • Boolean query with should clauses means that some clauses are required to match, whereas the others are not (i.e. soft obligation). The default number of clauses that must match here is simply 1. And to override this behavior the minimum_should_match parameter is coming into play. If you specify minimum_should_match=3 it will mean 3 clauses under should must match. From the practical perspective it exactly the same as specifying those clauses with must.

Hope it explains it in details.

Pavel Vasilev
  • 1,002
  • 1
  • 10
  • 11
  • 4
    As of Elasticsearch 7.0, it isn't so intuitive anymore. A `bool` query with only a `should` clause still has a default `minimum_should_match` of 1. But a `bool` query with both a `must` and a `should` clause has a `minimum_should_match` of 0! See https://stackoverflow.com/a/49012705/502240 – bcody Feb 05 '21 at 10:10