Query Rewriting

LLM foundationsRetrieval and dataPublished By Simon Budziak

Query rewriting changes a user's original question into one or more retrieval queries that better match the language, structure, and terminology of a knowledge base. A RAG system uses the rewritten query to find stronger evidence while preserving the user's actual intent, filters, and permissions.

The Rewrite-Retrieve-Read research formulation puts rewriting before retrieval because casual user language and document language often do not line up. Rewriting closes that gap. It must not silently rewrite the scope of the request.

How does query rewriting help RAG?

An LLM can resolve pronouns from chat history, add an official product name, split a compound question, or create a query in the language used by source documents. The goal is better retrieval, not a prettier question. RAG then retrieves against the rewritten form, and semantic search helps when the useful passage uses different words again.

What can go wrong with a rewrite?

A rewriter can drop a qualifier or invent a detail, which sends retrieval toward persuasive but irrelevant text. Keep the original question in the trace and preserve structured metadata filtering outside the model’s rewrite. Measure the retrieved evidence and final answer with and without rewriting. In an agentic search loop, the agent can retry only after it sees that the first retrieval did not answer the question.

Frequently asked questions

Is query rewriting the same as query expansion?

No. Rewriting reformulates one question, while expansion adds terms or creates multiple queries to broaden recall. A retrieval system can use both.

What should never be rewritten?

Structured constraints such as tenant, date, document type, and permission filters should remain explicit, because changing them can expose the wrong information.

Summarize this page with

Train your team to build this