Overview

8 Advanced indexing

Modern RAG systems often underperform not because of weak models but because of naïve indexing and retrieval design. This chapter reframes accuracy as a pipeline problem across ingestion and Q&A: vague queries, mismatched chunking, and thin metadata commonly yield irrelevant or incomplete context. The remedy is iterative design with LangChain—tuning chunking, enriching indexes, transforming questions, routing to the right stores, and post-filtering—so the retriever consistently surfaces the most useful evidence for synthesis.

The core advances center on indexing. First, choose a split strategy aligned to your questions: hierarchical splits preserve semantics for topic-centric docs, while fixed-size splits offer consistency; balance granular chunks (precision) and coarse chunks (context), and adjust overlaps as needed. Then layer multi-vector techniques to make coarse chunks discoverable by fine-grained signals: embed child chunks but return parent chunks (ParentDocumentRetriever) for rich context; add summary embeddings to densify coarse representations; generate hypothetical question embeddings so user phrasing better matches stored vectors; and expand granular chunks at retrieval time with neighboring sentences to restore missing context. Together, these methods raise recall and precision without sacrificing synthesis quality.

On the query side, transform unclear or broad questions into sharper, retrieval-ready variants, decompose them when necessary, and route to the right content store. Blend vector search with structured sources (relational or graph), generating source-specific queries (e.g., SQL) to unlock tables and facts, and filter noisy results before synthesis. Finally, extend the same principles to semi-structured and multi-modal content: summarize and embed tables or images, store the raw artifacts separately, and use a multi-vector retriever to return the full objects when their summaries hit. The result is a robust, ensemble-style RAG pipeline that consistently retrieves focused, context-rich evidence across text, structured data, and media.

Common accuracy issues in the ingestion stage of a simple RAG architecture are often due to inadequate indexing that only uses basic embeddings for each text chunk. Advanced indexing techniques involve generating multiple embeddings for each chunk, enhancing searchability.
Common Issues in the Q&A stage of the naive RAG architecture and their solutions:1) Handle poorly formulated questions with question transformation; 2) Enhance retrieval accuracy by transforming the original user questions into more suitable vector DB search queries; 3) Include relevant data sources by adding structured data content stores, such as relational DBs; 4) Generate DB queries for structured data content stores; 5) Filter out irrelevant context retrieved from the content stores.
Impact of Chunk Size on Answer Accuracy. Coarse chunks provide more context but less focus, and they are suitable for broader questions. Granular chunks provide less context but more focus and they are suitable for more detailed questions.
Child Chunk embeddings—A coarse document chunk is indexed with its own embedding and the embeddings generated from its smaller child chunks. This allows it to match effectively with both broad and detailed queries.
Chunk Summary Embedding—A coarse chunk is indexed with its own embedding and an additional embedding from its summary. This allows for more accurate retrieval when answering detailed questions.
Hypothetical Question embeddings—A document chunk is indexed with its own embedding and additional embeddings generated from hypothetical questions that it can answer. This allows for more accurate matching with user queries.
Sentence Expansion—A granular chunk can be enhanced by including the content from its preceding and following chunks to provide additional context.
Embedding Structured and Unstructured Content—Structured data, such as tables, should be summarized and embedded just like unstructured text chunks. This ensures that the embeddings match detailed questions as effectively as text embeddings. Use the MultiVectorRetriever to manage both types of content.
Multi-Modal RAG Workflow—1) Data Ingestion: Use a multi-modal LLM to generate an image summary, store the summary embeddings in a vector store, and keep the raw image in a document store. 2) Retrieval: If the summary embeddings match a query, the raw image is returned by the MultiVectorRetriever and fed into the LLM for synthesis.

