Score context
Serverless Stack
Use a Painless script in a function score to apply a new score to documents returned from a query.
Check the troubleshooting guide for help with script score calculation errors.
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 viadoc['fieldname'].get(index) _score(doubleread-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.
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;
"""
}
}
}
}
}