Inferensys

Glossary

Query Rewriting

Query rewriting is the process of transforming a raw user query into one or more alternative formulations—through expansion, refinement, or decomposition—to improve the likelihood of matching relevant documents in a retrieval index.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
QUERY UNDERSTANDING

What is Query Rewriting?

Query rewriting is a retrieval augmentation technique that transforms a raw user query into one or more alternative formulations to bridge the lexical and semantic gap between the user's input and the indexed documents, improving match likelihood.

Query rewriting is the process of automatically reformulating an original user query into alternative queries—through expansion, refinement, or decomposition—to improve retrieval recall and precision. Unlike simple synonym expansion, modern rewriting uses large language models to generate contextually rich paraphrases, extract key entities, or decompose complex questions into sub-queries that align more effectively with the underlying inverted index or dense vector embeddings.

This technique is a critical component of hybrid retrieval strategies, often serving as a pre-retrieval step before candidate generation. Effective rewriting mitigates vocabulary mismatch by generating queries that anticipate the terminology used in relevant documents, while also enabling multi-hop reasoning by breaking a compound question into answerable atomic units for parallel or sequential retrieval pipelines.

QUERY UNDERSTANDING

Core Query Rewriting Techniques

Query rewriting transforms raw user input into optimized formulations that bridge the gap between vague natural language and precise retrieval indexes. These techniques improve recall, handle vocabulary mismatches, and decompose complex questions into answerable sub-queries.

01

Query Expansion

Augments the original query with additional terms to improve recall. This can be achieved through synonym injection using a thesaurus or word embeddings, or by adding hypernyms and hyponyms to broaden or narrow the search scope. For example, a query for 'laptop' might be expanded to include 'notebook computer' and 'ultrabook'.

  • Classic approach: Pseudo-Relevance Feedback (PRF) extracts terms from top-k initial results
  • Modern approach: Generative models like LLMs produce expanded query variants
  • Risk: Over-expansion can introduce noise and reduce precision
02

Query Decomposition

Breaks a complex, multi-faceted question into a sequence of simpler sub-queries that can be answered independently. This is essential for multi-hop reasoning where the answer to one sub-question serves as context for the next.

  • Sequential decomposition: 'What is the capital of the country where the Amazon rainforest is located?' becomes 'Where is the Amazon rainforest?' → 'Brazil' → 'What is the capital of Brazil?'
  • Parallel decomposition: Independent facets are retrieved simultaneously and answers are aggregated
  • Tool use: LLMs with chain-of-thought prompting generate structured sub-query plans
03

HyDE (Hypothetical Document Embeddings)

A technique where a language model generates a hypothetical ideal document that would answer the query, then uses the embedding of that synthetic document for retrieval rather than the query embedding itself. This bridges the vocabulary gap between short queries and verbose documents.

  • A query like 'how to fix a leaky faucet' might generate a hypothetical DIY guide paragraph
  • The dense embedding of that generated text is used for ANN search
  • Particularly effective when queries and documents have different linguistic styles or lengths
04

Step-Back Prompting

Generates a higher-level, more abstract 'step-back' question that captures the general concept before addressing the specific query. This improves retrieval by first grounding the system in broad, relevant context.

  • Example: For 'What happens to the pressure of an ideal gas if volume doubles at constant temperature?', the step-back question is 'What is the ideal gas law?'
  • Retrieves foundational knowledge first, then applies it to the specific case
  • Reduces hallucination by ensuring the model has access to core principles before reasoning
05

Query2Doc

Uses a language model to generate a pseudo-document from the query, then concatenates that generated text with the original query to form an expanded search string. Unlike HyDE which uses only the synthetic embedding, Query2Doc enriches the query with additional lexical terms for sparse retrieval.

  • The pseudo-document adds contextually relevant keywords
  • Benefits both dense and sparse retrieval pipelines
  • Can be combined with BM25 to inject learned vocabulary into keyword search
06

Multi-Query Retrieval

Generates multiple semantically diverse reformulations of the same user query, retrieves documents for each variant independently, and then applies Reciprocal Rank Fusion (RRF) to merge the results into a single ranked list.

  • Each reformulation captures a different perspective or phrasing of the same intent
  • Improves recall by covering multiple ways the answer might be expressed in the corpus
  • The fusion step automatically up-weights documents that appear consistently across multiple retrieval passes
QUERY REWRITING

Frequently Asked Questions

Query rewriting is the process of transforming a raw user query into one or more alternative formulations to improve the likelihood of matching relevant documents in a retrieval index. The following questions address the core mechanisms, strategies, and trade-offs involved in building robust rewriting pipelines for hybrid retrieval systems.

Query rewriting is the computational process of automatically transforming a user's raw, often ambiguous, search query into one or more refined, expanded, or decomposed formulations before it reaches the retrieval index. The mechanism works by analyzing the original query for semantic intent, extracting key entities, and then applying a series of transformations—such as synonym expansion, morphological normalization, or decomposition into sub-queries—to bridge the vocabulary gap between the user's language and the indexed document language. In a modern Retrieval-Augmented Generation (RAG) pipeline, a language model or a specialized rewriter module generates these alternative queries, which are then executed against both sparse (BM25) and dense (DPR) indexes. The results from these multiple query formulations are fused using algorithms like Reciprocal Rank Fusion (RRF) to maximize recall. This process is distinct from simple query expansion because it can involve structural changes, such as converting a complex multi-hop question into a series of simpler, sequential queries that can be resolved independently.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.