← Master Index
Vol. 14 Module 14.1 Lecture

Query Expansion

RAG Core Concepts

How This Lesson Fits the Module & Volume

Users ask short, vague, or vocabulary-mismatched questions. Query expansion rewrites or augments the query before retrieval to improve recall—complements hybrid search and re-ranking. It sits on the query side of the upcoming RAG pipeline.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define query expansion and list common techniques (synonyms, multi-query, HyDE).
  • Explain how expansion trades recall gains for latency and noise.
  • Implement a simple multi-query retrieve-and-fuse pattern.
  • Contrast rule-based expansion with LLM rewriting.
  • Guard against query drift that retrieves irrelevant topics.
  • Evaluate expansion with recall@k on hard / short queries.
Definition

Query expansion improves retrieval by transforming a user query into one or more enriched queries (added terms, paraphrases, or hypothetical documents) whose search results are unioned or fused.

Technique Map

TechniqueIdeaRisk
Synonym / glossaryAdd domain aliasesStatic lists go stale
Multi-queryLLM paraphrases → multi searchCost × N queries
HyDEEmbed a hypothetical answerHallucinated topic drift
PRFExpand from top hits’ termsFeedback loop on bad hits

Helps

  • Short queries
  • Vocab mismatch
  • Multi-intent asks

Hurts

  • Already precise IDs
  • Tight latency budgets
  • Noisy corpora

Ops

  • Cache rewrites
  • Cap N paraphrases
  • Fuse with RRF

Code: Multi-Query + RRF Fuse

from collections import defaultdict from sentence_transformers import SentenceTransformer, util model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") corpus = ["PTO carryover rules", "Parental leave policy", "Sick leave documentation"] emb = model.encode(corpus, normalize_embeddings=True, convert_to_tensor=True) def search(q: str, k: int = 3): qe = model.encode(q, normalize_embeddings=True, convert_to_tensor=True) hits = util.semantic_search(qe, emb, top_k=k)[0] return [corpus[h["corpus_id"]] for h in hits] def rrf_fuse(lists, k=60): scores = defaultdict(float) for lst in lists: for r, doc in enumerate(lst, 1): scores[doc] += 1.0 / (k + r) return sorted(scores, key=lambda d: -scores[d]) user_q = "time off after having a baby" # In production, an LLM generates paraphrases; here we hardcode expansions: expansions = [user_q, "parental leave", "maternity paternity PTO"] fused = rrf_fuse([search(q) for q in expansions]) print(fused)

Strengths

  • Boosts recall on hard queries
  • Cheap wins with glossaries
  • Composes with hybrid + re-rank

Tradeoffs

  • Extra latency / embed calls
  • Drift retrieves wrong topics
  • Harder debugging
Common Misconception

“More paraphrases always help.” Past a point you retrieve a kitchen-sink of near-topic junk, burn tokens, and confuse the generator. Cap expansions, fuse ranks, and measure recall vs precision on a held-out set.

Knowledge Check

  1. Short Answer: What is query expansion? Answer: Enriching/rewriting the query to improve retrieval recall.
  2. True/False: HyDE embeds a hypothetical answer document. Answer: True (typically).
  3. Multiple Choice: Multi-query cost scales with: (a) number of rewrites searched, (b) CSS files, (c) DPI. Answer: (a).
  4. Short Answer: Name one drift risk. Answer: Expanded terms pull unrelated topics into the candidate set.
  5. True/False: Exact SKU lookups usually need heavy paraphrasing. Answer: False—often hurts.
  6. Multiple Choice: Fuse multi-query hits with: (a) RRF, (b) random delete, (c) only PNG. Answer: (a).
  7. Short Answer: What is PRF? Answer: Pseudo-relevance feedback—expand using terms from initial top hits.
  8. True/False: Expansion replaces the need for hybrid search. Answer: False—they complement.
  9. Multiple Choice: Next lecture: (a) RAG Pipeline, (b) Vol. 1 only, (c) printers. Answer: (a).
  10. Short Answer: How do you know expansion helped? Answer: Higher recall@k (without collapsing precision) on eval queries.

Key Takeaways

  • Expand queries to fix vocabulary mismatch and short asks.
  • Cap N, fuse with RRF, watch for topic drift.
  • Eval on hard queries; skip expansion when exact match dominates.
  • Next: RAG Pipeline.
Trainer’s Guide

Lab: Collect 15 failing short queries; add glossary expansion; measure recall@5 delta.

Prompt: Would you expand before or after metadata ACL filters? Why?

Recap: Query expansion is a recall lever on the question side. Continue with RAG Pipeline.