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.
Glossary
doc2query

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.
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.
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.
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.
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.
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.
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
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.
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.
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.
| Feature | doc2query | Synonym Expansion | Pseudo-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% |
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Explore the broader ecosystem of query expansion and document augmentation methods that work alongside doc2query to bridge the vocabulary gap between user intent and indexed content.
Generative Query Expansion
The use of large language models (T5, GPT) to generate relevant expansion terms, synonyms, or full alternative queries from a prompt.
- Uses seq2seq architectures similar to doc2query
- Operates on the query side rather than the document side
- Can generate context-aware paraphrases that capture user intent
- Often combined with doc2query for bidirectional vocabulary alignment
Pseudo-Relevance Feedback
An automatic query expansion technique that assumes the top-k documents from an initial retrieval are relevant and extracts expansion terms from them.
- Contrasts with doc2query's offline document expansion approach
- Operates at query time, adding latency to retrieval
- Risk of query drift if top results are not truly relevant
- doc2query avoids this by expanding documents independently of any query
Contextualized Embedding Expansion
A query expansion method that uses deep language models like BERT to generate expansion terms that are semantically appropriate for the query's specific context.
- Leverages contextualized token representations
- Unlike doc2query's generative approach, this method selects terms from pre-computed embedding spaces
- Handles polysemy by disambiguating word senses in context
- Complements doc2query by enriching the query encoding pipeline
Synonym Expansion
A query expansion technique that adds words with identical or highly similar meanings to the original query terms to increase recall.
- Relies on curated thesauri or lexical databases like WordNet
- Simpler and more deterministic than doc2query's learned approach
- Cannot generate multi-word phrases or capture document-specific terminology
- Often used as a baseline against which neural expansion methods are benchmarked
Paraphrase Generation
A query expansion technique that uses a model to generate alternative phrasings of the original query that convey the same meaning.
- Shares doc2query's sequence-to-sequence foundation
- Applied to the input query rather than the indexed document
- Can produce syntactically diverse reformulations
- When paired with doc2query, creates a dual-expansion pipeline that aligns both sides of the retrieval equation
Hybrid Search Fusion
Combines sparse retrieval (BM25) and dense retrieval (embeddings) results using techniques like reciprocal rank fusion.
- doc2query bridges these paradigms by adding lexical anchors to dense indexes
- Expanded documents improve BM25 recall while preserving dense matching
- Fusion strategies determine how expansion terms influence final ranking
- Critical infrastructure for deploying doc2query in production search systems

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us