Rewriting the question before searching
Users type short, vague or half-finished questions. Rewriting them into good search queries often helps more than a better database.
Words on this page (9)
- retrieval
- : Finding the most relevant pieces of text for a query.
- policy
- : A rule for choosing actions. In a language model, the model itself is the policy.
- chunk
- : A small piece of a document, sized for indexing and retrieval.
- reciprocal rank fusion
- : A way to merge two ranked result lists by adding 1 / (k + rank) for each list.
- prompt
- : The input text you give a model, including instructions, examples and data.
- recall
- : Of the truly positive cases, the share the system found.
- latency
- : The time you wait for a response.
- logarithm
- : The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.
- vocabulary
- : The full list of pieces (tokens) a model knows how to read and write.
What it is
People do not write search queries. They write "and for gift cards?" in the middle of a chat, "it's not working" with no detail, or two questions in one. A rewrite step uses a small model call to turn the raw text into something can use. Common techniques:
Standalone rewrite: use the chat history to remove pronouns and fill in the gaps. Multi-query: write three different phrasings, search each, and merge the results. Decomposition: split a two-part question into two searches. Step-back: ask the broader question behind the specific one. HyDE: have the model write a short hypothetical answer, then search with that text, because an answer looks more like a document than a question does.
A worked example
Chat: "What is the refund window?" The bot says 30 days. The user asks: "And for gift cards?" Searching that text returns random gift card pages. The rewrite step, given the history, produces: "What is the refund for gift cards?" and now the right is found. For "How do I recover my account?", a multi-query rewrite searches "reset password", "locked out of account" and "forgot username", then merges the three lists with .
How to write the rewrite step
The rewriter is a small with clear rules:
- Give it the last few chat turns and the new message.
- Ask for one standalone question with no pronouns.
- Tell it to keep every name, number, date and place exactly.
- Tell it to return the question unchanged if it is already clear.
Keep the original question too. If the rewrite goes wrong, you can fall back to searching the user's own words.
A second example: when a rewrite hurts
A user types "ERR_4021 refund". A helpful rewriter might turn it into "problems with payment errors". The exact code is gone, so keyword search can no longer find the one page that mentions it. Short, precise queries with identifiers are best left alone. HyDE has a related risk: the invented answer may contain wrong facts, and those wrong facts then steer the search toward the wrong documents.
What goes wrong
The symptom is that drops for questions that used to work. Another sign is a rewrite that answers instead of asking, such as returning "30 days" in place of a question. A third is cost: multi-query runs three searches instead of one, so and spend rise.
How to tell it is working
the original question and the rewrite side by side for 20 real conversations and read them. Then compute recall at 5 on your test set with and without the rewrite step, split by question type: follow-ups, vague questions and clear questions. Keep the step where it helps and skip it where it does not.
When to use it
Add it when your test set shows misses on follow-ups, vague questions or mismatches between users and documents. It costs one extra model call, so use a small fast model and skip it for clear, self-contained questions. Measure recall before and after.
Common mistake
A rewrite that drops a constraint. "Refund policy for orders over $100 in Canada" must not become "refund policy". Tell the rewriter to keep every name, date, number and place.
Someone asks the company bot about parental leave, then follows up with "what about adoption?" The bot, searching that follow-up on its own, returns a page about the adoption of a new software tool. A rewrite step that adds "parental leave" would have found the right policy in one try.
Check yourself
A user asks "And for gift cards?" mid-chat and search returns junk. What fixes it?
Not quite. The follow-up depends on earlier turns. A standalone rewrite makes it searchable.
What is the idea behind HyDE?
Not quite. A short invented answer shares more words and meaning with the real document than the question does.
What must a good rewrite keep?
Not quite. Dropping a constraint such as a region or an amount changes what is searched for and returns the wrong passages.
You got 0 of 3 right.
Go deeper
- Paper
Gao et al. · The HyDE paper: search with a hypothetical answer.
- Paper
Ma et al. · Shows that learning to rewrite queries improves answers.
- Repo
Nir Diamant · Notebooks for query transformation and other retrieval improvements.
Spotted a mistake or something unclear? Report it on GitHub.