Skip to content
TheAgent Ecosystem
RAG & Knowledge

Query Rewriting for RAG: Fix Retrieval at the Query Side

Most RAG fixes target documents. When retrieval misses on short or conversational questions, the query is the weak link.

Muhammad Qasim HammadAI-assisted6 min read1,225 words

AI-drafted, reviewed by Muhammad Qasim Hammad on July 24, 2026. See our AI disclosure.

RAG Retrieval: Fix Retrieval at the Query Side
Table of contents
  1. What is query rewriting in RAG and why does it matter?
  2. How does a raw query differ from a rewritten one?
  3. Which query-side techniques should you use?
  4. How do you build a query-rewriting step end to end?
  5. When is query rewriting not worth the latency?

You have tuned chunk sizes, added a reranker, and swapped embedding models, but your RAG system still misses on short or conversational questions. The reason is often upstream of every document-side fix you tried. The user's raw query never matched your chunks in the first place, so no amount of document polishing was going to rescue that retrieval.

What is query rewriting in RAG and why does it matter?#

Query rewriting in a RAG pipeline is where you use query rewriting rag techniques to transform the raw user question into a better search query before retrieval. A model resolves pronouns, adds chat-history context, and expands vague wording. That upstream fix repairs retrieval failures that chunking, reranking, and embedding swaps on the document side simply cannot reach.

The core insight is a division of labor. Document-side fixes assume the query is already good and try to match it better. Query-side fixes assume the query is the weak link. When a user types "does it support that too?", no chunk in your index contains those pronouns, so retrieval returns noise. Rewrite it to "does the Pro plan support single sign-on?" and the same retriever suddenly lands the right passage. Our guide on why RAG chatbots give wrong answers shows how many failures trace to retrieval rather than the model itself.

Flow from raw query into a rewrite and expand step, then retrieve, then merge resultsThe query gets cleaned and optionally fanned out before retrieval, then the ranked lists merge back into one.

How does a raw query differ from a rewritten one?#

A raw query is what the user actually typed, which is often short, context-dependent, or full of pronouns that only make sense inside the conversation. A rewritten query is a self-contained, keyword-rich version a model produced from that raw input plus recent chat history. The retriever sees only the query text, so the rewrite is what decides recall.

Comparison of a raw conversational query versus a rewritten self-contained query and their retrieval resultsThe raw query hides meaning in pronouns; the rewrite makes it self-contained so the retriever can match it.

The gap is easiest to feel with a conversational example. A user asks about a product, then follows up with "and how much is that on the annual plan?" The word "that" carries the entire meaning, and it is invisible to a vector search. The rewrite pulls the earlier product name forward and produces "annual plan price for the Standard tier", which now shares real vocabulary with your indexed pricing pages. Same retriever, same index, better input.

Which query-side techniques should you use?#

Four techniques sit at the query side, and they stack rather than compete. Query rewriting cleans a single query. Multi-query generates paraphrases and merges results. HyDE embeds a hypothetical answer instead of the question. Query expansion bolts on synonyms. Each buys recall at the price of at least one extra model call before retrieval even begins.

TechniqueWhat it doesAdded cost
Query rewritingRewrites 1 raw query into a clean, self-contained search query1 LLM call
Multi-queryGenerates N paraphrases, retrieves each, merges with RRF1 LLM call + N retrievals
HyDEGenerates a hypothetical answer, embeds that, retrieves by it1 LLM call + 1 embed
Query expansionAdds synonyms and related terms to the query0 to 1 LLM call

HyDE is worth understanding because it inverts the usual assumption. Instead of embedding the question, you ask a model to draft a plausible answer, embed that hypothetical document, and retrieve the real chunks nearest to it. The intuition, from Gao et al. 2022 (arXiv:2212.10496), is that an answer sits closer in embedding space to real answer passages than a bare question does. Multi-query, documented in the LangChain multi-query retriever docs, generates 3 to 5 paraphrases and unions their results, so a single awkward phrasing no longer sinks the whole retrieval.

Checklist of signals that query rewriting is worth adding to a RAG pipelineTwo or more of these and the query side is your highest-leverage fix.

How do you build a query-rewriting step end to end?#

