Inferensys

Glossary

doc2query

A document expansion technique that uses a sequence-to-sequence model to generate potential queries a document might answer, which are then appended to the document's index to bridge the lexical gap.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
Document Expansion

What is doc2query?

doc2query is a neural document expansion technique that uses a sequence-to-sequence model to predict queries for which a given document is relevant, appending the generated text to the document before indexing to bridge the lexical gap between user queries and document vocabulary.

doc2query is a document expansion technique that generates synthetic queries a document might answer and appends them to the document's indexed text. This process enriches the document's representation with the diverse vocabulary users actually employ when searching, significantly improving recall for sparse retrieval systems like BM25 without modifying the query processing pipeline.

The underlying model is typically a sequence-to-sequence transformer, often based on T5, trained on pairs of document passages and their associated queries from search logs. At indexing time, the model performs beam search decoding to produce multiple predicted queries, which are weighted and concatenated to the original text. This shifts computational cost to indexing, making it a highly efficient method for improving lexical matching in production search systems.

DOCUMENT EXPANSION

Key Characteristics of doc2query

doc2query is a sequence-to-sequence document expansion technique that generates synthetic queries a document is likely to answer, appending them to the index to bridge the vocabulary gap between user queries and document terms.

01

Sequence-to-Sequence Foundation

At its core, doc2query employs a sequence-to-sequence (seq2seq) model, typically based on the Transformer architecture. The model is trained on pairs of documents and their associated queries to learn the mapping from document text to potential search queries. During inference, the model takes a document's text as input and autoregressively generates multiple synthetic queries that the document could answer. This is fundamentally different from query expansion because it expands the document side of the index rather than modifying the user's query at search time.

02

Bridging the Vocabulary Gap

The primary value of doc2query is resolving lexical mismatch between how users search and how authors write. Key mechanisms include:

  • Synonym generation: A document about 'automobiles' might generate the query 'cars'
  • Paraphrase normalization: Technical jargon in the document gets expanded into layperson query terms
  • Question generation: Declarative sentences are transformed into interrogative forms users actually type
  • Entity variation: Formal entity names generate colloquial references and abbreviations

This effectively increases recall without requiring manual synonym dictionaries or query-time expansion.

03

Training with Doc-Query Pairs

The seq2seq model is trained using supervised learning on large corpora of document-query pairs. Common training data sources include:

  • MS MARCO dataset: Real Bing search queries paired with relevant passages
  • Natural Questions: Google queries paired with Wikipedia articles
  • Proprietary search logs: Internal click-through data from enterprise search systems

The model learns to maximize the conditional probability of generating the actual query given the document text. Once trained, the model is applied to the target corpus at indexing time, generating queries that are appended to each document's indexed text fields.

04

Integration with Sparse and Dense Retrieval

doc2query was originally designed for sparse retrieval systems like BM25, where the generated queries add new tokens to the inverted index. However, the technique has evolved:

  • doc2query-T5: Uses the T5 text-to-text transformer for higher quality query generation
  • docT5query: A specific implementation that fine-tunes T5 on MS MARCO, appending predicted queries to document text before BM25 indexing
  • Dense integration: Generated queries can also be encoded into dense vectors for hybrid retrieval, enriching the document's semantic representation in the embedding space
  • Multi-stage pipelines: Generated queries improve first-stage recall, while cross-encoder re-rankers handle precision
05

Computational Trade-offs

Document expansion with doc2query introduces specific computational considerations:

  • Indexing-time cost: Query generation runs once per document during indexing, not at query time, making it an offline cost
  • Index size inflation: Appending generated queries can increase the inverted index size by 2-5x, depending on the number of queries generated per document
  • Generation quality vs. speed: Larger models like T5 produce better queries but slower generation; distilled models offer practical trade-offs
  • Noise management: Not all generated queries are equally useful; techniques like confidence filtering or limiting the number of appended queries help control noise

Despite these costs, the technique is widely adopted because it shifts computation to indexing time, preserving low-latency query serving.

06

Relationship to Query Expansion

doc2query is the dual operation of traditional query expansion. While query expansion modifies the user's search terms at runtime, doc2query modifies the document representation at indexing time. This distinction matters:

  • Query expansion adds latency to every search request
  • doc2query front-loads computation to indexing, keeping search fast
  • Query expansion must generalize across all possible documents
  • doc2query can be document-specific, generating tailored queries per document

In practice, the two techniques are complementary and often combined in production search systems for maximum recall improvement.

DOCUMENT-CENTRIC VS. QUERY-CENTRIC EXPANSION

doc2query vs. Query Expansion Techniques

A comparative analysis of doc2query, a document-side expansion technique, against traditional query-side expansion methods like synonym expansion and pseudo-relevance feedback.

Featuredoc2querySynonym ExpansionPseudo-Relevance Feedback

Expansion Target

Document index (offline)

Search query (online)

Search query (online)

Processing Time

Performed at indexing time

Performed at query time

Performed at query time

Latency Impact

Zero query-time latency

Adds < 5ms per term

Adds 50-200ms for re-retrieval

Vocabulary Source

Generated by seq2seq model

Pre-built synonym dictionary

Top-k retrieved documents

Handles Multi-Word Concepts

Requires User Query Context

Expansion Drift Risk

Low (grounded in document)

Medium (static mappings)

High (topic drift from noisy docs)

Index Size Overhead

25-50% increase

0%

0%

DOC2QUERY EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the doc2query document expansion technique, its implementation, and its impact on modern semantic search pipelines.

doc2query is a document expansion technique that uses a sequence-to-sequence (seq2seq) neural model to generate potential search queries that a given document is likely to answer, and then appends those synthetic queries to the document's text before indexing. The core mechanism involves training a transformer-based model, typically a T5 variant, on pairs of document passages and their corresponding search queries. Once trained, the model ingests a document and autoregressively generates 5-20 plausible questions or keyword queries. For example, a document about 'semantic search' might generate queries like "What is semantic search?", "dense retrieval vs sparse retrieval", and "how do vector embeddings work?". These generated queries are then appended to the document's indexed text, effectively bridging the vocabulary gap between the terse, ambiguous language of user queries and the formal, descriptive language of documents. This significantly improves recall in both sparse retrieval systems like BM25 and dense retrieval systems by providing additional lexical and semantic anchors.

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.