Query Transformation

Learn how rewriting, normalization, expansion, multi-query retrieval and decomposition turn short, messy user questions into queries that retrieve better.

20 minIntermediateCode Examples
The Core Idea: Users write questions the way they speak: short, vague, full of abbreviations, often dependent on the previous message. Documents are written the opposite way: formal and complete. Query transformation closes that gap by improving the question before retrieval. Even a perfect retriever cannot find what the query fails to describe.

1. Beginner: Why Questions Need Help

Compare what people type with what the documents say.
User typesDocument saysProblem
How much PTO do I get?Employees receive 20 days of paid time offAbbreviation the document never uses
holiday allowancevacation policyDifferent word for the same idea
And for the Pro one?The Pro plan costs 25 dollarsMeaning depends on the previous message
WFH rules and 2FA setupTwo separate policiesTwo questions packed into one
Where it fits: transformation runs between the user and the retriever. It changes the query only; the knowledge base and the index stay untouched.

2. Beginner: Normalization and Rewriting

Query normalization cleans and standardizes the text: fixing case and spacing, correcting obvious typos, and expanding abbreviations into the words the documents use. It is cheap and often done with simple rules or a lookup table.
Query rewriting goes further and rewrites the whole question so it can stand alone. In a conversation the latest message is often incomplete, so the system uses the chat history to produce a self-contained version.
Latest messageRewritten standalone question
And for the Pro one?What is the price of the Pro plan?
Does it work offline?Does the mobile app work without an internet connection?
Do not skip this in chatbots: a follow-up like what about the second one is meaningless to a retriever. Without rewriting, multi-turn assistants retrieve poorly after the first message.

3. Intermediate: Query Expansion

Query expansion adds related words to the query so more relevant documents can match: synonyms, alternate spellings, related terms and abbreviations with their long forms.
Where it helps most
Keyword and hybrid search, where documents must contain the query words to match.
Where it helps less
Dense retrieval, which already matches meaning, though expansion can still add missing context.
The danger
Query drift: too many added terms pull the search toward a different topic.
A safe pattern keeps the original query and adds expansions with lower weight, or runs them as separate searches. Never replace the user wording without a measured reason.

4. Intermediate: Multi-Query Retrieval

In multi-query retrieval the system produces several versions of the question, often with a language model, runs a search for each, and merges the results. Different phrasings reach different chunks, so the union has higher recall than any single search.
1. Generate 3 to 5 variants: rephrased, more formal, with synonyms.
2. Always include the original question.
3. Search with every variant.
4. Merge by removing duplicates, or by fusing the rankings with reciprocal rank fusion.
5. Pass the merged list to the reranker.
Cost: every variant is another search, and generating variants may add a model call. Latency and expense multiply, so use it where recall problems are proven.

5. Advanced: Decomposition, Routing and Quality Control

Query decomposition splits a compound question into simple ones that are each searched separately, and the results are combined. A question such as WFH rules and 2FA setup becomes two focused queries, each of which finds its own policy. A single embedding of the combined text would point somewhere in between and may find neither.
Related ideas include routing a query to the right index or tool, and deciding that no transformation is needed. Simple, clear questions should pass through untouched.
TransformationAddsTypical cost
NormalizationConsistent wordingAlmost free
RewritingStandalone meaningOne model call
ExpansionExtra vocabularyLight, or one model call
Multi-queryHigher recallSeveral searches
DecompositionCoverage of compound questionsSeveral searches plus merging
Prove each step: measure retrieval quality with and without every transformation on your test questions, and keep only those that help. Log the original and the transformed queries so you can debug surprising results.

6. Visual Explanation: Reshaping the Question

Panel A shows the transformation stage in the pipeline. Panel B lists real before and after queries from the code below. Panel C is a technique explorer.
Panel A · Where Transformation SitsBefore retrieval, after the user
User question→Normalize→Rewrite→Expand or split→Retriever
Panel B · Before and AfterResults from the code in section 7
Original queryTransformed queryResult
How much PTO do I get?how much paid time off do i getfinds d1, was empty
holiday allowance for staffvacation allowance for stafffinds d1, was empty
WFH rules and 2FA setuptwo sub-queries, each normalizedfinds d2 and d3, was empty
Panel C · Technique ExplorerClick a technique to open it
Click to see why the original query is always kept▼
A transformation can fail, for example by expanding in the wrong direction. Keeping the original wording in the mix guarantees that the system is never worse at finding what the user literally typed.
How to read it: Panel A is the position of the stage, Panel B shows its effect on the example queries, and Panel C lists what each technique fixes and what to watch.

