Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does actually minimum_should_match in percentage work for query search?

I would to understand more how minimum_should_match works in elasticsearch for a a query search

GET /customers/_search
{
  "query": {
     "bool": {
        "must":[
           {
           "query_string":{
              "query": "大月亮",
              "default_field":"fullName",
              "minimum_should_match": "70%" ------> experimented with this value
           }
        }
      ]
    }
  }
}

I experimented with the percentage in the query and I can see I get different results for Chinese language ?

I tried reading the documentation but did not clearly understand how does this option work ?

like image 365
Sorin Penteleiciuc Avatar asked Mar 03 '23 13:03

Sorin Penteleiciuc


1 Answers

The minimum_should_match parameter works for "should" clauses in the "bool" query. With this parameter you specify how many of the should clauses must match for a document to match the query.

Consider the following query:

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } },
        { "term" : { "tag" : "stackoverflow" } }
      ],
      "minimum_should_match" : 2,
      "boost" : 1.0
    }
  }
}

Here a document will only be a match if minimum 2 should clauses match. This means if a document with both "stackoverflow" and "wow" in the "tags" field will match, but a document with only "elasticsearch" in the tags field will not be considered a match.

When using percentages, you specify the percentage of should clauses that should match. So if you have 4 should clauses and you set the minimum_should_match at 50%, then a document will be considered a match if at least 2 of those should clauses match.

More about minimum_should_match can be found in the documentation. There you can read it's for "optional clauses", which is "should" in a "bool" query.

like image 142
Daantie Avatar answered Apr 27 '23 05:04

Daantie