Day 5:Week 11, Day 5: Query Transformation — Coreference Rewriting, Multi-Query Expansion & `HyDE`
Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz
Week 11, Day 5: Query Transformation — Coreference Rewriting, Multi-Query Expansion & HyDE
1. Why Raw User Queries Are Terrible Retrieval Queries
Users in a chat interface rarely type self-contained search queries:
- Turn 1: "Payments API is throwing HTTP 502 errors."
- Turn 2: "How do I roll it back?"
If you send the raw Turn 2 string "How do I roll it back?" to BM25 or a Vector Database, it has zero mention of "Payments API" or "HTTP 502", so it retrieves generic rollback chunks for the wrong services!
2. The Three Production Query Transformation Patterns
1. Conversational Coreference Rewriting (De-contextualization):
Chat History + "How do I roll it back?" ──► "How do I roll back the Payments API during an HTTP 502 error?"
2. Multi-Query Expansion + RRF Fusion:
Complex Query ──► Generate 3 Sub-Queries (Lexical, Semantic, Troubleshooting)
──► Run Hybrid Search on all 3 ──► Fuse with RRF (k=60)!
3. HyDE (Hypothetical Document Embeddings — Gao et al., 2022):
Short Question ("Fix Postgres replication lag")
│
▼ LLM drafts a hypothetical 2-sentence runbook excerpt
"To resolve PostgreSQL standby replication lag, check pg_stat_replication and WAL sender throughput."
│
▼ Embed the hypothetical document to search Document-to-Document space!
[!WARNING] When NOT to use
HyDE: Never feed aHyDEhypothetical document intoBM25if the LLM might invent fake error codes or package names! Only useHyDEfor Dense Vector similarity when user queries are extremely short (2–4words) and suffer from a "Query-vs-Document style gap".
In Simple English: Fixing Messy User Questions Before Searching (HyDE & Query Rewriting)
Real users rarely ask clean textbook questions! They type follow-ups like: "How do I fix it on staging?"
- Coreference Rewriting: Uses conversation history to rewrite "it" "PostgreSQL connection timeout on staging".
- Multi-Query Expansion: Generates 3 phrasing variants ("DB pool limit", "PostgreSQL max_connections", "staging connection refused") to boost search recall.
HyDE(Hypothetical Document Embeddings): Ask a small LLM to draft a fake 3-sentence textbook answer, embed THAT fake answer, and search the vector DB! Because answers sound way more like stored documents than short questions do, matching accuracy jumps cleanly!