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



