Sub topic 2
Multi-query Expansion: Searching with Several Phrasings
Assume you know basic RAG, embeddings, keyword search, and how to call an LLM. Query rewriting changes the user's question before retrieval so the search system can find relevant evidence more reliably.
Multi-query Expansion
A single query can miss documents because it uses different vocabulary from the indexed text. Multi-query expansion asks an LLM to create several versions of the question. The system searches with the original query and each variant, then combines the results.
For example, “How do I reduce API response time?” might become:
- “Techniques for lowering API latency”
- “How to improve web service performance”
- “Methods for reducing server response time”
The variants should preserve the user's intent. Extra wording can introduce query drift, where retrieval moves toward a related but incorrect topic.
Reciprocal Rank Fusion
Each search produces a ranked list. Reciprocal rank fusion (RRF) gives a document the score 1 / (k + rank) for every list where it appears. Documents that rank well across several searches rise to the top.
def reciprocal_rank_fusion(
rankings: list[list[str]], k: int = 60
) -> list[str]:
scores: dict[str, float] = {}
for ranking in rankings:
for rank, document_id in enumerate(ranking, start=1):
scores[document_id] = scores.get(document_id, 0.0)
scores[document_id] += 1 / (k + rank)
return sorted(scores, key=scores.get, reverse=True)
print(reciprocal_rank_fusion([["a", "b", "c"], ["b", "c", "d"]]))HyDE and Step-Back Queries
HyDE, or hypothetical document embeddings, asks the LLM to write a hypothetical answer or passage. The system embeds that text and searches for real documents with similar meaning. HyDE can help when the question is short or uses unfamiliar terms, but a fabricated passage can also add incorrect assumptions.
A step-back query asks a broader question before searching for specific details. For example, “What caused error 403 in this deployment?” can first become “How does authorization work in this deployment system?” Use it when the question lacks background context.
Decomposition
Decomposition splits a complex question into smaller sub-questions. For example:
- What authentication method does the service use?
- How is the token validated?
- Which failures produce a 403 response?
Search each part separately, then combine the evidence. This works well when one query contains several facts or requires multiple reasoning steps.
Exercise
Which technique is a good first choice for a follow-up question containing several independent facts?
Show answer
Decomposition, because it creates focused sub-questions that can be retrieved separately.