Query Rewriting

Query rewriting is the transformation of a user’s query into one or more different queries before retrieval runs, on the grounds that what a person types is often a poor input to a search.

The gap it addresses is between how questions are asked and how documents are written. A query may be a fragment, may carry a pronoun referring to an earlier turn, may contain two questions, or may use the vocabulary of the person asking rather than the vocabulary of the corpus. The retrieval step matches whatever it is given, so the phrasing of the query is a determinant of what can be found.

The rewrite happens entirely on the query side. Nothing about the stored collection changes, which is what distinguishes the practice from ingestion-time transformations and makes it possible to apply without re-indexing.

The term covers a wide range of operations, from mechanical substitution to generating a fresh query with a language model. What they share is position in the pipeline, not method.

In practice

The forms encountered most often, in rough order of how much machinery they require:

Normalisation. Case folding, punctuation handling, and stemming applied to the query so it matches the treatment the corpus received. Mandatory on the lexical side and not usually described as rewriting at all, though it is.

Expansion. Adding terms — synonyms, abbreviation expansions, spelling variants — so that a term-matching method can hit text using different words. The classical technique, and one that predates learned retrieval by decades. See sparse retrieval.

Contextualisation. Rewriting a follow-up turn into a self-contained question, so that “and the second one?” becomes a query that can be searched on its own. Necessary in any conversational system, because a retriever has no access to the conversation.

Decomposition. Splitting a query containing several questions into separate queries, each searched independently, with the results combined.

Reformulation into corpus register. Generating a statement resembling how the answer would be written, rather than how the question was asked, on the theory that a passage resembles a declarative sentence more than an interrogative one.

Whichever form is used, the rewrite sits on the critical path before retrieval, so a rewrite performed by a language model adds a model call’s latency to every request. It also introduces a failure mode that did not previously exist: a rewrite can discard the term that mattered, and the resulting miss is attributed to retrieval, because the query the index actually received is not the query the user typed. Systems that log both are able to tell the difference; many log only one.

Commonly confused with

Query expansion. One form of rewriting — adding terms without replacing the original. Expansion is a subset; rewriting also covers replacement, splitting, and translation. Older information-retrieval writing uses “expansion” for the whole area because expansion was the whole area.

Prompt engineering. Concerns the instructions given to the generation model. Query rewriting concerns the text given to the retriever. Both are edits to text before a model or index sees it, and they occur at opposite ends of a retrieval-augmented request.

Chunk enrichment. Rewriting or annotating stored text at ingestion so it retrieves better — adding a heading path or a summary to each chunk. It targets the same mismatch from the other side, requires re-indexing, and is not query rewriting. See chunk.

Reranking. Happens after retrieval and reorders results. Query rewriting happens before and changes which results exist. A rewrite can surface a document that reranking could never reach, because reranking only reorders what was returned. See reranking.

Spelling correction. A narrow special case, and one embedding-based retrieval partly handles by itself, since a misspelling often embeds near its correct form. On the lexical side it remains necessary.

Usage notes

The term is applied to operations of wildly different cost. Appending synonyms from a static list and calling a language model to compose a new question are both query rewriting. A claim that a system “rewrites the query” says nothing about whether a model is involved.

Multi-query retrieval is often described as rewriting, though it produces several queries rather than replacing one. Where several rewrites are searched in parallel and their results merged, the merge is a fusion step with the same problems as any other. See hybrid search.

In web-search literature the phrase has a narrower history, referring to reformulations learned from logs of what users typed next after an unsuccessful search. That signal does not exist in an internal corpus, so the techniques transfer only partly.

Generating a hypothetical answer and searching with that is a recognised variant, sometimes given its own name. It is a rewrite in the sense used here: the text sent to the retriever is not the text the user supplied.

See also

Retriever · Semantic search · Sparse retrieval · Reranking