Elasticsearch Elasticsearch Function Score: Boosting Relevance with Custom Scoring

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Introduction

Function Score is a powerful query in Elasticsearch that allows you to modify the relevance score of documents returned by a query. This is particularly useful when you want to boost the importance of certain documents based on specific criteria, such as recency, popularity, or any other custom factors. In this article, we will dive into the details of the Function Score query and explore how to use it effectively to improve search results.

Understanding Function Score Query

The Function Score query combines the results of a base query with one or more functions that compute a new score for each document. These functions can be based on various factors, such as field values, distance, or even random values. The final score of a document is calculated by combining the original score from the base query with the scores produced by the functions, using a specified score mode.

Here’s a basic structure of a Function Score query:

{
  "query": {
    "function_score": {
      "query": { ... }, // Base query
      "functions": [ ... ], // Array of functions
      "score_mode": "multiply", // How to combine function scores
      "boost_mode": "multiply" // How to combine function scores with the base query score
    }
  }
}

Function Types

There are several types of functions that can be used in a Function Score query:

1. Field Value Factor: This function modifies the score based on the value of a specific field in the document. You can apply a factor, a modifier (such as log, square, or sqrt), and a missing value to handle documents without the specified field.

2. Decay Functions: Decay functions (linear, exp, and gauss) allow you to reduce the score of documents based on the distance from a specific value in a numeric, geo_point or date field. You can control the rate of decay using the scale, offset, and decay parameters.

3. Script Score: This function allows you to write a custom script (using Painless or another scripting language) to calculate the score based on the document’s fields and other parameters.

4. Random Score: This function assigns a random score to each document, which can be useful for testing or promoting diversity in search results.

5. Weight: This function assigns a static weight to the documents that match the filter, which can be useful for boosting specific documents or groups of documents.

Using Function Score Query

Let’s look at some examples of using Function Score queries to boost search results based on custom criteria.

Example 1: Boosting documents based on recency

Suppose you have an index of blog posts, and you want to boost the relevance of more recent posts. You can use a decay function to achieve this:

{
  "query": {
    "function_score": {
      "query": {
        "match": { "title": "elasticsearch" }
      },
      "functions": [
        {
          "gauss": {
            "publish_date": {
              "origin": "now",
              "scale": "30d",
              "offset": "7d",
              "decay": 0.5
            }
          }
        }
      ],
      "score_mode": "multiply"
    }
  }
}

In this example, we use a gauss decay function to reduce the score of documents based on the publish_date field. The origin is set to “now”, and the scale, offset, and decay parameters control the rate of decay.

Example 2: Boosting documents based on popularity

If you want to boost the relevance of more popular blog posts, you can use the field_value_factor function:

{
  "query": {
    "function_score": {
      "query": {
        "match": { "title": "elasticsearch" }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "views",
            "factor": 1.2,
            "modifier": "sqrt",
            "missing": 1
          }
        }
      ],
      "score_mode": "multiply"
    }
  }
}

In this example, we use the field_value_factor function to boost the score of documents based on the views field. The factor, modifier, and missing parameters control the impact of the field value on the score.

Conclusion

Elasticsearch Function Score is a versatile tool for customizing the relevance of search results based on various factors. By understanding the different function types and how to combine them, you can create powerful search experiences that cater to your specific use cases and requirements.