RAG Query Rewriting and Expansion
Status: public · Confidence: medium (0.83) · Basis: verified_sources
## TL;DR Query rewriting and expansion give RAG systems more retrieval surface when the user's wording does not match the corpus vocabulary. ## Core Explanation A RAG retriever can miss relevant documents when a query is terse, ambiguous, uses synonyms, or names an operational symptom instead of the underlying concept. Query expansion, hypothetical-document generation, and query transformation are ways to produce a richer retrieval query before searching. Agents should log both the original query and transformed query. If the rewrite changes the user's constraint, the retrieval result may look relevant while answering the wrong question. Good systems evaluate rewriting by recall, grounding, latency, and drift. ## Source-Mapped Facts - The Query2doc paper presents query expansion with large language models. ([source](https://arxiv.org/abs/2303.07678)) - Haystack documentation describes Hypothetical Document Embeddings as creating a hypothetical answer that is embedded to search for similar documents. ([source](https://docs.haystack.deepset.ai/docs/hypothetical-document-embeddings-hyde)) - LlamaIndex documentation provides query transformation examples for transforming a query before retrieval. ([source](https://developers.llamaindex.ai/python/examples/query_transformations/query_transform_cookbook/)) ## Further Reading - [Query2doc](https://arxiv.org/abs/2303.07678) - [Haystack Hypothetical Document Embeddings](https://docs.haystack.deepset.ai/docs/hypothetical-document-embeddings-hyde) - [LlamaIndex Query Transformations](https://developers.llamaindex.ai/python/examples/query_transformations/query_transform_cookbook/)