The RAG retrieval funnel: how production systems find the right 5 chunks out of millions

The RAG retrieval funnel: how production systems find the right 5 chunks out of millions

Part 2 of 7 in the RAG in Production series: engineer-level, on Azure, mapped to Microsoft exam AI-103.

TL;DR for managers. A production RAG system finds the right 5 passages out of millions in stages. A cheap, fast search casts a wide net, a fusion step merges the results, and a slower, more accurate model re-ranks a short list. Each stage has a different job. When answers are wrong, the cause is usually that the first stage never found the right passage, and no later stage can fix that.

Every retrieval method trades accuracy for speed. The precise ones (models that read the question and the document together) are far too slow to run over a million chunks. The fast ones (vector indexes, keyword indexes) can search millions of chunks in milliseconds but make more ordering mistakes. So production systems use both, in order: fast and broad first, slow and precise last. Microsoft's Architecture Center describes this same pattern for RAG: retrieve broadly, merge with RRF, re-rank with a model, then truncate to the top few.

The funnel

Stage Job Method In Out In Azure AI Search
1. Broad retrieval Recall: don't miss the right chunk ANN vector search (HNSW) and BM25 keyword search, in parallel Every chunk in the index About 50 per method A hybrid query: search plus vectorQueries with k
2. Score fusion One list from several Reciprocal Rank Fusion (RRF) 2 or more ranked lists 1 fused list Automatic whenever a query has more than one leg
3. Precision filtering Get the order right Re-ranking model (cross-encoder style) Top 50 Same 50, re-scored Semantic ranker, @search.rerankerScore 0 to 4
4. Truncate Fit the prompt Keep the top N 50 5 to 10 top

The important asymmetry: stage 1 is judged on recall (is the right chunk anywhere in the 50?) and stage 3 on precision (is it in the top 5?). A re-ranker can promote the right chunk from position 38 to position 1. It cannot promote a chunk that isn't in the list. That is why I would rather widen stage 1 than add a clever prompt.

Worked example

An employee asks: "Can I claim mileage for a client visit in my own electric car?" The expenses index has 200,000 chunks.

  • Vector search finds chunks about "using a personal vehicle for business travel", even though they never say "mileage" or "electric car". That's what embeddings are good at: meaning.
  • BM25 finds the one chunk that names HMRC's "Advisory Electricity Rate", because it contains the literal words "electric" and "mileage". Vector search ranked it 31st, because the chunk is mostly a rate table.
  • RRF lifts chunks that both methods agree on and keeps strong single-method hits in play.
  • The semantic ranker reads the question against each of the 50 candidates and puts the electricity rate chunk and the personal-vehicle policy at the top.
  • The LLM gets 5 chunks and answers with both sources cited.

Remove BM25 and the rate table is probably not in the top 50. Remove the re-ranker and it's in the list but maybe at position 9, outside the 5 you send.

All four stages in one Azure request

In Azure AI Search the whole funnel is a single hybrid semantic query. The index needs a vectorizer (so Azure can embed the query text for you) and a semantic configuration.

from azure.search.documents.models import VectorizableTextQuery

question = "Can I claim mileage for a client visit in my own electric car?"

results = search.search(
    # Stage 1a: BM25 over the searchable text fields
    search_text=question,
    # Stage 1b: ANN over the vector field (HNSW); k=50 candidates
    vector_queries=[VectorizableTextQuery(text=question, k_nearest_neighbors=50,
                                          fields="content_vector")],
    # Stage 2 happens automatically: two result sets, so Azure fuses them with RRF
    # Stage 3: semantic ranker re-scores the top 50 fused results
    query_type="semantic", semantic_configuration_name="default",
    # Stage 4: keep only what the LLM needs
    top=5,
    select=["chunk_id", "title", "content"])

for r in results:
    print(f"{r['@search.reranker_score']:.2f}  {r['@search.score']:.4f}  {r['title']}")

The same request over REST, which is useful for seeing exactly what you are asking for:

POST https://<service>.search.windows.net/indexes/expenses/docs/search?api-version=2024-07-01
{
  "search": "Can I claim mileage for a client visit in my own electric car?",
  "vectorQueries": [
    { "kind": "text", "text": "Can I claim mileage for a client visit in my own electric car?",
      "fields": "content_vector", "k": 50 }
  ],
  "queryType": "semantic",
  "semanticConfiguration": "default",
  "top": 5,
  "select": "chunk_id,title,content"
}

Read the two scores in the output separately. @search.score is the RRF score from stage 2 (small numbers, around 0.01 to 0.03). @search.rerankerScore is stage 3 on a 0 to 4 scale. Results come back in reranker order.

Three mistakes I see most

  1. Starving the re-ranker. Setting k to 5 because you only want 5 results. The semantic ranker then re-orders 5 candidates instead of 50. Microsoft's guidance is to make sure at least 50 results reach the ranker (with the newer API versions, k plus maxTextRecallSize) and to control the output size with top.
  2. Sending 20 chunks "to be safe". More context costs more tokens and tends to dilute the answer. Liu et al. (2023, "Lost in the Middle") showed models use information at the start and end of a long context better than information in the middle. Re-rank well and send fewer.
  3. Debugging the prompt when retrieval is the problem. Log the 50 stage-1 candidates for failed questions. If the right chunk isn't there, no prompt change will help. Part 7 covers measuring this properly.

The next three parts take one stage each: ANN plus BM25, then RRF, then re-ranking.

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

  1. Part 1: RAG or fine-tuning?
  2. Part 2: The RAG retrieval funnel (you are here)
  3. Part 3: Stage 1: ANN + BM25 hybrid search
  4. Part 4: Stage 2: Reciprocal Rank Fusion
  5. Part 5: Stage 3: re-ranking with cross-encoders
  6. Part 6: Scaling: 10,000 vs 10 million documents
  7. Part 7: Evaluating RAG: retrieval vs generation
Back to blog

Ready to take the first step?

Live online courses on UK time, mapped to the current Microsoft AI exams. No coding needed for AI at work.

Explore AI coursesAsk us a question