Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: should + minimum_should_match vs must

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?

like image 487
plchia Avatar asked Mar 16 '17 10:03

plchia


People also ask

What is the difference between must and should in Elasticsearch?

Using must_not tells Elasticsearch that document matches cannot include any of the queries that fall under the must_not clause. should – It would be ideal for the matching documents to include all of the queries in the should clause, but they do not have to be included. Scoring is used to rank the matches.

Should minimum should match?

Minimum Should Match is another search technique that allows you to conduct a more controlled search on related or co-occurring topics by specifying the number of search terms or phrases in the query that should occur within the records returned.

What is bool in Elasticsearch?

The bool query is a go-to query because it allows you to construct an advanced query by chaining together several simple ones. The results must match the queries in this clause. If you have multiple queries, every single one must match. Acts as an and operator.

What is bool query?

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery . It is built using one or more boolean clauses, each clause with a typed occurrence.


1 Answers

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
    }
  }
}
like image 51
Antonio Val Avatar answered Sep 20 '22 11:09

Antonio Val