How it works · 9 of 37 Advanced
What is query rewriting in RAG?
Fix the question before searching
Query rewriting: The model rewrites a vague or chatty question into a clean search query first.
Loading the animation…
Say it like this Query rewriting turns a vague or chatty question into a complete search query before searching, so the search finds what the person meant, not just the words they typed last.
Under the hood — for builders
- A real follow-up, rewritten
- CANARD (EMNLP 2019): people rewrote 40,527 chat follow-up questions so each one makes sense on its own. From the paper's Figure 1:
- What the rewriter gets
- One short model call before the search: the recent chat plus the new message in, one search query out. This card's chat, with a prompt of our own wording:
- Real impact
- Ma et al., EMNLP 2023 (“Rewrite-Retrieve-Read”): ChatGPT answers with Bing search. Score = exact match: the share of answers identical to the correct one. Higher is better:
- Another way: HyDE
- Hypothetical Document Embeddings (Gao et al., 2022). A model writes a made-up answer page; that page is turned into an embedding (a list of numbers capturing its meaning, see Embeddings), and the search finds real pages close to it. From the paper's Figure 1:
- Kinds of rewrite
- Make it stand alone (this card, CANARD) · reword for the search engine (Ma et al.) · search with a made-up answer (HyDE).
- Measure it
- recall@k: how often the right page lands in the top k results (say, top 5). Compare it with and without rewriting on real follow-up questions.
- Watch out
- Every search waits for one extra model call. A rewrite can change the meaning: log and spot-check them. Skip it when the question already stands alone.
- Sources
- Elgohary, Peskov & Boyd-Graber, Can You Unpack That? (EMNLP 2019) · Ma, Gong, He, Zhao & Duan, Query Rewriting for Retrieval-Augmented LLMs (EMNLP 2023) · Gao, Ma, Lin & Callan, Precise Zero-Shot Dense Retrieval without Relevance Labels (2022).
What happens in the query rewriting animation, step by step?
- A customer asks the support bot about refunds, then follows up: "…and what about Germany?" On its own, that last message barely says anything.
- The bot hands the follow-up to search as-is. The fetcher only sees "Germany", runs to the Germany shelf and proudly brings back a football kit.
- Your turn: tap the note. The bot will rewrite it using the chat so far.
- The bot reads the earlier messages and rewrites the note as a complete question that stands on its own.
- Same fetcher, better note: it heads straight for the Refunds shelf, and the answer is right.
What everyday example explains query rewriting?
A librarian asking "the 2023 policy or the current one?" before walking to the shelf.
Why should a team care about query rewriting?
Essential for any chat-style RAG.