Lesson 12 / 25
Relevance: BM25, Boosting and function_score
Why one result ranks above another.
How scores are computed and tuned
Elasticsearch scores text matches with BM25 by default: a term counts more when it is frequent in the document (with saturation, controlled by k1, default 1.2), rare across the index (inverse document frequency) and found in a shorter field (length normalisation, controlled by b, default 0.75). Scores of clauses are combined by the bool query. Tune relevance with field boosts (title^3), should clauses that reward signals, boosting queries that demote (negative boost) without excluding, and function_score, which multiplies or adds business signals such as popularity (field_value_factor), recency (decay functions like gauss) or fixed weights for matching filters. "explain": true shows how a score was built.
Text relevance plus popularity and freshness
Kibana Dev Tools console syntax; send the same requests with curl or a client library.
GET /products/_search
{
"query": {
"function_score": {
"query": { "multi_match": { "query": "rain jacket", "fields": [ "name^3", "description" ] } },
"functions": [
{
"field_value_factor": {
"field": "sales_count",
"modifier": "log1p",
"missing": 0
}
},
{
"gauss": {
"created_at": { "origin": "now", "scale": "30d", "decay": 0.5 }
}
},
{ "filter": { "term": { "featured": true } }, "weight": 2 }
],
"score_mode": "sum",
"boost_mode": "multiply"
}
}
}Measure relevance changes
Collect a set of real queries with judged good results and compare before and after each tuning change (the ranking evaluation API, _rank_eval, can help). Tuning by anecdote often fixes one query and breaks ten.
Quick check: Under BM25, which term contributes more to the score, all else equal?
- A term that appears in every document
- A term that is rare across the index
- A term in a much longer field
- A term only in a filter clause
Answer
A term that is rare across the index — Inverse document frequency rewards rare terms; filters do not score.