Module 3 · Index types
Summary Index and Document Summary Index
4 min read
Vector search excels at a precise question whose answer fits in a few passages. It fails on global questions: "Summarize our policies", "Which documents talk about furniture?", "What changed in the warranty?". No single passage answers them; you need an overview. Two LlamaIndex indexes meet this need: the SummaryIndex and the DocumentSummaryIndex.
Why vector search fails on global questions
Asking a vector index to "summarize the returns policy" amounts to looking for the four passages closest to that sentence. The result depends on incidental words, and the synthesis only covers those four excerpts — perhaps leaving out the most important section. The model will answer confidently… from an incomplete basis.
SummaryIndex: read everything
The SummaryIndex (formerly ListIndex) is the simplest structure: a list of nodes. At query time, it hands all of them to the synthesizer.
from llama_index.core import SummaryIndex
index = SummaryIndex.from_documents([returns_policy])
response = await index.as_query_engine(response_mode="tree_summarize").aquery("Résume cette politique")
The tree_summarize synthesis mode is made for this: it summarizes the nodes in groups that fit in the model's context, then summarizes the summaries, until a single answer. Coverage is complete, but cost is proportional to the document's size. Reserve it for reasonably sized documents, or for cases where completeness prevails.
DocumentSummaryIndex: one summary per document
The DocumentSummaryIndex computes, at ingestion, a summary of each document with the LLM. At query time, it first looks for documents whose summary matches the question, then works on their nodes. Summaries are embedded: you search among a few dozen summaries rather than thousands of passages.
Lumen uses it for the policies:
def summary_index(documents: Sequence[Document]) -> DocumentSummaryIndex:
return DocumentSummaryIndex.from_documents(
documents,
llm=models.fast_llm(),
embed_model=models.embed_model(),
transformations=[
SentenceSplitter(chunk_size=config.CHUNK_SIZE, chunk_overlap=config.CHUNK_OVERLAP)
],
summary_query="Résume ce document en trois phrases "
"et liste les questions auxquelles il répond.",
)
Three choices to note:
- The fast model produces the summaries: it's a simple task, repeated for each document.
- The summary prompt (
summary_query) also asks for "the questions it answers": the summary is used to retrieve the document, so it must contain the vocabulary of users' questions, like the extractor of lesson 2.3. - The summary can be inspected:
index.get_document_summary(doc_id)returns a document's summary. Reread a few; a bad summary will make the document impossible to find.
Querying the summary index
The DocumentSummaryIndex offers two ways to select documents:
| Retriever | Selection | Trade-off |
|---|---|---|
| By embeddings (default when summaries are embedded) | Summaries closest to the question | Fast, cheap |
By LLM (DocumentSummaryIndexLLMRetriever) | The model reads the summaries and picks the relevant documents | More accurate on subtle questions, one LLM call per query |
In Lumen, the summary index becomes an engine dedicated to overviews, with tree_summarize:
QueryEngineTool.from_defaults(
summaries.as_query_engine(llm=models.main_llm(), response_mode="tree_summarize"),
name="policy_summaries",
description="Résumé ou vue d'ensemble d'une ou plusieurs politiques "
"(retours, garantie, livraison) ; comparaison entre politiques.",
)
The router (lesson 3.4) sends global questions to it, and keeps the vector index for precise questions.
The cost of summaries
One summary per document means one LLM call per document at ingestion — and at every modification of the document. On a corpus of ten thousand documents, the initial cost is real, but it's only paid once per version; it's rarely the main cost item compared to daily queries. Still, keep two reflexes: the fast model to summarize, and building the summary index only on collections where global questions make sense (policies, not tickets).
Summarize at ingestion or at query time?
| Approach | Advantages | Drawbacks |
|---|---|---|
SummaryIndex + tree_summarize (at query time) | Always up to date, no preparation | Costly and slow for every question on a large document |
DocumentSummaryIndex (at ingestion) | Fast queries, document selection possible | Summaries to recompute at each modification |
SummaryExtractor on nodes | Local context for each passage | Doesn't help global questions |
Mature systems often combine a vector index for detail and a summary index for the overview — exactly Lumen's architecture.
Key takeaways
- Vector search retrieves a few passages: it fails on global questions, without saying so.
SummaryIndexreads everything, withtree_summarize: complete but costly for every question.DocumentSummaryIndexsummarizes each document at ingestion and selects documents by their summary.- A good summary contains the vocabulary of the questions; reread a few.
- Combine: vector for detail, summaries for the overview, and a router between the two.
Practice
- Ask "Résume la politique de retours" to the vector engine, then to the summary engine: compare the coverage of the two answers.
- Display the three policy summaries with
get_document_summaryand improve thesummary_queryif needed. - Replace the embedding retriever with
DocumentSummaryIndexLLMRetrieverand compare the documents selected on five questions.