What is query rewriting?
Query rewriting is a technique that turns a user's question, usually with a language model, into a form better suited to retrieval before search: it fills in missing context, removes ambiguity and drops needless words.
It differs from query expansion in how much the query changes: query expansion keeps the original and adds terms, while query rewriting produces a new sentence. Its most common use in chat is making a question that refers to earlier messages understandable on its own.
- Topic
- Embeddings, document processing and RAG
- Also known as
- Query reformulation
How query rewriting works
- The question, with earlier chat messages if needed, goes to a language model or rule set.
- Ambiguous phrases, pronouns and missing subjects are identified; for “what about last year?” the topic is taken from the previous message.
- The model writes a search-ready query that stands alone while keeping meaning and scope.
- The rewritten query is used for search, the user's original question for answer generation.
In more detail
Topic shift and resolving pronouns and ellipsis
In multi-turn chat, rewriting mainly performs decontextualisation: resolving pronouns ('it', 'that policy'), ellipsis ('what about last year?') and topic shifts across turns. Crucially, when a user switches to a completely new topic, the rewriter must avoid dragging irrelevant entities from earlier turns into the new search query.
Conditional triggering and latency optimisation
Using an LLM to rewrite every incoming turn adds latency even when the user's message is already self-contained (for example, the first turn of a chat). Production systems often gate rewriting behind a lightweight classifier, heuristics (checking for pronouns or short length) or pass the standalone check directly to skip unnecessary calls.
Retrieval-tuned rewriting (Rewrite-Retrieve-Read) and dual rewrites for hybrid search
As Ma et al. (2023) showed with Rewrite-Retrieve-Read, a small language model can be fine-tuned or aligned with reinforcement learning using downstream retrieval and QA accuracy as the reward, specifically to write search-friendly queries rather than human-sounding questions. Combining a lexical keyword rewrite for BM25 with a natural-language rewrite for dense vector search often yields stronger hybrid retrieval than a single rewrite.
Evaluation measures and preserving identifiers
Rewriting quality is evaluated on benchmarks such as QReCC and TREC CAsT using both surface similarity against gold rewrites and downstream retrieval metrics (Recall@k, MRR, nDCG). In enterprise monitoring, A/B comparing retrieval hits before and after rewriting catches cases where the rewriter drops critical identifiers such as product codes or regulation numbers.
Commonly confused terms
| Term | How it differs |
|---|---|
| Query decomposition | Query rewriting produces one improved query for a single search, whereas query decomposition breaks a multi-part complex question into multiple distinct sub-questions answered sequentially or in parallel. |
| Prompt rewriting | Prompt rewriting (or prompt optimisation) modifies system or user instructions to improve an LLM's generation behaviour, whereas query rewriting modifies the search query specifically to improve document retrieval. |
Why query rewriting matters for on-premises AI
Users of in-house assistants ask short questions that only make sense in context; searched as they are, they return irrelevant results. Rewriting improves retrieval but must keep the original intent; permissions are enforced in the retrieval layer, never left to the rewriter. Each rewrite is an extra model call, often handled well enough by a small in-house model. Rewritten queries are logged so errors can be tracked.
In a holding company's HR assistant an employee asks about annual leave rules, then writes “what about maternity leave?”; the system rewrites it as “how is annual leave entitlement calculated during maternity leave” before searching.
Common questions
How does query rewriting differ from query expansion?
Query expansion adds terms to the original query. Query rewriting turns it into a new sentence, used to complete incomplete questions in a chat.
Can rewriting distort the user's question?
It can; the model may misread intent. The original question is therefore kept for answer generation, rewritten queries are logged and samples are reviewed regularly.
Related terms
- Query expansion
- Query decomposition
- Multi-query retrieval
- Hypothetical Document Embeddings
- Retrieval-Augmented Generation
- Conversation history
Sources
- Ma et al., Query Rewriting for Retrieval-Augmented Large Language Models, 2023
- Gao et al., Retrieval-Augmented Generation for Large Language Models: A Survey, 2023
In the glossary: Glossary, M to Q