A single search query can miss an answer that is already in the corpus. A support bot asked “Why is my internet slow?” might overlook a document about packet loss on fiber connections: the user’s wording and the document’s terminology point to different parts of the search space. Multi-query retrieval addresses this coverage problem by searching with several formulations of the same information need. section 8.1
A multi-query retrieval pipeline separates preparation from query-time work:
- Prepare the corpus for retrieval—for example, split documents and store their embeddings in a vector database. section 8.2.1
- At query time, generate variants that target different perspectives or sub-intents, not merely different wording. Retrieve a result list for each variant. section 8.7
- Merge the result lists. A simple union removes duplicates but leaves ranking unresolved; Reciprocal Rank Fusion (RRF) uses ranks rather than scores to promote documents supported across lists. section 8.2.1 section 8.7
Product search beyond literal matches
For “warm winter jacket,” a single query returns jackets whose descriptions closely match those words. Variants such as “best down parkas for extreme cold” and “waterproof ski jackets for winter” also surface parkas and ski jackets. The fused results offer more kinds of products that could meet the shopper’s intent, rather than only more matches to the original phrasing. section 8.5
Listing 8.1 Generating query variants with LangChain.
from langchain_classic.retrievers.multi_query import (
MultiQueryRetriever)
from providers import get_chat_model, get_embeddings
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
loader = WebBaseLoader( #A
"https://lilianweng.github.io/posts/2023-06-23-agent/"
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter( #B
chunk_size=500, chunk_overlap=0)
splits = text_splitter.split_documents(docs) #C
embedding_function = get_embeddings()
vector_db = Chroma.from_documents(
documents=splits,
embedding=embedding_function
) #D
llm = get_chat_model(temperature=0) #E
retriever_from_llm = MultiQueryRetriever.from_llm( #F
retriever=vector_db.as_retriever(),
llm=llm,
include_original=True #G
)
user_query = "What are the different methods for Task Decomposition?"
retrieved_docs = retriever_from_llm.invoke(user_query) #H
print(f"\nRetrieved {len(retrieved_docs)} unique documents.")
The Python listing shows how LangChain’s MultiQueryRetriever wraps a vector-store retriever and an LLM to generate and execute query variants; its default union does not intelligently rerank results. section 8.2.1
Use multi-query retrieval when a discovery question may be phrased differently from the corpus, as in technical support or product search. More variants are not automatically better: each adds latency, cost, and an opportunity for noise. Three to five queries is a suggested starting point, followed by evaluation of coverage and result quality. RRF can combine their lists; cross-encoder reranking is another possible later stage. section 8.7 section 8.0
