Sub topic 15
Measuring and tuning relevance
This chapter assumes you know how BM25 scoring works, how a bool query combines a scored must clause with filter clauses, and how a search service is wired to a source of truth. Relevance tuning is the last step. Without a way to measure results, every change is a guess.
Why you need measurement
A search change that helps one query often hurts another. Raising the weight of the name field may fix "running shoes" and break "gift card". If you judge changes by trying a few queries by hand, you will not notice the regressions.
The fix is to build a small, repeatable test set before you tune anything.
Build a judgment list
A judgment list is a set of queries, each paired with documents that a person has rated for relevance. A simple scale works well: 0 for irrelevant, 1 for acceptable, 2 for a good match, 3 for a perfect match.
Sources for queries:
- Your query logs. Sample both frequent queries and rare ones, because the rare ones are the long tail where search usually fails.
- Queries that returned zero results.
- Queries from support tickets or user complaints.
Start with 50 to 100 queries. That is enough to catch large regressions and small enough for one person to rate in an afternoon.
Offline metrics
Run each judged query against the engine and compare the ranked results with the ratings. Three metrics cover most needs.
| Metric | What it measures | Use it when |
|---|---|---|
| Precision@k | Share of the top k results that are relevant | Users only look at the first page |
| Recall@k | Share of all relevant documents that appear in the top k | Missing a result is costly |
| NDCG@k | Quality of the ordering, rewarding highly rated documents near the top | You have graded ratings |
NDCG stands for normalized discounted cumulative gain. It gives each result a gain based on its rating, discounts that gain by position, and divides by the best possible ordering. A score of 1.0 means the ranking is ideal.
This script computes precision@k and NDCG@k for one query:
import math
def precision_at_k(ratings, k):
top = ratings[:k]
return sum(1 for r in top if r > 0) / k
def dcg(ratings, k):
return sum(r / math.log2(i + 2) for i, r in enumerate(ratings[:k]))
def ndcg_at_k(ratings, k):
ideal = sorted(ratings, reverse=True)
best = dcg(ideal, k)
return dcg(ratings, k) / best if best > 0 else 0.0
# Ratings of the results in the order the engine returned them.
returned = [3, 0, 2, 1, 0]
print(round(precision_at_k(returned, 5), 2)) # 0.6
print(round(ndcg_at_k(returned, 5), 2))Average each metric across all queries and record the result for every change you test.
Tune one thing at a time
Typical levers, from cheapest to most invasive:
- Field boosts. Give matches in
namemore weight than matches indescription. - Analyzer changes, such as adding synonyms or a stemmer. These need a reindex.
- BM25 parameters
k1andb. Defaults are usually fine. Lowerbif long documents are being unfairly penalized. - Business signals, such as popularity or recency, blended into the score.
This query boosts the name field:
GET /products/_search
{
"query": {
"multi_match": {
"query": "running shoes",
"fields": ["name^3", "description"]
}
}
}Change one lever, rerun the judgment list, and keep the change only if the average metric improves and no important query gets much worse.
Online signals
Offline ratings can drift from what users want. Once the service is live, watch behavior:
- Click-through rate on results, and the position of the clicked result.
- Zero-result rate and the queries behind it.
- Reformulation rate, where users immediately retype the query.
Use A/B tests to compare two ranking configurations on live traffic. Clicks are biased toward top positions, so treat them as a signal to investigate and not as ground truth.
Exercise
After you raise the boost on name from 2 to 5, average NDCG@10 rises from 0.71 to 0.74, but the query "gift card" drops from 0.9 to 0.4. What should you do?
Show answer
Do not accept the change on the average alone. Inspect "gift card" to see why the heavier name boost pushed worse results up. Then try a smaller boost, or fix the cause, such as a missing synonym. Keep the change only if no important query regresses badly. Add similar queries to the judgment list so the problem stays covered.