Query Rewriting
In this article
- Rewrite a follow-up question into a search query that stands on its own, using only turns you can trust.
- Check in code that every product name, negation, date, number and scope in the question survives the rewrite, and that nothing new was added.
- Decide what to do when there is nothing to resolve "it" against, or when the question needs no rewrite at all.
- Measure whether rewriting helps by comparing Recall@3 of raw and rewritten queries on labelled conversations.
- Terms defined here: query rewriting.
Overview
A customer types: "The home one shows E-42. How do I fix it without a factory reset?" A search engine reads only this line, where "the home one" and "it" point at nothing. You will learn to turn a follow-up like this into a query that stands on its own, check in code that the rewrite kept every constraint, and measure whether the extra step pays.
The setting is made up. Brightline, a home-internet provider, runs a support assistant that answers from its help pages and router manuals. Each question goes to a hybrid search, a keyword search plus a vector search that matches by meaning, over short stored pieces of those pages called chunks. The best 3 chunks go into the model's request. The application keeps the conversation so far. A model writes the rewrite; code checks it.
What the search actually sees
Brightline's assistant uses retrieval-augmented generation (RAG): it looks text up, then the model answers from it. The retrieval step gets one string, nothing else.
- Turn 1, customer: "I have two of your routers, a BL-R5 in the office and a BL-R7 at home."
- Turn 2, customer: "The home one shows E-42. How do I fix it without a factory reset?"
Send turn 2 as it is, and the search has "E-42", "fix" and "factory reset", but no router name. With the raw question the top 3 is manual-bl-r7#reset (whose reset erases saved Wi-Fi settings), manual-bl-r5#errors and help-router-setup#1. The reset steps win on "factory reset"; the BL-R5 error table reads almost like the BL-R7 one, and nothing says which router. The chunk with the answer, manual-bl-r7#e42, lands fourth and never reaches the model.
Filling in the missing words
Query rewriting asks a language model to restate a query so that it finds better results. Microsoft's guide to building RAG on Azure lists the problems it targets: a vague query, missing keywords, words that do not help, and unclear meaning. [1] It is one of several optional query translation methods, next to augmentation, decomposition and HyDE. [1]
For a follow-up, the job is narrow: replace each pointing phrase with what it points at, using the earlier turns. Microsoft's decomposition prompt asks for the same move, replacing pronouns such as "it" with the full name of the thing they refer to. [1] Turn 1 says the BL-R7 is the one at home, so "the home one" is the BL-R7 and "it" is the same router. The rewrite becomes:
> BL-R7 error E-42 fix without factory reset
Now manual-bl-r7#e42 ranks first, because "BL-R7" and "E-42" both match it. Brightline's keyword search treats "without" as one more word to match, so the reset chunk may still appear lower down; keeping the word keeps the query asking for a fix that is not a reset.
What must survive
A model asked to rewrite is also tempted to improve. Microsoft's sample rewrite prompt asks the model to keep the original meaning and intent, and in the same list to expand the query with synonyms and related terms. [1] A prompt enforces neither, so Brightline treats the rewrite as a proposal and checks it in code against invariants: things taken from the original that must appear in the rewrite unchanged.
| invariant | taken from turn 2 | found in the rewrite |
|---|---|---|
| entities | E-42, and the router "the home one" resolves to | BL-R7, E-42 |
| negation | without factory reset | without factory reset |
| scope | the home router only | BL-R7 only, BL-R5 absent |
| dates, numbers | none in this question | nothing to check |
The check is plain string work. Product ids and error codes match a pattern of letters, a dash and digits (BL-R7, E-42); negations come from a short word list (not, without, except, no, n't); numbers and dates come from a regex. Each item found in turn 2 must also appear in the rewrite, and scope means the rewrite names no product except the one the question pointed at, so the BL-R5 stays out.
Two parts need care. Code cannot tell by itself that "the home one" means the BL-R7, so the model returns, for each pointer it resolved, the turn words it relied on ("BL-R7 at home", turn 1), and code checks that those words appear in that turn and that they contain the distinguishing word of the pointer ("home" in "the home one"). And the check runs in the other direction too: every product name, version or number in the rewrite must appear in a turn, so the model cannot add something from its own training. An addition like that is a small hallucination inside the query.
Three rewrites to judge
Predict what the checker does with each seeded rewrite, and what the search would pull if it let them through.
- "BL-R7 error E-42 fix factory reset"
- "BL-R5 error E-42 fix without factory reset"
- "BL-R7 firmware 3.2 error E-42 fix without factory reset"
All three are rejected. The first lost "without", so the negation check fails; let through, it asks for a reset, manual-bl-r7#reset comes first and the customer gets steps that erase their Wi-Fi settings. The second guessed the wrong router: its cited words, "a BL-R5 in the office", do not contain "home", so the scope check fails; let through, it pulls manual-bl-r5#errors, a table with no E-42 row. The third added "firmware 3.2", a version no turn names, so the no-new-entity check fails. On rejection the code searches with the original question, which is no worse than not rewriting.
Which turns the model may read
The stored history holds more than the customer's words. In LangGraph's agentic RAG tutorial, where the model itself decides whether to search, the graph's state is a list of chat messages, and the retrieved text arrives in it as a tool message. [2] If the rewrite step reads that list whole, an earlier search result can steer the next query: a BL-R5 passage retrieved two turns ago could put "BL-R5" back into a question about the BL-R7, and a page that contains instructions could inject them.
So Brightline feeds the rewrite trusted turns only: the customer's own turns and the assistant's own replies. Retrieved chunks and tool output stay out, and the no-new-entity check compares against the same trusted turns, so a name that came only from a retrieved page counts as new.
When there is nothing to resolve
A new chat that opens with "How do I fix it without a factory reset?" has no earlier turn. There is no router to find, and a guess would have nothing to check against. The model reports that it cannot resolve "it"; the code then asks "Which router do you have?" or, where asking is not possible, passes the original query. Two candidates count as none: if turn 1 names both routers and turn 2 says only "it", the model reports "it" unresolved. Microsoft's advice for augmentation points the same way: with no context, augment only when the model itself knows the topic, and otherwise leave the query alone. [1]
The opposite case matters as much. "What does E-42 mean on the BL-R7?" already names the router and the code, so it skips the rewrite. A rewrite cannot add what a good query lacks, and a model told to expand with related terms may dilute it; the extra model call also adds its time to the reply. Brightline rewrites only questions with a pointing word, found by a short word list (it, that, there) plus a few phrase patterns such as "the home one". The filter is rough: "Is it true that the BL-R7 supports Wi-Fi 6?" triggers a needless rewrite, and a phrasing no pattern covers slips through. Either miss is cheap: a needless rewrite must still pass the invariant check, and a missed follow-up is searched as typed.
The flow, as pseudo-code (not run):
if not has_pointing_words(question):
return search(question)
turns = trusted_turns(history) # customer turns and assistant replies
rewrite = model_rewrite(question, turns) # text, plus the turn words behind each resolved pointer
if rewrite.unresolved:
return ask_customer("Which router do you have?")
if not check_invariants(question, turns, rewrite):
return search(question) # fall back to the original
return search(rewrite.text)Measuring the gain
Brightline labelled 12 conversations, each with its history, follow-up, an accepted rewrite and the chunks that answer it. Three are seeded with a dropped negation, a guessed router and an added version. The pass rule: the checker rejects all 3 seeded cases and accepts the others.
Recall@3 is the share of a case's relevant chunks that reach the top 3. On the raw follow-ups it was 0.42; on the checked rewrites, 0.79. All 3 seeded rewrites were rejected and fell back to the raw query, which partly explains why 0.79 is not higher. The weak point is size: one of 12 conversations moves the score by about 0.08, so a fresh labelled batch should confirm the gain before anyone quotes it.
The rule at work
Rewrite only follow-ups: resolve pointers from trusted turns, keep every name, negation, date, number and scope, add nothing, and let code decide whether the rewrite is used; when the check fails or nothing resolves, send the original or ask. The same pattern, a model proposes and code checks, returns for typed search filters.
Common mistakes
- Trusting the prompt's "keep the meaning". A dropped "not" or "except" changes the answer; check negations in code.
- Filling a gap with a guess. "There" becomes a city, "it" the more popular router; require cited words from a turn, and ask when there are none.
- Rewriting every query. Standalone questions get slower and no better; rewrite only questions with pointing words.
In production
Log the question, the rewrite, its cited words and the check result, so a bad answer can be traced to its query. Rerun the 12 conversations whenever the rewrite prompt or model changes: a change that lets one seeded case through is a regression even if recall rises.
Sources
- Develop a RAG Solution on Azure - Information-Retrieval Phase (Azure Architecture Center). Microsoft. Checked 21 Nov 2026.
- Build a custom RAG agent with LangGraph. LangChain. Checked 24 Oct 2026.
- LlamaIndex example: Query Transform Cookbook. LlamaIndex. Checked 24 Oct 2026.
Check yourself
Check yourself
Question 1 of 13
What is query rewriting for in a conversational search assistant?
Your first check counts towards your result.