Summary

  • A basic RAG implementation often produces low-quality results, making it unsuitable for real-world applications.
  • You can improve answer quality using techniques like advanced indexing, multiple query generation, query transformation, content-specific queries, retrieval post-processing (e.g., result reranking), and ensemble methods.
  • Advanced indexing may include refined document splitting strategies, multiple embeddings for coarse chunks, and expanded context for granular chunks.
  • Choose a splitting strategy based on content type: by size (e.g., paragraphs, sentences, words, or characters) or by structure (e.g., chapters or sections).
  • Structured formats like HTML or Markdown benefit from splitting based on document hierarchy.
  • For coarse chunks, enhance retrieval accuracy by using multiple embeddings generated from child chunks, summaries, or hypothetical questions about the content.
  • Embed child chunks using the ParentDocumentRetriever. Use the MultiVectorRetriever to embed chunk summaries, hypothetical questions, or child chunks.
  • Since it’s hard to predict which multi-vector retrieval technique will perform best, experiment with each and compare results to find the optimal approach.
  • For granular chunks, expand retrieval context by including adjacent chunks for a fuller context.
  • For semi-structured content (e.g., text with tables) or multi-modal content (e.g., images and audio), use specialized indexing techniques for optimal results.

FAQ

What is “advanced indexing” in RAG and why does it matter?Advanced indexing augments basic “one-embedding-per-chunk” with multiple, purpose-built embeddings and smarter chunk-linking. It improves recall and precision by: (1) tuning chunk size/overlap, (2) storing multiple vectors per chunk (e.g., child chunks, summaries, hypothetical questions), and (3) returning context-rich chunks for LLM synthesis. This reduces wrong or shallow matches and boosts answer quality.
How should I choose chunk size and overlap?Match chunk size to expected question types: small, granular chunks for detailed queries; larger, coarse chunks for broad queries. Use overlap to preserve context at boundaries. Always A/B test sizes and overlaps for your corpus to balance focus vs context.
Which splitting strategies should I consider, and what are their trade-offs?
  • By document hierarchy: keeps topical coherence; chunk sizes vary. Tools: HTMLHeaderTextSplitter, MarkdownHeaderTextSplitter.
  • By absolute size (tokens/characters): consistent size; may split sentences. Tools: CharacterTextSplitter (incl. from_tiktoken_encoder).
  • By sentence/paragraph/word: preserves meaning; small chunks can lack context. Tool: RecursiveCharacterTextSplitter.
Pick based on document structure, mixed media, and retrieval method (vector vs metadata).
What is the two-layer chunking design (synthesis vs retrieval) and why use it?Use smaller retrieval chunks to create precise embeddings that point to larger synthesis (parent) chunks. Retrieval stays accurate for fine-grained queries, while the LLM receives context-rich parent chunks for better answers. This pattern underpins ParentDocumentRetriever and MultiVectorRetriever.
How does ParentDocumentRetriever improve retrieval?It stores child (granular) chunks and their embeddings in a vector store, while full parent (coarse) chunks live in a document store. Hits on child vectors return the linked parent chunk, combining precise matching with rich context. Typical setup: Chroma + InMemoryStore with parent/child splitters.
When should I use MultiVectorRetriever instead?Use it when you want multiple vector representations per parent chunk (e.g., child chunks, summaries, hypothetical questions). It pairs a vector store with a byte/doc store, returns the parent content for synthesis, and is flexible across strategies. Ingestion can be slower due to managing multiple vectors.
Why embed document summaries and how does it help?Coarse chunk embeddings can be “diluted” by filler content. Summarize each coarse chunk and embed the summary; store these alongside the parent. Summary embeddings are denser and improve retrieval precision, while the retrieved item remains the full parent for context. Trade-off: added LLM summarization cost/time.
What are “hypothetical question” embeddings?For each chunk, generate likely questions it can answer and embed those questions. These vectors often align better with user phrasing, improving matches even when answer wording differs from the query. Retrieved results still return the parent content for synthesis.
What is granular chunk expansion and when should I use it?Store an expanded version of each granular chunk that includes its neighbors (previous/next). At retrieval, return the expanded chunk to provide added context without losing the precision of small-chunk indexing. This is easy to wire with MultiVectorRetriever by linking granular vectors to expanded documents.
Beyond indexing, what improves accuracy in the Q&A stage?
  • Question transformations: clarify/expand the user query; decompose broad questions into sub-questions.
  • Multi-store routing: include structured stores (relational/graph) and route queries appropriately.
  • Structured query generation: have the LLM produce SQL/graph queries for structured data.
  • Post-retrieval filtering: remove irrelevant hits before synthesis.
  • Ensemble strategies: combine advanced indexing, transformations, routing, and filtering for maximal precision.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free