Query Transformation, Decomposition & Multi-Query Expansion
Raw conversational queries diverge linguistically from formal technical documentation, producing vocabulary mismatches that silently collapse retrieval recall. This article implements query rewriting, Hypothetical Document Embeddings (HyDE), step-back abstraction, and parallel multi-query decomposition for the Workspace knowledge router.
Full-Stack AI Architecture: From Streaming Foundations to Resilient Agentic Systems in Node.js & React
Query Transformation, Decomposition & Multi-Query Expansion
A user asking "how do I stop my server from running out of memory when streaming" does not use the phrase "V8 heap exhaustion" or "TransformStream highWaterMark" — the technical terms present in the documentation. Dense vector search finds semantic neighbours of the query vector, but the vocabulary gap between casual user language and precise technical writing reduces recall. Exact phrase lookups (Part 10's BM25) do not help when none of the words match.
This article implements four query transformation strategies for the Workspace knowledge router: query rewriting to align vocabulary, HyDE to embed a hypothetical answer rather than the question, step-back abstraction to retrieve foundational context, and parallel multi-query decomposition for compound questions — all orchestrated before the hybrid RRF retrieval pipeline of Part 10.
Who this is for: Engineers whose RAG system has high recall for technical queries from documentation-fluent users but fails on natural language questions from end users. Query transformation is the gap-bridger.
1. Query Rewriting
1.1 The Vocabulary Gap
Query rewriting prompts the LLM to rephrase the user's query in the technical vocabulary of the knowledge domain, before retrieval.
2. Hypothetical Document Embeddings (HyDE)
2.1 Why Embedding the Answer Beats Embedding the Question
Query vectors and document vectors occupy different regions of the embedding space — even when semantically related. A question vector points toward "uncertainty" and "asking", while document vectors point toward "definition" and "stating facts".
HyDE bridges this gap by generating a hypothetical answer to the question and embedding that instead.
HyDE augments only the dense retrieval leg. Pass the hypothetical passage embedding as the
vector input, but still pass the original user query as queryText to the sparse BM25 leg
of hybridSearch(). HyDE and BM25 are complementary: HyDE improves semantic recall while BM25
preserves exact-match coverage. Using HyDE output for BM25 would break exact-match retrieval.
3. Step-Back Abstraction
3.1 Retrieving Foundational Context First
For questions about specific implementations, retrieving the foundational concept often provides the necessary context for a complete answer.
4. Multi-Query Decomposition
4.1 Splitting Compound Questions
Compound user questions span multiple document topics. A single query vector is a centroid of all topics — precisely the wrong location for any of them individually.
5. The Query Router: Choosing the Right Strategy
Summary
| Strategy | Solves | Token Cost |
|---|---|---|
| Query Rewrite | Vocabulary gap between user language and documentation | ~100 tokens (Haiku) |
| HyDE | Query vector far from document vectors in embedding space | ~300 tokens (Haiku) |
| Step-Back | Specific implementation question needs foundational context | ~100 tokens (Haiku) |
| Multi-Query | Compound questions spanning multiple document topics | ~200 + N × retrieval |
What's Next
Part 14 assembles the complete context for the LLM: the Context Window Scheduler that enforces token budgets across system instructions, tool schemas, RAG evidence, and conversation history — preventing silent overflow and the "Lost-in-the-Middle" attention failure.
This article was developed with AI-assisted deep search, specification cross-referencing, and technical research synthesis.