Type to search.

Score context

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Use a Painless script in a function score to apply a new score to documents returned from a query.

For help debugging errors in this context, refer to Debug script score calculation errors in Painless.

params (Map, read-only)
User-defined parameters passed in as part of the query.
doc (Map, read-only)
Contains the fields of the current document. For single-valued fields, the value can be accessed via doc['fieldname'].value. For multi-valued fields, this returns the first value; other values can be accessed via doc['fieldname'].get(index)
_score (double read-only)
The similarity score of the current document.
double
The score for the current document.

Both the standard Painless API and specialized Field API are available.

To run the example, first install the eCommerce sample data.

The following request boosts results for customers in New York, helping highlight local preferences or regional inventory. It uses a function_score query to match products with "jeans" in the name and increase their score when the city is New York.

JSON
GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "products.product_name": "jeans"
        }
      },
      "script_score": {
        "script": {
          "source": """
            double baseScore = _score;
            def city = doc['geoip.city_name'];

            if (city.size() > 0 && city.value == 'New York') {
              baseScore *= 1.5;
            }

            return baseScore;
          """
        }
      }
    }
  }
}