Back
advanced

Advanced RAG & Context

Query expansion: search for the same need in different words

Use rewrites and hypothetical documents to improve retrieval while preserving identifiers, constraints, and uncertainty.

Lesson 39 of 67About 22 min with practice

A learner asks about classes after their shift, while the catalog calls them evening workshops. How can a rewrite bridge that wording gap without inventing when the shift ends? Query expansion should preserve the need while trying another way to search for it.

Before you begin: You understand retrieval, hybrid search, and relevance evaluation.

The danger is equally simple: a rewrite may change the question. “Classes after 6 pm that do not need equipment” must not become “popular equipment classes.” Better retrieval of the wrong intent is still failure.

Preserve the parts that must not drift

Keep exact identifiers, dates, names, negation, and explicit constraints. Store the original query alongside any generated versions. If “after my shift” lacks a time, a rewrite should not invent one; the system may need a clarification or a broader initial search.

Separate query cleanup from query decomposition. Cleanup removes irrelevant conversational clutter. Decomposition creates subquestions, such as finding a workshop's time and its equipment requirements. Each subquestion should contribute to the original answer.

Use a small set of complementary searches

One expansion might use a synonym, another a domain term, and another preserve an exact code. Retrieve for each, deduplicate candidates, and combine ranks or another evaluated signal. More rewrites add latency and can dilute the candidate pool with off-topic material.

TechniqueMain risk to check
Synonym or domain rewriteA near-synonym changes the requirement.
Multi-query retrievalRepeated or drifting queries add noise.
Subquestion decompositionA missing subquestion leaves the answer incomplete.
Hypothetical documentGenerated details are mistaken for evidence.

HyDE generates a hypothetical document and uses its embedding to retrieve real documents. The hypothetical text is a search aid. It must not be cited as if it were a retrieved source, and its invented details must not silently enter the final answer.

Separate searchable wording from hard constraints

Represent a request as the original text plus independently validated constraints: level is beginner, attendance is in person, and start time is later than 18:00. Let a generated query try “introductory evening workshops,” while the application keeps the constraints attached to every candidate evaluation.

If the rewrite retrieves an online beginner workshop at 19:00, did expansion fail completely?

Inspect retrieval and eligibility separately

It found a related candidate, but that candidate fails the attendance constraint and must not be recommended. Candidate retrieval can be broad if a later validated filter enforces the request. If the system drops the filter or lets the rewrite redefine it, a wording change becomes an intent change.

For practice, remove the explicit shift end time. Preserve it as unknown and decide whether to ask a clarification or return a broader set with that limitation. A hypothetical search document may help matching; it cannot supply the missing user preference as fact.

Keep the source of every text segment clear

Label original questions, generated searches, hypothetical text, retrieved passages, and final claims separately in the workflow. A convenient list called context can hide those distinctions and encourage the model to treat every item as equally trustworthy.

Apply access control to every expanded search. A rewrite should not widen a user's document scope or bypass a version filter. Query expansion changes matching, not authorization.

Evaluate on failures the baseline actually has

Build cases with synonyms, jargon, acronyms, exact identifiers, negation, and ambiguous requests. Compare the original-query baseline with a small expansion set using the same corpus and retrieval budget where possible.

Measure candidate recall, irrelevant-result rate, final-answer support, and additional latency. Inspect which rewrite produced each useful or harmful candidate. If improvements come only from issuing many more searches, report that cost rather than attributing everything to better wording.

Exercise: the query is “beginner pottery, not online, after 18:00.” A generated rewrite says “introductory virtual pottery courses.” Which constraints were lost?

Compare your reasoning

“not online” was reversed, and the time constraint disappeared. Reject or correct that rewrite. Preserve the original query as an anchor and validate important structured constraints separately from generated language.

Next, decide how to fit the retrieved evidence into the answer context without losing the details that make it correct.

Sources

Precise Zero-Shot Dense Retrieval without Relevance Labels introduces HyDE. Retrieval-Augmented Generation explains the wider separation between retrieved information and generated answers.

Continue: Context Compression and Long-Context RAG.

Practice for this lesson

Expand the wording without loosening the constraints

Search several phrasings while keeping hard filters intact.

About 12 min70 points3 checks and one written task
Loading your lesson progress...