Query Rewriting
Query rewriting is the practice of reconstructing a user's original question into a query better suited to the retrieval engine, using rules or a large model, before retrieval. It addresses the mismatch between "the way people speak" and "the way a vector store or inverted index matches", and is one of the most cost-effective optimization steps in a RAG system, often markedly improving recall after just a few lines of logic are changed.
Core Principles
User queries are usually short, colloquial, full of ellipsis and anaphora, and may implicitly require multi-hop reasoning, while the underlying retrieval favors keyword or single semantic vector matching, so the two naturally have a gap. Query rewriting addresses these problems upfront. The first category is normalization and correction: unify synonyms and abbreviations, fix typos, and fill in omitted subjects and time ranges. The second is anaphora resolution and context completion: in multi-turn dialogue, a sentence like "and what about its price then" must be combined with history to be restored to "the price of model such-and-such", otherwise the retriever has no way to start. The third is query expansion, generating multiple rewritten versions through synonym dictionaries, pseudo-relevance feedback, or a large model, then running parallel multi-query retrieval and fused ranking, with the common fusion method being Reciprocal Rank Fusion (RRF).
The fourth category is the most representative in recent years, HyDE (Hypothetical Document Embeddings): let the large model first write a "hypothetical answer document" out of thin air, then use the vector of this fake answer to retrieve. Its insight is that the semantic similarity between an answer and a real document is usually higher than that between a question and a document, so the fake answer bridges the expressive gap between interrogative and declarative sentences. The fifth is rewriting based on knowledge base structure, converting natural language questions into structured queries, for example into SQL, or splitting them into metadata filter conditions. Real systems often chain the above into a multi-step pipeline, outputting multiple queries for parallel retrieval and finally merging them uniformly with a reranking model.
Key Points
Rewriting quality must be verified with an evaluation loop, looking at at least recall and final answer accuracy, with rewriting latency also counted in the latency budget. The number of queries must be controlled, usually three to five, with an original query kept as a fallback for each rewriting task to avoid all rewrites drifting together. Caching is key: rewrite results for popular questions can be cached to avoid repeated calls. Prompts must constrain the output format, requiring only the query itself and forbidding explanations and pleasantries, otherwise retrieval terms get polluted. HyDE carries hallucination risk: incorrect facts in the hypothetical document mislead retrieval, so it is advisable to use it only when the domain is familiar and the document style is stable, and to limit the generation length. Fine-tuning a small model can replace online calls to a large model; distilling a dedicated rewriter from offline data can bring latency down to tens of milliseconds.
Common Misconceptions
Assuming rewriting is always better: in fact when the original query is already precise (for example a product model number, an error code or a proper noun), rewriting introduces noise, and such queries should be bypassed and passed straight through. Another misconception is not keeping the original query, leaving no fallback path when rewriting fails. Some treat multiple queries as a free lunch, ignoring the cumulative effect of duplicate recall and latency.
Application Scenarios
RAG semantic retrieval, multi-turn dialogue retrieval, e-commerce search recall, enterprise knowledge base question answering, cross-lingual retrieval, code retrieval, and professional retrieval in law and medicine.