Query Expansion and Rewriting
In this lesson, you will learn how to improve the accuracy of vector retrieval by programmatically transforming ambiguous user queries into more precise representations before they ever hit your index.
The Problem: Semantic Mismatch
When a user types a query into a vector search system, the resulting embedding is only as good as the input. If the user uses niche jargon, shorthand, or vague terminology, the vector embedding might miss the intent of the data stored in your database. This is the "Semantic Mismatch" problem.
The Solution: Query Expansion and Rewriting
To bridge this gap, we perform a pre-lookup step where we refine or "rewrite" the user’s prompt. This ensures that the vector we search with is mathematically closer to the intended ground-truth embeddings in our database.
There are two primary methods:
- Query Expansion: Adding synonyms or related terms to the original query to broaden the search space.
- Query Rewriting: Using an LLM or a rewriting model to generate a more detailed, "de-noised" version of the prompt that better aligns with the domain-specific language of your documents.
Practical Implementation
Before querying your vector database, send the user's prompt through a light model or a template-based refiner. Consider this workflow:
# 1. User Input
user_prompt = "Why is my DB slow?"
# 2. Rewrite/Expand the query
# Using an LLM to inject domain-specific context
refined_query = llm.generate(
f"Rewrite this for a technical vector search: {user_prompt}. "
"Include keywords like 'latency', 'index contention', and 'disk I/O'."
)
# 3. Now search the vector store with the optimized query
embedding = embedding_model.encode(refined_query)
results = vector_db.search(embedding)
By rewriting "Why is my DB slow?" into "Investigating high latency, index contention, and disk I/O bottlenecks in SQL database," you transform a generic input into a high-signal search vector. This minimizes the risk of retrieving irrelevant data and significantly boosts the quality of your retrieval-augmented generation (RAG) pipeline.
Key Takeaway: Query expansion and rewriting transform underspecified user queries into semantically dense prompts, ensuring your vector lookup targets the most relevant data points by bridging the language gap between the user and the index.