Notes

Query Rewriting

People do not ask questions the way documents answer them. Someone types “and the second one?”, or “is it covered?”, and a search index has nothing to match. Query rewriting inserts one step before retrieval that turns what the user said into something worth searching for.

What the rewrite actually changes
  • Resolving context: pronouns and follow-ups are filled in from earlier turns, so “its warranty” becomes “the warranty on the X200 pump”.
  • Decomposition: a compound question is split into separate queries, each retrieved on its own.
  • Vocabulary: everyday words are swapped for the terms the corpus uses — a product code for a nickname, terminated for fired.
  • Hypothetical answers: the model drafts a plausible answer and searches with that instead, since an answer resembles the passage containing it more closely than the question does.
Why one extra call earns its keep

Retrieval quality is a ceiling on grounded answers: a generator handed the wrong three passages cannot rescue them. Rewriting usually improves the retrieved set more than upgrading the generator does, and it is cheap — a small fast model, with the rewrite cacheable per turn. It is also the main thing separating advanced RAG pipelines from naive ones.

The failure to watch for

A rewrite that drops a constraint — the year, the region, the product version — retrieves documents that look relevant and are wrong, and the generator will cite them confidently. Logging the rewritten query beside the original is the cheapest debugging habit in a retrieval stack.

Query rewriting transforms a user's question before it reaches the retriever: references to earlier turns are resolved, compound questions are split into separate searches, and everyday wording is replaced by the vocabulary the indexed documents use. Retrieval quality sets a ceiling on a grounded answer, and raw conversational phrasing matches stored text poorly. One cheap model call ahead of the search often improves results more than swapping in a larger generator.

Ask a librarian for “that book about the whale” and she does not type those words into the catalogue. She works out what you mean and searches for something that will actually match. Query rewriting is that librarian. Before an AI assistant goes hunting through documents for your answer, it quietly rewrites your question: filling in what “it” refers to from earlier in the conversation, splitting a two-part question into two searches, and using the words the documents themselves would use. You never see this step happen. You just notice that the assistant found the right page instead of something nearly right.