recallA retrieval
field guide

Query Rewriting, Expansion and HyDE

Build this skill

Improve retrieval with query rewriting, expansion and HyDE, and measure whether each helps.

People do not type good search queries

Real questions are short, vague, misspelt and full of local slang. "hostl fee kab tak", "pg ka exam re-eval??", "sick leave rule" or just "leave". The documents they should match are long, formal and carefully worded. Retrieval works best when the query looks like the passage it should find, so a powerful idea is to improve the query before searching.

Three techniques do this, usually with the help of a language model: query rewriting, query expansion and HyDE. Each adds one model call before retrieval, which costs a little time and money, and each can noticeably improve what retrieval finds.

User's questionshort, messyTransformrewrite · expand ·HyDERetrievewith the betterqueryGenerateanswer the originalquestion

The transformed query is only used for searching. The answer still responds to what the user actually asked.

Query rewriting

Query rewriting asks a language model to turn the user's question into a clear, complete search query: fix spelling, expand abbreviations, translate mixed-language text, and remove filler. For example:

User typed Rewritten for search
hostl fee kab tak hostel fee payment due date
pg ka exam re-eval?? postgraduate exam re-evaluation procedure
can i go home 4 diwali Diwali holiday dates and hostel opening during the break

Rewriting is cheap and helps most with messy, informal or code-mixed queries. Its risk is that the model may change the meaning, for example rewriting "PG" as "paying guest accommodation" instead of "postgraduate". Keeping the original question alongside, and searching with both, limits the damage.

Query expansion

Query expansion adds related words or alternative phrasings to the query, so that documents using different vocabulary still match. Instead of a hand-written synonym list, a language model suggests the expansions for each question: "sick leave rule" becomes "sick leave rule; medical leave policy; absence due to illness; medical certificate". Expansion helps keyword search the most, because it directly addresses vocabulary mismatch.

HyDE: search with a hypothetical answer

HyDE stands for Hypothetical Document Embeddings. It was introduced by Luyu Gao and colleagues in a 2022 paper. The idea is clever: a question and its answer often look quite different, but an answer looks like other answers. So instead of embedding the question, HyDE asks a language model to write a short, made-up answer to the question, and embeds that to search.

For "can I get my paper rechecked?", the model might write: "Students can apply for re-evaluation of their answer script within a few days of results by paying a fee to the examination cell." This invented answer may have wrong details, but it is written in the vocabulary of a real policy ("re-evaluation", "answer script", "examination cell"), so its embedding lands right next to the real re-evaluation passage.

The made-up answer is used only for searching. It is never shown to the user, and the final answer is still written from the real retrieved passages.

Compare the three techniques on one question below.

Measuring whether it helps

Each technique adds a model call to every question, typically a few hundred milliseconds and a small cost, so it should earn its place. Measure Recall@K on your test questions with and without it. Query transformations tend to help most on informal, short and multilingual questions, and least on questions that are already clear.

The program below simulates a measurement: for each test question it checks whether the correct page appears in the top 3 before and after rewriting. The rewrites are written by hand here, standing in for a language model's output.

python · runs in your browser

Expected output:

text
Recall@3 original:  3/4
Recall@3 rewritten: 4/4

found counts the words a query shares with each page, takes the three pages with the most, and reports whether the correct page is among them with at least one shared word. hits runs that check for all four questions, using either the original wording or the rewritten one. The misspelt and code-mixed questions only reach the right page after rewriting.

Follow your curiosity.

↑ ↓ to explore · Enter to open · Escape to close