esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

What Are Query Rewriting and Expansion?

What query rewriting and expansion are, how they close the gap between a user's words and your documents, and what they cost, for your AI interview.

In short

Query rewriting and query expansion change the user's question before the search runs. Rewriting writes a clearer version of the question, sometimes a few alternatives, while expansion adds related terms or extra query variants, so the search finds documents written in different words.

11 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Query rewriting and expansion change the question before it reaches the search. People ask in their own words, but documents use the company's words, so a plain search can miss. Rewriting writes a clearer version of the question, or a few alternatives, using the earlier chat when needed. Expansion runs several versions or adds related terms, then merges the results. Both cost an extra model call and can drop exact codes or drift off topic. So I keep the original query in the search too, skip rewriting for exact lookups, and measure the results.

What are query rewriting and expansion, in plain words?

A search engine matches the words in a query against the words in your documents. People ask in their own words. Documents use the company's words. Query rewriting and query expansion close that gap before the search runs.

Think of a bank teller. A customer asks about sending money abroad, and the teller knows the form sits under international transfers. The teller turns the request into the bank's own words. Query rewriting does the same job for a search engine.

Here is the AI version. The HR assistant in the worked example gets a question about phone bills after a trip, and the policy says telecom, not phone.

Key words

Query rewriting
Turning the question into a clearer search query, or a few, often with a model.
Query expansion
Adding related terms, or running several query variants and merging the results.
Recall
The share of the right documents that the search actually finds.
Query drift
The search wandering off the topic the user asked about, after a bad rewrite.
Exact match
Finding the same word or code, such as a policy ID, word for word.

How does query rewriting work?

A model reads the message, plus the chat so far, and writes search queries. Microsoft's Foundry IQ service (Azure AI Search) runs this step and describes it in its own docs. It also says the step is in preview.

  • “The Foundry IQ service sends the search query (or a variation of it) to a generative model that generates alternative queries.”

    Microsoft, Foundry IQ (Azure AI Search) docs

  • “The rewritten queries might not contain all of the exact terms the original query had. This might impact search results if the query was highly specific and required exact matches for unique identifiers or product codes.”

    Microsoft, Foundry IQ (Azure AI Search) docs

One question, clearer search queries

What goes in

Staff question

the words the person typed

Earlier chat turns

tell what a follow-up refers to

Rewriter model

writes clearer search queries

Clearer queries

uses the policy's words, typos fixed

Policy index

policy text store, searched with both

Right policy found

the answer comes from a real policy

  1. 1The staff question goes to the rewriter.
  2. 2The earlier chat goes too, so follow-ups make sense.
  3. 3The model writes clearer queries in the policy's words.
  4. 4The search runs on those queries and the original, against the policy index.
  5. 5The best-matching policy comes back as the answer.
The staff question and the earlier chat go into a rewriter model. It writes clearer queries, and the search runs on them and the original against the policy index. The best match comes back as the answer, drawn from a real policy.
  1. 1

    Read the chat too

    A follow-up such as what about last month only makes sense with the turns before it.

  2. 2

    Write clearer queries

    Fix typos, use the policy's words, and drop the chatter.

  3. 3

    Keep the original query

    Microsoft's Foundry IQ service searches with the original query and the rewritten ones together. Do the same, so exact words still count.

How does query expansion differ?

Expansion starts from the same question but makes a wider search. It adds related terms, or it writes several query variants, searches with each one, and merges the lists. Its aim is recall, the share of right documents a search finds.

  • “Query expansion is a widely used technique to improve the recall of search systems.”

    Jagerman and colleagues, 2023 paper

  • “The document captures relevance patterns but is unreal and may contain false details.”

    Gao and colleagues, 2022 paper (HyDE, a made-up answer method)

Expansion: many queries, one merged list

Staff question

the words the person typed

Several queries

Travel phone claims

a policy-style wording

Mobile phone bill claim

the policy's own terms

Phone bill from trip

the user's wording, kept

Merged list

duplicates removed, then ranked

Best matches

passed to the ranking step

  1. 1The question is turned into several search queries.
  2. 2All the versions search, and their hits are merged into one list.
  3. 3The best matches go on to the ranking step.
