liveBook · Manning

Query expansion

As explained in Retrieval Augmented Generation, The Foundational Ideas

Query expansion generates alternative query terms or formulations to improve the recall of relevant information.

A single search query can miss documents that answer a complex question: its wording captures only part of the user’s intent. Query expansion tackles this coverage problem by generating several formulations and retrieving results for each. It broadens the search rather than simply asking for more results from one query. section 8.0

In a RAG pipeline, an LLM can generate query variants before retrieval. Each variant produces a ranked document list; reciprocal rank fusion (RRF) combines those lists into one ranking. The top documents then become context for answer generation. Cross-encoder reranking can further refine the candidates. Query expansion is one query-time enhancement, distinct from HyDE’s hypothetical-document approach. section 8.4 section 8.0

Finding the right student laptop

A request about laptops for students can become searches for durability, low weight, budget and battery life. Each retrieves a different ranked list. Fusion can promote documents found across several formulations while retaining candidates a single wording might miss. section 8.4

Listing 8.3 Toy implementation of the pipeline.
def generate_queries(original_query: str) -> List[str]:  #A
    print(f"Generating query variants for: '{original_query}'")
    return [  #B
        "durable laptops for students",
        "lightweight laptops for carrying to class",
        "best budget laptops for college",
        "laptops with long battery life for studying"
    ]
 
def retrieve_documents(query: str) -> List[str]:  #C
    print(f"Retrieving documents for: '{query}'")
    sim_results = {  #D
        "durable laptops for students": ["doc_A1", "doc_D1", "doc_D2"],
        "lightweight laptops for carrying to class":
            ["doc_L1", "doc_A1", "doc_L2"],
        "best budget laptops for college": ["doc_A1", "doc_B1", "doc_L1"],
        "laptops with long battery life for studying":
            ["doc_E1", "doc_L1", "doc_E2"]
    }
    return sim_results.get(query, [])  #D
 
user_query = "best laptop for college"  #E
k_value = 60
 
query_variants = generate_queries(user_query)  #F
print(f"\nGenerated variants:\n{query_variants}")
 
all_ranked_lists = []  #G
for query in query_variants:  #H
    all_ranked_lists.append(retrieve_documents(query))
print(f"\nRetrieved lists (Input to RRF):\n{all_ranked_lists}")
 
fused_document_ranking = reciprocal_rank_fusion(  #I
    all_ranked_lists, k=k_value)
print(f"\nFused ranking (RRF output):\n{fused_document_ranking}")
 
top_doc = fused_document_ranking[0][0]  #J
print(f"The top-ranked fused doc is '{top_doc}',")
print("demonstrating consistency over confidence.")
 

The Python listing shows the query variants and their separate document lists. Its query-generation and retrieval functions are placeholders, making the data flow clear without claiming to implement production retrieval. section 8.4

Use expansion when missing relevant documents is the problem, not as an automatic replacement for simpler retrieval. Multiple queries require multiple retrieval runs, and their results still need fusion or reranking. Evaluate which pipeline component fails before adding infrastructure. section 8.0 section 1.5.2

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Traces query variants through parallel retrieval and rank fusion, then illustrates the flow with placeholder Python functions.
Examples
Uses student-laptop searches for durability, weight, budget and battery life, plus simulated ranked document lists.
What it assumes
Assumes basic RAG and Python familiarity while spelling out how query variants become retrieved lists and a fused ranking.
What you can do after
Build it from scratch as a toy Python multi-query retrieval pipeline, or apply it with LlamaIndex’s fusion retriever.
How it connects
Connects query generation to parallel retrieval, reciprocal rank fusion, cross-encoder reranking and HyDE.

Also explained in

Compare how 4 books explain query expansion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.