Skip to content

Query Rewriting

Transforming a user query before embedding it for retrieval, often using an LLM to expand, clarify, or decompose the original query.

Query rewriting is the technique of transforming a user’s query before it is embedded and used for retrieval, so that the search becomes more effective. Rather than searching with the raw query as typed, the system reformulates it — often using a language model — to clarify, expand, or restructure it for better matching.

This helps because the way users phrase questions does not always align well with how relevant documents are written. A terse or ambiguous query may not retrieve the best material directly. Rewriting can expand abbreviations, add implied context, split a complex question into simpler sub-queries, or rephrase it into a form closer to the language of the target documents, improving the relevance of what comes back.

Query rewriting is a common component of advanced retrieval and agentic RAG pipelines. It may be combined with techniques like generating multiple query variations and merging their results, or with HyDE, which rewrites the query into a hypothetical answer before searching. The cost is an extra processing step, usually a model call, in exchange for noticeably better retrieval on hard or poorly phrased queries.