Back
advanced

Advanced retrieval and context

Improving search with query expansion

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

Lesson 39 of 67About 25 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.

Which constraints must survive a paraphrase?

Expansion can help retrieve wording that differs from the user's query. It can also broaden the request accidentally. Dates, class identity, location and negation may be essential filters, so preserve them while varying the phrasing around them.

Compare each generated query with a short list of original constraints before issuing it. After retrieval, check that combined results still answer the original question. The challenge asks you to improve recall without changing the user's task into a more convenient but different one.

Example

The user asks about refunds for Tuesday evening classes in September.

What changes

An expansion searches for "class refunds" and drops the day and month.

Result

More results may appear, but they can answer the wrong policy question.

Expansion should vary expression while preserving meaning-bearing constraints.

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.

Practice with feedback

Lesson challenge

Expand the wording without loosening the constraints

Query: "refund for the Tuesday evening class in September". Expansion produces variants that quietly drop "Tuesday" and "September".

Search several phrasings while keeping hard filters intact.

Check your understanding

Question 1 of 3
What must never be expanded away?
Score: 0/0

Your task

Split the query into constraints and searchable text, then evaluate on baseline failures.

These notes stay on this page. Download them before leaving. Code in this field is not executed.

What to include

  • Constraints are extracted before expansion and never paraphrased
  • All variants search under identical filters
  • Results are deduplicated and their source variant is tracked
  • Evaluation targets the failure set and reports added latency
Compare with a worked answer

Here is one way to answer. Check how it uses the information in the task.

import re

def split_query(q):
    constraints = {}
    m = re.search(r'\b(Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day\b', q, re.I)
    if m: constraints['day'] = m.group(0).title()
    m = re.search(r'\b(January|February|...|September|...|December)\b', q, re.I)
    if m: constraints['month'] = m.group(0).title()
    for code in re.findall(r'\b[A-Z]{2}-\d{3,}\b', q):
        constraints['code'] = code
    return constraints, q          # searchable text keeps the full wording

constraints, text = split_query(query)
variants = [text] + expand(text, n=3)
#   'refund for the Tuesday evening class in September'
#   'how do I get my money back for an evening session'
#   'cancellation and reimbursement policy for classes'
#   'requesting a refund after attending'
# Note the variants may lose 'Tuesday' and 'September' - that is fine now,
# because those live in constraints and are applied as filters to every search.

hits = {}
for v in variants:
    for h in search(v, filters=constraints, k=20):
        hits.setdefault(h.id, {'h': h, 'from': []})['from'].append(v)
fused = rrf([[h.id for h in search(v, filters=constraints, k=20)] for v in variants])

# Evaluation on the 14 questions the baseline failed, not all 60:
#   baseline recall@5 on those 14: 0.00 by construction
#   with expansion:                0.57 (8 of 14 recovered)
#   added latency: +310ms p50, because four searches replace one
#   effect on the 46 the baseline already passed: 45 still pass, 1 regressed
# Averaged over all 60 that is +13 points, which looks unimpressive and hides
# both the real gain and the one regression. Reporting the two groups
# separately is what made the regression visible at all.

When you are signed in, opening the challenge carries your edited working notes into its draft in this browser. The challenge has its own completion record. Practising here does not award points or mark it complete.

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.

Practise this lesson

Expand the wording without loosening the constraints

Search several phrasings while keeping hard filters intact.

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