7. Python Implementation: Normalize, Merge Variants and Decompose

The program searches four short policy documents with a simple word-match retriever. Each query is searched as typed, and then again after normalization, where abbreviations and synonyms are replaced with the words the documents use. Results from the original and the normalized query are merged, as in multi-query retrieval. The last lines split a compound question into two sub-queries and search each one.
query_transform.pyPython 3.10+ · Standard library only
rewrites = dict([("pto", "paid time off"), ("wfh", "work from home"), ("2fa", "two factor authentication"), ("holiday", "vacation")])stop_words = set(["how", "many", "do", "i", "get", "a", "the", "what", "is", "my", "of", "and", "for", "to", "can", "does"]) documents = [    ("d1", "vacation policy employees receive 20 days of paid time off per year"),    ("d2", "work from home policy remote work is allowed two days per week"),    ("d3", "security policy enable two factor authentication on every account"),    ("d4", "expense policy submit receipts within 30 days"),] def words(text):    return "".join(ch if ch.isalnum() or ch == " " else " " for ch in text.lower()).split() def normalize(query):    return " ".join(rewrites.get(w, w) for w in words(query)) def search(query, k=2):    q = set(w for w in words(query) if w not in stop_words)    scored = [(len(q & set(words(text))), doc_id) for doc_id, text in documents]    scored.sort(key=lambda s: (-s[0], s[1]))    return [doc_id for score, doc_id in scored[:k] if score > 0] def multi_query(query):    variants = [query, normalize(query)]    seen = []    for variant in variants:        for doc_id in search(variant):            if doc_id not in seen:                seen.append(doc_id)    return variants, seen def decompose(query):    parts = query.lower().split(" and ")    return [p.strip() for p in parts] for query in ["How much PTO do I get?", "holiday allowance for staff", "WFH rules and 2FA setup"]:    variants, merged = multi_query(query)    print("Query:", query)    print("  Original search :", search(query))    print("  Normalized text :", normalize(query))    print("  Multi-query merge:", merged) print("")parts = decompose("WFH rules and 2FA setup")print("Decomposed into:", parts)combined = []for part in parts:    for doc_id in search(normalize(part), k=1):        combined.append(doc_id)print("One search per sub-question:", combined)
Expected output
Query: How much PTO do I get?  Original search : []  Normalized text : how much paid time off do i get  Multi-query merge: ['d1']Query: holiday allowance for staff  Original search : []  Normalized text : vacation allowance for staff  Multi-query merge: ['d1']Query: WFH rules and 2FA setup  Original search : []  Normalized text : work from home rules and two factor authentication setup  Multi-query merge: ['d2', 'd3'] Decomposed into: ['wfh rules', '2fa setup']One search per sub-question: ['d2', 'd3']
The original queries find nothing because users wrote PTO, holiday and WFH, which the documents never use. After normalization the vacation policy and the other policies are found. The compound question works only when decomposed, since each half maps onto a different document. A real system would use a language model for rewriting and decomposition, but the structure is identical.

Knowledge Check

Q1. Why is rewriting essential in a chatbot?
Q2. What is query drift?
Q3. Why is the original query kept in multi-query retrieval?

Key Points

✓
Query transformation improves the question before retrieval, leaving the index unchanged.
✓
Normalization standardizes wording; rewriting makes follow-ups standalone using chat history.
✓
Expansion adds related terms, mainly useful for keyword search, and can cause query drift.
✓
Multi-query retrieval searches several variants and merges the results for higher recall.
✓
Decomposition splits compound questions so each part finds its own evidence.
✓
Always keep the original query, and use simple questions as they are.
✓
Measure each transformation on test questions and log original and transformed queries.

The Big Picture

Retrieval can only match what the query expresses. Query transformation is a low-cost way to make the query express more of what the user means, and it often delivers more improvement than tuning the retriever itself.
Raw question → Clean → Make standalone → Expand or split → Retrieve
Up Next: Advanced Query Strategies. You will learn HyDE, step-back prompting, sub-question retrieval and iterative multi-step retrieval for questions that a single search cannot answer.