Sub topic 1
Where Reranking Fits: First-Stage Retrieval
A reranker never searches the whole collection. It reorders a short list that another system has already produced. This chapter describes that first system, called first-stage retrieval, and shows why the reranker depends on it.
Prerequisites
This chapter assumes you have:
- Basic Python, including lists, functions, and installing packages.
- Familiarity with text embeddings and vector similarity search.
- A general idea of what a transformer model is.
- Basic information retrieval vocabulary: query, document, relevance.
The two jobs in a search system
Search has two jobs that pull in opposite directions.
- Recall: find every passage that might answer the query, out of thousands or millions.
- Precision: put the best passage at the top of the few results the user or the language model will actually read.
One model rarely does both well. A model that is cheap enough to scan millions of passages cannot afford a careful comparison of each one. A model that compares carefully is too slow to run on the whole collection. So systems split the work. A fast first stage favors recall. A slower second stage, the reranker, favors precision.
Two kinds of first-stage retrieval
Most first stages use one of two methods, and many systems combine them.
Lexical retrieval matches words. BM25 is the common example. It scores a passage higher when it contains the query's terms, especially rare terms. It is fast and needs no model. It also handles exact strings well, such as error codes and product names. It fails when the query and the passage use different words for the same idea.
Dense retrieval matches meaning. A bi-encoder is a transformer that turns text into a vector, called an embedding. The system embeds every passage once, ahead of time. At query time it embeds the query and finds the nearest passage vectors. This catches paraphrases, but it can miss exact terms.
| Method | Matches on | Strength | Weakness |
|---|---|---|---|
| Lexical (BM25) | Shared words | Exact terms, no model needed | Misses synonyms |
| Dense (bi-encoder) | Embedding closeness | Paraphrases | Can blur exact terms |
Chapter 11 covers how to merge both lists. For now, treat the first stage as a black box that returns a ranked list.
A first-stage retriever in code
This example uses the dense method. It embeds a small corpus once, then retrieves the nearest passages for a query.
from sentence_transformers import SentenceTransformer, util
bi_encoder = SentenceTransformer("all-MiniLM-L6-v2")
corpus = [
"Rotate API keys from the Security page.",
"Reset your password in Account Settings.",
"Our API returns JSON by default.",
"Billing invoices are emailed on the first of each month.",
]
corpus_emb = bi_encoder.encode(corpus, convert_to_tensor=True, normalize_embeddings=True)
query = "how to change an api key"
q_emb = bi_encoder.encode(query, convert_to_tensor=True, normalize_embeddings=True)
hits = util.semantic_search(q_emb, corpus_emb, top_k=3)[0]
for h in hits:
print(f"{h['score']:.2f} {corpus[h['corpus_id']]}")The passage embeddings are computed once and reused for every query. This is why dense retrieval scales. Each new query costs one model call plus a vector search.
What the first stage limits
The reranker only sees what the first stage returns. This sets a ceiling on the whole pipeline.
- If the best passage is not in the candidate list, the reranker cannot recover it. Reranking can improve the order, but it cannot add passages.
- If the best passage is in the list but ranked 40th, the reranker can move it to the top. That is the case where it helps most.
For this reason, tune the first stage for recall at a generous depth, such as the top 50 or 100. Judge the reranker on how well it orders what it receives. The later chapters on candidate depth and evaluation build on this split.
Exercise
A reranker's top result is wrong for a query. You find that the correct passage was never in the 50 candidates that stage one returned. Which stage needs to change, and why?
Show answer
The first stage needs to change. The reranker can only reorder the candidates it receives, so a passage missing from the list cannot be found. Options include returning more candidates, adding lexical retrieval alongside dense retrieval, or improving the embedding model.