1. Overview

In the world of search engines and information retrieval, ElasticSearch has emerged as a powerful and versatile tool for indexing and searching large volumes of data. As developers and users, we often find ourselves working with complex queries to retrieve relevant documents based on specific criteria. One of the critical aspects of crafting effective queries in ElasticSearch is understanding and utilizing the minimum_should_match parameter.

In this tutorial, we’ll look at the minimum_should_match parameter and how it influences the behavior of Boolean queries in ElasticSearch.

2. Understanding Boolean Queries in ElasticSearch

Before we move on to the specifics of minimum_should_match, it’s fairly important to have a solid understanding of Boolean queries in ElasticSearch. Boolean queries are a fundamental concept in ElasticSearch and form the basics for more complex types. It enables us to combine multiple query conditions using logical operators to create more precise and targeted searches.

2.1. mustshould, and must_not Clauses

In ElasticSearch, Boolean queries consist of three main clauses:

  • must
  • should
  • must_not

Each clause plays a specific role in determining how the query matches documents:

  • must specifies conditions that a document must satisfy for us to consider it a match, equivalent to the logical AND operator
  • should specifies conditions that a document should ideally satisfy, but they aren’t mandatory, equivalent to the logical OR operator
  • must_not specifies conditions that a document must not satisfy for us to consider it a match, equivalent to the logical NOT operator

Knowing these are the main clauses of Boolean queries and equipped with their meaning, we can move on to practical examples.

2.2. How Boolean Queries Work in ElasticSearch

Moreover, when we construct a Boolean query in ElasticSearch, we combine the mustshould, and must_not clauses to define the desired matching criteria. ElasticSearch evaluates each clause independently and then combines the result based on the logical operators.

Let’s look at an example of a Boolean query that searches for documents containing the term elasticsearch in the title field and search or query in the content field:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "elasticsearch"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "search"
          }
        },
        {
          "match": {
            "content": "query"
          }
        }
      ]
    }
  }
}

In the code snippet above, the must clause ensures that the term elasticsearch is present in the title field, while the should clauses enable either search or query to be present in the content field. Therefore, the result set contains documents that satisfy both the must and at least one of the should clauses.

Notably, we should understand how Boolean queries work to utilize the minimum_should_match parameter effectively.

3. The minimum_should_match Parameter

The minimum_should_match parameter plays a crucial role in controlling the behavior of should clauses within a Boolean query.

In particular, the minimum_should_match parameter specifies the minimum number of should clauses that a document must match for us to consider it a hit. This way, it enables us to fine-tune the balance between precision and recall in the search results. By setting a higher value for minimum_should_match, we can increase the precision of the queries, ensuring that only documents that match a sufficient number of should clauses are returned.

Conversely, a lower value for minimum_should_match can improve recall by allowing documents that match fewer should clauses to be in the result set.

However, if we don’t specify the minimum_should_match parameter by default, ElasticSearch treats the should clauses as optional. This means that ElasticSearch considers a given document as a match even if it doesn’t satisfy any of the should clauses, as long as it matches all the must clauses (if any) and doesn’t match any of the must_not clauses.

For example, let’s consider a fairly simple query:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "elasticsearch"
          }
        },
        {
          "match": {
            "content": "search"
          }
        }
      ]
    }
  }
}

In this case, if we didn’t specify minimum_should_match, a document that matches either the title or the content field should be in the result set. However, documents that don’t match any of the should clauses are also be returned, as long as they satisfy other query conditions.

Yet, we can use the minimum_should_match parameter to enforce a minimum number of should clause matches. For instance, setting minimim_should_match to 1 would require us to consider a document that matches at least one of the should clauses a hit.

In the next section, we explore how to configure minimum_should_match and how to leverage its flexibility to suit various search scenarios.

4. Configuring minimum_should_match

The minimum_should_match parameter offers flexibility in its configuration, enabling us to adapt it to different search requirements. Let’s explore the various formats available for specifying minimum_should_match.

4.1. Percentage Format

One way we can set minimum_should_match is by using a percentage value. We use this format to specify the minimum number of should clauses that the query must match as a percentage of the total number of should clauses in the query.

For example, let’s consider a slightly modified query:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "elasticsearch"
          }
        },
        {
          "match": {
            "content": "search"
          }
        },
        {
          "match": {
            "author": "john"
          }
        }
      ],
      "minimum_should_match": "50%"
    }
  }
}

In the code snippet above, we set minimum_should_match to 50%. Thus, we require that at least half of the should clauses (rounded down) must be matched for a document to be considered a hit. Thus, if a document matches two out of the three should clauses, it becomes part of the result set.

4.2. Numeric Format

Moreover, another way we can configure minimum_should_match is by using an integer value. We use this format to specify the exact number of should clauses that must match:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "elasticsearch"
          }
        },
        {
          "match": {
            "content": "search"
          }
        },
        {
          "match": {
            "author": "john"
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

Here, we set minimum_should_match to 2. Consequently, we require that at least two out of the three should clauses must match for us to consider the document a hit.

4.3. Combination Format

Additionally, ElasticSearch also supports a combination format for minimum_should_match. It enables us to specify different criteria based on the number of should clauses present. This format is useful when dealing with queries that have a variable number of should clauses.

Moreover, the combination format follows a specific pattern:

[criteria]-[criteria]-...

Each criterion can be either a percentage or a numeric value. ElasticSearch evaluates the criteria from left to right until a matching condition is found:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "elasticsearch"
          }
        },
        {
          "match": {
            "content": "search"
          }
        },
        {
          "match": {
            "author": "john"
          }
        }
      ],
      "minimum_should_match": "2<75%"
    }
  }
}

In the code snippet above, the combination format 2<75% means one of two conditions, in order:

  • if the number of should clauses is less than 2, then all of them must match
  • if the number of should clauses is 2 or more, then at least 75% of them must match

This way, we can have a more flexible criterion.

5. Real-World Examples and Use Cases

Let’s look at some real-world examples and use cases where we can apply this parameter to solve common search challenges.

In an ecommerce scenario, we can leverage minimum_should_match to improve the relevance of product search results. Let’s consider an online store that enables users to search for products based on multiple criteria, such as name, description, brand, and category:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "running shoes"
          }
        }
      ],
      "should": [
        {
          "match": {
            "brand": "Nike"
          }
        },
        {
          "match": {
            "category": "sports"
          }
        },
        {
          "match": {
            "description": "lightweight"
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

In the code snippet above, the search query looks for products with running shoes in the name field. Additionally, it specifies three should clauses to match the brand Nike, the category sports, and the term lightweight in the description. By setting minimum_should_match to 2, we require that at least two of these additional criteria must be matched for a product to be considered relevant.

5.2. Full-Text Search in CMS

Furthermore, another common use case for minimum_should_match is in a content management system (CMS) that supports full-text search.

Within a CMS, users often search for articles, blog posts, or documents based on multiple keywords or phrases:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "ElasticSearch"
          }
        },
        {
          "match": {
            "content": "search engine"
          }
        },
        {
          "match": {
            "tags": "open source"
          }
        }
      ],
      "minimum_should_match": "60%"
    }
  }
}

In this scenario, the search query aims to find articles that contain the term ElasticSearch in the title, and search engine in the content. In addition, we search for articles that have the tag open source. By setting the minimum_should_match to 60%, we require that at least 60% of these criteria must be met for us to consider an article a match.

6. Conclusion

In this article, we looked at the minimum_should_match parameter in ElasticSearch. Specifically, we found out that it controls how many terms must match for a result. Finally, we looked at its configuration and some real-world examples and use cases.