Sub topic 1
Where Reranking Fits: First-Stage Retrieval
Prerequisites
This chapter assumes basic Python, text embeddings, vector similarity search, and transformer models. You should also know the terms query, passage, and relevance.
First-Stage Retrieval
A search system usually starts with first-stage retrieval. This stage quickly selects a candidate pool from a large document collection.
A dense retriever uses a bi-encoder, a model that encodes the query and each passage separately as vectors. Vector similarity, often cosine similarity, identifies passages that are close to the query in embedding space. A lexical retriever, such as BM25, instead matches words and their frequencies.
The first stage must be fast because it may search millions of passages. It does not need to produce the final order. It needs to avoid missing the useful passages.
Why Bi-Encoder Similarity Is Not Enough
A bi-encoder compares two independently created vectors. This is efficient, but the separate encoding limits fine-grained interaction.
For example, a query such as “rotate an API key” should prefer a passage about creating a replacement key and deleting the old one. A passage about changing a password may contain related security words but answer a different question. Vector similarity can confuse these meanings, especially when passages use different wording or contain several topics.
A bi-encoder also compresses each passage into one fixed-size vector. That compression loses some relationships between particular query terms and passage phrases.
> callout note > First-stage retrieval quality sets an upper bound on the final result. A reranker cannot rank a relevant passage that was never retrieved.
How a Cross-Encoder Reads Both Texts
A cross-encoder receives the query and one passage together. Its transformer layers can attend to tokens across both inputs. It then returns a relevance score for that pair.
This joint reading is more accurate but more expensive. The model must process every query-passage pair, so it is normally applied only to a small candidate pool.
from sentence_transformers import CrossEncoder
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
query = "How do I rotate an API key?"
passages = [
"Create a new key on the Security page, then delete the old key.",
"Our API supports JSON and XML response formats.",
]
scores = model.predict([(query, passage) for passage in passages])
ranked = sorted(zip(passages, scores), key=lambda item: item[1], reverse=True)
for passage, score in ranked:
print(f"{score:.2f} {passage}")Building a Retrieve-Then-Rerank Pipeline
The complete pipeline is:
- Encode the query and search the vector index.
- Keep the top candidates, such as 20 or 50 passages.
- Create one query-passage pair for each candidate.
- Batch those pairs through the cross-encoder.
- Sort by the returned scores and keep the final results.
The scores are useful for ordering within this request. Do not assume they are calibrated probabilities.
Exercise
Why should a cross-encoder usually run after first-stage retrieval?
Show answer
It reads each query and passage together, which improves ranking but costs too much to run against an entire large collection.