Sub topic 6
Measuring whether reranking helps
This chapter assumes you can run the two-stage pipeline from the previous chapters and know what a query, a document, and a relevance label are. Here you will check whether the reranker improves results over the first stage alone.
What you need
You need three things:
- A set of test queries.
- Relevance labels for each query. These can be binary (relevant or not) or graded (0, 1, 2, 3).
- The ranked list from the first stage alone, and the ranked list after reranking.
Both rankings must come from the same queries, the same corpus, and the same candidate pool. If you change more than one thing, you cannot tell which change helped.
The three metrics
- Recall@k is the fraction of relevant documents that appear in the top k results. It tells you whether the right passages reach the consumer, such as an LLM that only sees the top few.
- MRR (mean reciprocal rank) averages 1/rank of the first relevant result across queries. It suits tasks where one good answer is enough.
- nDCG@k (normalized discounted cumulative gain) rewards putting highly relevant documents near the top. It supports graded labels and divides by the best possible score, so values fall between 0 and 1.
The functions below implement all three for one query. MRR is the mean of reciprocal_rank over all queries.
import math
def reciprocal_rank(ranked_ids, relevant):
for i, doc_id in enumerate(ranked_ids, start=1):
if doc_id in relevant:
return 1.0 / i
return 0.0
def recall_at_k(ranked_ids, relevant, k):
return len(set(ranked_ids[:k]) & relevant) / len(relevant)
def ndcg_at_k(ranked_ids, gains, k):
dcg = sum(gains.get(d, 0) / math.log2(i + 1)
for i, d in enumerate(ranked_ids[:k], start=1))
ideal = sorted(gains.values(), reverse=True)[:k]
idcg = sum(g / math.log2(i + 1) for i, g in enumerate(ideal, start=1))
return dcg / idcg if idcg else 0.0
before = ["d3", "d7", "d1", "d9"]
after = ["d1", "d3", "d7", "d9"]
relevant = {"d1"}
print(reciprocal_rank(before, relevant), reciprocal_rank(after, relevant))
print(recall_at_k(before, relevant, 2), recall_at_k(after, relevant, 2))
print(ndcg_at_k(before, {"d1": 1}, 4), ndcg_at_k(after, {"d1": 1}, 4))Running it prints 0.3333333333333333 1.0, then 0.0 1.0, then 0.5 1.0. The relevant document d1 moved from rank 3 to rank 1, so all three metrics improved.
Reading the results
Reranking only reorders the candidates it receives. It cannot add a relevant passage that the first stage missed. So recall at the full candidate depth (for example recall@50) is the same before and after. It is the ceiling for the whole pipeline. Gains show up in recall at small k, such as recall@3, and in MRR and nDCG.
A table of averages over your query set is a good summary:
| Metric | First stage | Reranked |
|---|---|---|
| recall@50 | same | same |
| recall@3 | lower | higher if reranking helps |
| MRR | lower | higher if reranking helps |
| nDCG@10 | lower | higher if reranking helps |
If recall@50 is low, fix the first stage before tuning the reranker.
Common mistakes
- Unlabeled relevant passages. If your labels are incomplete, a reranker that finds a correct passage you never labeled looks worse than it is. Spot-check the top results where the metric dropped.
- Training data overlap. If you fine-tuned the reranker on the same queries you test on, the scores are inflated. Keep a held-out test set.
- Ignoring cost. A small nDCG gain may not justify the added latency. Report the metric change alongside the time per query.
Exercise
A first stage returns 20 candidates per query, and only 70% of queries have a relevant passage among them. After adding a cross-encoder, what is the highest recall@20 you can reach, and why?
Show answer
70%. The reranker only reorders the 20 candidates, so it cannot recover relevant passages the first stage missed. Recall@20 is unchanged by reranking. The gains appear at smaller k, in MRR, and in nDCG.