Stage 3 of the RAG funnel: re-ranking with cross-encoders and Azure's semantic ranker
Part 5 of 7 in the RAG in Production series: engineer-level, on Azure, mapped to Microsoft exam AI-103.
TL;DR for managers. The first stages are fast because they compare pre-computed summaries of each document. A re-ranker is slower but more accurate, because it reads the question and each candidate together. It is only run on the top 50 or so, then you keep the best 5. In Azure this is the semantic ranker, switched on with one setting. It is usually the change that makes the biggest visible difference to answer quality.
Bi-encoders and cross-encoders
The vector search in stage 1 uses a bi-encoder: the embedding model encodes the question and each chunk separately, and similarity is a dot product between two vectors. Chunk vectors are computed once at indexing time, which is what makes search over millions of chunks fast. The cost is that the model never sees the question and the chunk at the same time.
A cross-encoder takes the question and one chunk as a single input. Every word of the question attends to every word of the chunk, and the model outputs one relevance score. It is much better at negation, word order and "does this actually answer the question?". It also can't be pre-computed: one model call per (question, chunk) pair, at query time.
| Bi-encoder (stage 1) | Cross-encoder (stage 3) | |
|---|---|---|
| Input | Question and chunk encoded separately | Question and chunk together |
| Pre-compute chunks? | Yes, at indexing | No |
| Work per query | One embedding plus an index lookup | One model pass per candidate |
| Scales to | Millions of chunks | Tens of candidates |
| Good at | Topic and paraphrase | Precise relevance, negation, direction |
Where bi-encoders get it wrong
Two questions show the gap:
- "Which benefits apply if I am not a permanent employee?" The chunk about permanent employees' benefits is very close in vector space. A cross-encoder reading both together can tell it answers the opposite question.
- "Notice period when the employer ends the contract" against a chunk on the notice period when the employee resigns. Same words, opposite direction.
Azure's semantic ranker
In Azure AI Search, stage 3 is the semantic ranker. From Microsoft's documentation:
- It re-ranks the top 50 results of the BM25 or RRF-fused list. It does not search the rest of the index again.
- For each result it builds a summary string of up to 2,048 tokens from the fields in your semantic configuration: title (up to 128 tokens), keywords (up to 128), then content. Field order matters, because long content is trimmed.
- It returns
@search.rerankerScorefrom 0 to 4, and results are sorted by it. - Captions and answers it returns are verbatim text from your index; it doesn't generate anything.
Microsoft describes the models as language-understanding models from Bing and Microsoft Research and doesn't publish the architecture. In the funnel it plays the cross-encoder role: it reads query and text together and re-scores a short list.
| rerankerScore | Microsoft's meaning |
|---|---|
| 4.0 | Highly relevant; answers the question completely |
| 3.0 | Relevant but missing details |
| 2.0 | Somewhat relevant; answers partially |
| 1.0 | Related; answers a small part |
| 0.0 | Irrelevant |
Configure it
Put the field that best describes the chunk first. Here the exam and error codes go in as keywords:
"semantic": {
"defaultConfiguration": "default",
"configurations": [ {
"name": "default",
"prioritizedFields": {
"titleField": { "fieldName": "title" },
"prioritizedContentFields": [ { "fieldName": "content" } ],
"prioritizedKeywordsFields": [ { "fieldName": "codes" } ]
} } ]
}
Use the score to say "I don't know"
Unlike RRF scores, reranker scores have a stable meaning, so they are the right place for a relevance cut-off. A cut-off is also your best defence against a confident answer built from weak context.
from azure.search.documents.models import VectorizableTextQuery
results = search.search(
search_text=question,
vector_queries=[VectorizableTextQuery(text=question, k_nearest_neighbors=50, fields="content_vector")],
query_type="semantic", semantic_configuration_name="default",
query_caption="extractive",
top=10, select=["chunk_id", "title", "content"])
MIN_RERANK = 2.0 # coarse cut-off: "somewhat relevant" or better; tune on your eval set
context = [r for r in results if r["@search.reranker_score"] >= MIN_RERANK][:5]
if not context:
reply = "I couldn't find this in the policy documents." # say so instead of guessing
Microsoft notes that reranker scores vary slightly with infrastructure and model updates, so keep thresholds coarse (whole or half points) and check them against your evaluation set, not single examples.
Bring your own cross-encoder
If you aren't using the semantic ranker, need a domain-tuned model, or are fusing results from outside Azure AI Search, run a cross-encoder yourself. This open model was trained on the MS MARCO passage-ranking dataset:
from sentence_transformers import CrossEncoder
reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
def rerank(question: str, candidates: list[dict], keep: int = 5) -> list[dict]:
pairs = [(question, c["content"]) for c in candidates] # each pair is read together
scores = reranker.predict(pairs) # one forward pass per pair
ranked = sorted(zip(candidates, scores), key=lambda cs: cs[1], reverse=True)
return [c for c, _ in ranked[:keep]]
candidates = list(search.search(search_text=question, vector_queries=[vq], top=50))
context = rerank(question, candidates)
Hosted re-rank APIs (Cohere Rerank, for example) do the same job as a service. Before sending document content to any external API, check it against your data-protection and residency requirements.
Scoring profiles and freshness
One subtlety: if you boost recent documents with a scoring profile, the semantic ranker's re-ordering can undo the
boost. Azure handles this with rankingOrder: "boostedRerankerScore" in the semantic configuration, which
applies the scoring profile again after re-ranking and returns @search.rerankerBoostedScore. Useful for
policy libraries where the newest version should win.
What re-ranking costs
The work grows with the number of candidates times their length. That's why Azure caps it at 50 candidates and summarises each, and why a self-hosted cross-encoder over 500 candidates will hurt your latency. If you need more than 50, the fix is usually in stage 1 (better chunking, filters, BM25 analysers), not a bigger re-rank window.
Learning this for AI-103? Microsoft's AI-103 exam (Azure AI Apps and Agents Developer Associate) asks you to implement RAG, configure semantic, hybrid and vector search for grounding, and monitor relevance and grounding quality. Our live online AI-103 course builds these pipelines hands-on in Microsoft Foundry and Azure AI Search, on UK time, with mock assessments before you book the exam.
The RAG in Production series
- Part 1: RAG or fine-tuning?
- Part 2: The RAG retrieval funnel
- Part 3: Stage 1: ANN + BM25 hybrid search
- Part 4: Stage 2: Reciprocal Rank Fusion
- Part 5: Stage 3: re-ranking with cross-encoders (you are here)
- Part 6: Scaling: 10,000 vs 10 million documents
- Part 7: Evaluating RAG: retrieval vs generation