Query rewriting in RAG pipelines transforms user queries before retrieval to better match the language of knowledge bases and datasets. The guide covers why retrieval quality determines LLM response accuracy, the full workflow from query normalization through generation, and five core techniques: query expansion, decomposition, paraphrasing, multi-query generation, and step-back prompting. Practical Python code examples show how to normalize queries, call an LLM to rewrite them, retrieve documents via vector search, and generate grounded answers. Evaluation metrics (Recall@k, MRR, NDCG) and comparisons with reranking and query expansion are also covered, along with limitations like semantic drift, added latency, and over-expansion.
Table of contents
What is query rewriting in RAG?Why is query rewriting important for RAG?How does query rewriting work in RAG?What problems does query rewriting solve?What are common query rewriting techniques?When should you use query rewriting?How do you implement query rewriting?What are the limitations of query rewriting?How do you evaluate query rewriting performance?How does query rewriting compare to reranking?How does query rewriting differ from query expansion?How does query rewriting fit into RAG pipelines?What tools support query rewriting for RAG?Why query rewriting for RAG is becoming essential85 Impressions