Query Expansion with Synonyms and Related Terms
You'll learn how to broaden retrieval coverage by enriching user queries with semantically similar words while preserving the original search intent.
The Core Problem
When users search with specific terms like "car malfunction," relevant documents might use "vehicle breakdown," "automobile failure," or "engine problem." Pure vector or keyword search might miss these variations, limiting recall.
What Is Query Expansion?
Query expansion augments the original query with synonyms, related terms, and semantic variants before retrieval. Instead of searching for just "car malfunction," you search for "car OR vehicle OR automobile malfunction OR breakdown OR failure."
This technique acts like casting a wider net: you catch more potentially relevant documents without drifting into unrelated territory.
How It Works in RAG
Before embedding or searching, you:
- Identify key terms in the user's query
- Generate expansions using synonym databases (WordNet), LLMs, or domain-specific thesauri
- Append or interleave these terms into the query
- Retrieve with the expanded version
For example:
- Original: "python memory leak"
- Expanded: "python OR py memory OR RAM leak OR overflow OR excessive usage"
The LLM can generate contextually appropriate expansions: "Show me synonyms for 'memory leak' in programming contexts."
When to Use It
- Domain-specific jargon where multiple terms mean the same thing
- Sparse documents where exact keyword matches are rare
- Complementing vector search, especially in hybrid retrieval where BM25 benefits from expanded keywords
Key Takeaway: Query expansion trades precision for recall by adding semantically related terms, helping RAG systems retrieve more relevant documents that use different vocabulary than the user's original query.