Catalog My learning Blog
Elasticsearch Tutorial: How Full-Text Search Works · Phase 2 · Lesson 10 · 3 min read

Elasticsearch Boosting and Custom Scoring

On this page
  1. Why the most relevant result isn't always the best
  2. Weighting fields with boosts
  3. Boosting with should clauses
  4. function_score: blending popularity and recency into relevance
  5. Tuning relevance without guessing
  6. Check yourself

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 ^:

HTTP
GET /products/_search
{ "query": { "multi_match": { "query": "laptop", "fields": ["name^3", "description"] } } }
Code
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:

JSON
{ "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.

Field weights and boosts adjust text relevance; function_score multiplies or adds business signals such as popularity, rating and recency to produce the final ranking.
Field weights and boosts adjust text relevance; function_score multiplies or adds business signals such as popularity, rating and recency to produce the final ranking.

function_score: blending popularity and recency into relevance

Plain text match for "mouse":

Code
  101    1.702   Wireless Mouse | sales 2702 | rating 4.5
  103    1.702   Gaming Mouse   | sales 622  | rating 4.6

A tie. Adding popularity (log of sales, so a hit item doesn't overwhelm everything) and rating:

HTTP
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"
    }
  }
}
Code
  101    9.672   Wireless Mouse | sales 2702 | rating 4.5
  103    8.673   Gaming Mouse   | sales 622  | rating 4.6
  • score_mode combines the functions (here, summed).
  • boost_mode combines that with the text score (here, multiplied, so text relevance still matters).
  • log1p compresses 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_score for custom formulas, at a performance cost.
  • rank_feature fields: 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_eval API (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.