New Tool Radar

AI Concepts

How to Rewrite a User Query Before Search So the Retriever Matches the Documents Wording

Turn the user question into the wording the documents use, search with the rewritten query, and check the right chunks rank higher.

Schematic with three steps: Rewrite the question into document wording; Search with the rewritten query; Keep the rewrite only when it helps.

Users ask questions in their own words, but documents use different terms, and an embedding search compares the query vector with chunk vectors, so a wording gap becomes a retrieval miss. Query rewriting uses a language model to restate the question in the vocabulary the documents use, then searches with the rewritten form. LlamaIndex describes rewriting as a core query transformation: keep the same tools, but rewrite the query in different ways to execute against them.

What you need

  • A set of questions that retrieve poorly in their original wording
  • A language model you can call to rewrite queries
  • The documents wording for the same topics, to judge the rewrite

Step 1: Rewrite the question into document wording

Ask the model to restate the question using the terms the documents use, for example turning a casual question into the product name and feature name the manual uses. Keep the meaning identical; a rewrite that changes the question answers a different question.

Step 2: Search with the rewritten query

Embed the rewritten query and run the same retrieval as before, with the same top-k. Compare the ranked lists side by side with the original query results.

Step 3: Keep the rewrite only when it helps

Score both versions on the test set. Keep rewriting for the question types where it raises the hit rate, and skip it where it does not, since every rewrite costs a model call.

Worked example: synthetic query rewriting on 4 questions

The table below is a synthetic example. It shows the expected result of rewriting queries; no search was run.

Original question Rewritten query Result
How do I get my money back? Refund policy return window Found
Why is it so slow? Performance troubleshooting latency Found
Can I change my plan? Subscription plan change billing Found
What time are you open? Support hours opening times Missed

Expected result (UNTESTED). rewriting finds the right chunk for 3 of 4 questions that missed in their original wording. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • A rewrite that changes the meaning retrieves the wrong documents confidently, so verify the rewrite preserves the question.
  • Rewriting adds a model call and latency to every query, so it is not free.
  • Rewriting helps wording gaps, not missing documents: if no document contains the answer, no rewrite will find it.
  • Keep the original query too, so you can compare and fall back when the rewrite fails.

Sources

See all