Elasticsearch Boosting and Custom Scoring
On this page
Why the most relevant result isn't always the best
Two products match "mouse" equally well on text. One sells 2,700 units a month with a 4.5 rating; the other sells 600. Users expect the popular one first. And a match in the product name should count more than one deep in the description. Scoring is where search becomes product design.
Weighting fields with boosts
Multiply a field's contribution with ^:
GET /products/_search
{ "query": { "multi_match": { "query": "laptop", "fields": ["name^3", "description"] } } }total 3
104 6.689 Budget Laptop
102 1.438 Wireless Keyboard
105 1.222 Ultrabook Pro"Budget Laptop" has the word in its name, worth three times as much, so it leads clearly. The keyboard matches only because its description says "for laptops".
Boosting with should clauses
Inside a bool query, should clauses with a boost reward documents without excluding others:
{ "bool": {
"must": [ { "match": { "name": "headphones" } } ],
"should": [ { "term": { "brand": { "value": "Sonic", "boost": 2 } } },
{ "term": { "in_stock": true } } ] } }To push results down without removing them (out-of-stock items, say), use a boosting query with a negative clause and a negative_boost below 1.
function_score: blending popularity and recency into relevance
Plain text match for "mouse":
101 1.702 Wireless Mouse | sales 2702 | rating 4.5
103 1.702 Gaming Mouse | sales 622 | rating 4.6A tie. Adding popularity (log of sales, so a hit item doesn't overwhelm everything) and rating:
GET /products/_search
{
"query": {
"function_score": {
"query": { "match": { "name": "mouse" } },
"functions": [
{ "field_value_factor": { "field": "sales", "modifier": "log1p", "factor": 1 } },
{ "field_value_factor": { "field": "rating", "modifier": "none", "factor": 0.5 } }
],
"score_mode": "sum",
"boost_mode": "multiply"
}
}
} 101 9.672 Wireless Mouse | sales 2702 | rating 4.5
103 8.673 Gaming Mouse | sales 622 | rating 4.6score_modecombines the functions (here, summed).boost_modecombines that with the text score (here, multiplied, so text relevance still matters).log1pcompresses large numbers: 2,700 sales isn't treated as 4.3 times better than 622.
Other functions:
- Decay functions (
gauss,exp,linear) for freshness or distance: news from today scores higher than last month's, stores nearby higher than far away. script_scorefor custom formulas, at a performance cost.rank_featurefields: an efficient, purpose-built way to store and use signals like popularity.
Tuning relevance without guessing
- Build a judgment list: real queries with the results people should see.
- Measure with the
_rank_evalAPI (precision, NDCG) before and after each change. - Change one thing at a time, and watch click-through and conversion after release.
- Don't let business boosts swamp relevance: a best-seller that barely matches shouldn't top every search.
Check yourself
Matches in the name field contribute three times as much to the score as matches in unweighted fields, so a product named "Laptop Stand" outranks one that only mentions laptops in its description.
It compresses large values: 10 sales and 10,000 sales differ by a factor of 4 after log scaling, not 1,000. Popular items get an advantage without overwhelming text relevance.
Use a boosting query with the main query as positive and an out-of-stock clause as negative, with a negative_boost below 1, such as 0.3. Out-of-stock items stay in results but drop lower.
Build a set of real queries with judged results, measure with the rank evaluation API, and compare changes against that baseline.
For content where freshness matters, like news. A gauss or exp decay on the date lowers scores gradually as content ages.