liveBook · Manning

Multi-query retrieval

As explained in Retrieval Augmented Generation, The Foundational Ideas

Multi-query retrieval generates or uses multiple formulations of an information need to retrieve complementary result sets.

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

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Explains query generation, per-variant retrieval, and result merging, with a LangChain implementation and a warning about its default union.
Examples
Contrasts slow-internet troubleshooting with packet-loss documentation and compares literal jacket matches with parkas and ski jackets.
What it assumes
Assumes familiarity with RAG retrieval, embeddings, and vector databases; supplies a concrete LangChain setup for query generation.
What you can do after
Apply it via LangChain’s MultiQueryRetriever, then evaluate whether list fusion improves coverage enough to justify added latency and cost.
How it connects
Connects query expansion to RAG, vector search, Reciprocal Rank Fusion, cross-encoder reranking, and hypothetical-document 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.