You wire a rewrite stage between the user input and your retriever, then measure whether it actually lifts recall. Capture the raw query plus recent turns, run 1 model call to rewrite or expand, optionally fan out to multiple queries, retrieve for each, and merge the ranked lists with reciprocal rank fusion before they reach your reranker or generator.

Decision flowchart for choosing between query rewriting, multi-query, HyDE, and query expansion in a RAG pipelineStart from how your queries fail; each path ends at measure recall before you keep the step.

Reciprocal rank fusion is the merge step that makes multi-query practical. Each generated query returns its own ranked list, and RRF scores every document by summing 1 divided by (k + rank) across the lists, with k commonly set to 60. A chunk that appears high in several lists floats to the top, and you do not need calibrated scores from different retrievers to combine them. That property is why the same fusion trick powers hybrid search that blends BM25 and vector results.

The honest trade-off is latency and spend. Every technique here adds at least 1 model call before retrieval, and multi-query multiplies your retrieval count by N. That overhead earns its keep when queries are short, ambiguous, or conversational, and it is mostly wasted when users already type long, specific, keyword-rich questions. Start with plain rewriting, which is the cheapest at a single call, and only add multi-query or HyDE if your recall measurements still fall short.

When is query rewriting not worth the latency?#

Skip or trim the query side when your users already send long, specific, keyword-dense queries, because there is little left to rewrite and you only add latency. It is also a poor fit for hard real-time paths where a single extra round trip breaks your budget, or when a cheaper document-side fix clears the same bar.

The decision is really about where your recall is leaking. If a labeled evaluation set shows retrieval already returns the right chunks for typical questions, spend your effort on reranking or chunking instead. If retrieval whiffs on the short and conversational tail, the query side is the highest-leverage place to intervene. Pair this with the right index too, since your embedding model choice sets the ceiling that any rewrite has to work within.

Frequently asked questions

What is query rewriting in RAG?
Query rewriting is where a language model rewrites the raw user question into a cleaner search query before retrieval runs. It resolves pronouns, folds in recent chat history, and expands vague wording so the query shares real vocabulary with your indexed chunks. The retriever only sees the query text, so the rewrite is what decides recall.
How is query rewriting different from query expansion?
Query rewriting produces a single, self-contained rewrite of the question, often using chat history to resolve context. Query expansion instead adds synonyms and related terms to widen the match without changing the core question. Rewriting fixes ambiguity and missing context; expansion mainly improves lexical coverage. Many pipelines use both, since they solve different failure modes.
What is HyDE and when should you use it?
HyDE, or Hypothetical Document Embeddings from Gao et al. 2022 (arXiv:2212.10496), asks a model to draft a plausible answer, embeds that hypothetical document, and retrieves the real chunks nearest to it. The idea is that an answer sits closer in embedding space to real answer passages than a bare question does. Use it when questions and documents are phrased very differently.
How does reciprocal rank fusion merge multi-query results?
Reciprocal rank fusion scores each document by summing 1 divided by (k + rank) across every ranked list, with k commonly set to 60. A chunk that appears high in several lists floats to the top. RRF needs only the rank position, not calibrated scores, so it can combine results from different queries or even different retrievers cleanly.
When is query rewriting not worth the added latency?
Skip it when users already send long, specific, keyword-dense queries, since there is little to rewrite and you only add a model call and latency. It also fits poorly on hard real-time paths where one extra round trip breaks your budget. If a labeled evaluation shows retrieval already returns the right chunks, spend effort on reranking or chunking instead.

Sources

Primary references and vendor documentation used while drafting and reviewing this article.

  1. Gao et al. 2022, Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE), arXiv:2212.10496
  2. LangChain: How to use the MultiQueryRetriever

Written by

Muhammad Qasim Hammad
Muhammad Qasim Hammad
AI agents & automationFounder · Cart Gaze LLCPMP-certified PM

Muhammad Qasim Hammad is an AI agent and automation expert and the founder of Cart Gaze LLC (cartgaze.com). He builds product for the love of it: when an idea lands, a working prototype is usually running within hours, built with the same AI agents and automations he sells. He puts his own output at roughly 20× what it was before agents, and the Agentic OS behind this site is the working proof, documented in public with the tools he actually ran and what they really cost.

AI & Automation Services

Want a pipeline like this running in your business?

I'm Qasim — I design and ship AI agents and n8n automations for solo operators and small teams. Tell me what's eating your team's week, and I'll scope a fix.

Related reading