Use controlled synonyms and subqueries while retaining exact IDs, exclusions, dates and authorization scope.
Query expansion: add recall terms without changing the user's question
Split immutable constraints from optional terms
Parse exact identifiers, quoted phrases, exclusions, tenant scope and time bounds before expansion. Keep those constraints attached to every generated subquery. A query for case AX-47 after September must not turn into a general query about delayed cases. Expansion is useful for vocabulary mismatch: ‘chargeback’ may appear as ‘payment dispute’ in a permitted document. It is not permission to relax an exact case ID. Routing should decide whether expansion is needed at all.
Bound candidates and weights
Start with a small, reviewed synonym list or a validated reformulator. Search the original query as one leg and each expansion as a separate leg, then merge by passage ID. Preserve which leg found each candidate. Weight the original query and exact-match leg so a broad synonym cannot bury the literal target. Rank fusion is a useful merge baseline because score scales from different legs are not directly comparable.
Reject drift
A proposed rewrite fails when it drops an identifier, changes a number, reverses a negation, broadens a tenant, or shifts a date interval. Compare parsed constraints before executing the rewrite. Some vocabulary changes are context dependent: ‘hold’ can mean a payment hold or warehouse hold. For ambiguous terms, retrieve with both senses only inside the authorized corpus and let evidence disambiguate; otherwise ask the user.
Measure value by query family
Expansion should improve recall for jargon and paraphrase queries without harming exact-ID precision or latency. Build paired judgments with original-only and expanded runs. Report newly found answer passages, false positives, average additional calls and p95 latency. Retain a no-answer set; a system that answers more questions by matching unrelated synonyms has not improved. Traces should expose which expansion introduced each candidate.
Worked contract
import re
def safe_expansions(question: str, synonyms: dict[str, tuple[str, ...]]) -> list[str]:
required_ids = set(re.findall(r"\bAX-\d+\b", question.upper()))
variants = [question]
for term, replacements in synonyms.items():
if not re.search(rf"\b{re.escape(term)}\b", question, re.IGNORECASE):
continue
for replacement in replacements[:2]:
candidate = re.sub(rf"\b{re.escape(term)}\b", replacement,
question, count=1, flags=re.IGNORECASE)
if required_ids <= set(re.findall(r"\bAX-\d+\b", candidate.upper())):
variants.append(candidate)
return variants[:4]
variants = safe_expansions("AX-47 chargeback status",
{"chargeback": ("payment dispute", "card dispute")})
assert variants == ["AX-47 chargeback status",
"AX-47 payment dispute status", "AX-47 card dispute status"]
assert all("AX-47" in variant for variant in variants)Performance and operating cost
For Q query characters and at most S reviewed replacements, this small implementation scans O(SQ) time and stores at most four variants. Search cost can grow roughly with the number of variants, so cap expansion count and parallel calls. This demonstration protects exact IDs only; production validation must also compare negations, dates, tenant scope and other parsed constraints before dispatch.
Common Mistakes
- Replacing an exact identifier with a likely-looking related identifier.
- Dropping ‘not’ or a date bound while rewriting.
- Merging scores from different search legs as if their scales were equal.
- Celebrating recall gains without measuring no-answer false positives.
Validation drill
Judge 61 requests across exact IDs, jargon synonyms, negations, date bounds and no-answer cases. Compare original-only search with a maximum of three expanded variants; reject any rewrite that changes parsed constraints.
Read next
- Retrieval query routing: preserve exact constraints before search
- Hybrid retrieval: fuse exact terms and semantic candidates by rank
- Retrieval traces: locate the stage that lost the answer
- Retrieval release gates: evaluate slices before changing an index