One question becomes several queries, and their results are merged into one list with duplicates removed. Then the ranking step puts the best matches first. More variants find more right documents, but each one can also bring in noise.
  • Rewriting

    One or a few clearer queries

    Writes a clearer version of the question, and searches with it plus the original. One rewrite can drop an exact term, such as a policy code.

  • Expansion

    Many queries, merged

    Finds more right documents, but costs more search time and can bring in noise.

  • HyDE

    A made-up answer

    The model writes a fake answer based on the question, then searches for real text that looks like it.

Papers from 2023 tried both ways. Ma and colleagues had a model write the query for a web search. Wang and colleagues had a model write pseudo-documents, which are made-up passages, and added them to the query.

What goes wrong, and what does it cost?

Here is a small, made-up version of the HR assistant. The index holds a few policy lines. A plain search of the staff question finds nothing, and a model-written rewrite finds the right lines. The model call is stubbed out, so you can run the code as it is.

Python
# A made-up HR policy index. Search matches shared words only, so word gaps show up.
POLICY = {
    "P1": "Mobile and telecom expenses on official travel are reimbursed up to a monthly cap.",
    "P2": "Travel must be booked through the approved portal.",
    "P3": "Claims are filed within 30 days of return.",
}

def search(query):
    words = set(query.lower().replace("?", "").split())
    return [pid for pid, text in POLICY.items()
            if words & set(text.lower().replace(".", "").split())]

# A model would write these rewrites. Here they are typed in by hand.
question = "can I claim my phone bill from my Pune trip?"
rewrite = "reimbursement for mobile telecom expenses on official travel"
expansion = ["claims for travel phone expenses", "mobile phone bill claim policy"]

print("question:", search(question))
print("rewrite:", search(rewrite))
for q in expansion:
    print("variant:", search(q))

Output

question: []
rewrite: ['P1', 'P2']
variant: ['P1', 'P2', 'P3']
variant: ['P1']

The rewrite finds the telecom policy, and also the travel booking rule, because that line shares the word travel. The expansion finds more, and some of it is noise.

  • Delay and money

    Every search waits for an extra model call.

  • Lost exact terms

    A rewrite can drop a policy code, a product ID or a name that must match word for word.

  • Noise

    Expansion brings in documents that share one word but answer a different question.

  • Drift

    A bad rewrite changes what the user asked, and nobody notices unless someone checks.

Tip

Asked how to improve retrieval? Don't say rewriting always helps. Say you keep the original query, skip rewrites for exact lookups, and test before and after on real questions.

How would you set up rewriting for that HR assistant?

  1. 1

    Log every rewrite

    Store the question, the rewrite and the hits, so you can see what changed and why.

  2. 2

    Keep the original query

    Search with both the question and the rewrite, so exact words still count.

  3. 3

    Skip codes and IDs

    If the question holds a policy code such as HR-7.2, search it as typed.

  4. 4

    Test before and after

    Label a set of real staff questions with the right policy, then compare hits with and without rewriting.

Time the extra model call in the logs, so the delay has a number on it before anyone argues about it.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: rewrite the question, or search several versions of it, so the search finds more.
  • The gap it closes: users and documents use different words.
  • Rewriting versus expansion, with one example of each.
  • The costs: an extra model call, lost exact terms and noise.
  • Measurement on a labelled set of real questions, before and after.
  • Keep the original query, and skip rewriting for exact codes.

Common traps

  • Trap"Rewriting always makes search better."

    BetterIt can help or hurt. Measure on real questions, and keep the original query too.

  • Trap"Just use a bigger model to rewrite."

    BetterA bigger model adds cost and delay, and it can still drop an exact code. Fix the rules first.

  • Trap"Expansion is free, it is only more queries."

    BetterEach extra query costs search time, and the merged list can hold more noise. Check the top results.

Can you spot the risky rewrite?

Spot the risky rewrite

A staff member asks about the phone cap on business trips and types the policy code HR-7.2. Which rewrite is the risky one?

Quick check

Exact lookups started failing after you added a rewriting step. What is the first fix?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q123What is hybrid search (BM25 + vectors)?11 min
  2. Q124What is BM25?12 min
  3. Q126Rerankers: cross-encoder vs bi-encoder?12 min
  4. Q128What is HyDE?Coming soon
  5. Q131Retrieval metrics: recall@k, MRR, nDCG?12 min

Last updated on 26 November 2026

Building search or an assistant over company documents?

I design and build AI products and agents, including the interface and the system behind it. Book a short call and tell me what you need.