Skip to main content
Query enhancement rewrites user queries to improve retrieval coverage. Instead of searching with a single query, you generate multiple query variations that capture different aspects of the information need. This addresses vocabulary mismatch between queries and documents.

Why query enhancement matters

A single query may miss relevant documents because:
  • Users phrase questions differently than document content
  • Important terms have synonyms or alternative phrasings
  • Specific queries miss broader context needed for complex answers
Query enhancement solves this by casting a wider retrieval net, then fusing results.

Techniques

Multi-query generation

Generates 3-5 alternative phrasings of the original query. Each variation may match different documents in your vector store.
When to use: Best for factual queries where different terminology might match different documents. Effective for domain-specific vocabulary.

HyDE (Hypothetical Document Embeddings)

Generates a hypothetical answer to the query, then searches for documents similar to that answer. This bridges the distribution gap between questions (short, interrogative) and documents (long, declarative).
Output:
When to use: Best when queries are very short or when query/document distributions differ significantly (e.g., questions vs. encyclopedia articles).

Step-back prompting

Generates broader, more abstract questions that retrieve background context before answering the specific query.
Output:
When to use: Best for complex questions requiring background knowledge. The step-back questions retrieve context that improves answer quality.

Configuration

How result fusion works

Each enhanced query retrieves top_k documents. Results are merged using Reciprocal Rank Fusion (RRF):
RRF scoring formula:
Where:
  • rank_i is the document’s rank in query result i
  • k controls the weight of lower-ranked results (default: 60)

Implementation details

Parallel execution

Haystack pipelines execute searches in parallel using ThreadPoolExecutor:

Deduplication

After fusion, duplicate documents are removed based on content similarity:

Cost considerations

  • LLM cost: 1 call to generate variations
  • Embedding cost: 3-5 query embeddings
  • Search cost: 3-5 vector searches
  • Total latency: ~2-3x single query (parallel execution)
  • LLM cost: 1 call to generate hypothetical docs
  • Embedding cost: 3 document embeddings
  • Search cost: 3 vector searches
  • Trade-off: Higher embedding cost (documents longer than queries)
  • LLM cost: 1 call to generate step-back questions
  • Embedding cost: 4 query embeddings
  • Search cost: 4 vector searches
  • Benefit: Retrieves broader context for complex questions

Best practices

Choose the right technique

  • Multi-query: General-purpose, works for most queries
  • HyDE: When queries are very short or domain-specific
  • Step-back: For complex questions needing background context

Tune fusion parameters

  • Lower rrf_k (30-40): Prioritizes top-ranked results
  • Higher rrf_k (80-100): Gives more weight to lower ranks
  • Default k=60 works well for most cases

Cache enhanced queries

For frequently-asked questions, cache the generated query variations to avoid repeated LLM calls.

Combine with reranking

Query enhancement increases recall (more relevant docs retrieved). Follow with reranking to improve precision.

See also