arrow_back All articles

Beyond Flat Search - Mastering Hierarchical RAG

William Golovlev · 2 min read AI Engineering
Beyond Flat Search - Mastering Hierarchical RAG

Most RAG (Retrieval-Augmented Generation) systems assume that slicing a document into equal-sized chunks and grabbing the top 5 is enough. For dense scientific papers, it isn’t. A paragraph on page 4 might depend on a table on page 12, and flat retrieval will happily hand the model one without the other.

For my Seed Oil Research Analyst, I dropped basic chunking for Hierarchical Node Parsing and Auto-Merging Retrieval.

Parents, Children and Auto-Merging 🌳

Flat RAG treats your data like a deck of cards spread on a table. Hierarchical RAG treats it like a family tree. With LlamaIndex, each document gets split at several sizes so you keep both the big picture and the details:

  1. Parent nodes: Large chunks (e.g., 2048 tokens) that hold the overall context, like a study’s full methodology or conclusion.
  2. Child nodes: Each parent is split into smaller pieces (e.g., 512 and 128 tokens). These are what actually get indexed in Pinecone.
  3. Auto-merge: If the retriever finds that, say, 3 out of 4 children of a parent are relevant, it doesn’t send those fragments to the LLM. It swaps them for the whole parent node.

Why it helps: You get the precision of small-chunk search with the context of a large chunk, and the answers stop feeling choppy.

When to use it: Long, structured documents where meaning spans sections (papers, contracts, manuals). For short FAQs or notes, flat chunking is fine.

Parsing Scientific PDFs 🧬

Scientific PDFs are a nightmare for text extraction: multi-column layouts, complex tables, footnotes everywhere.

I used LlamaParse, which uses vision-based models to understand the page layout, not just the text. So when a study reports linoleic acid oxidation in a table, the table structure survives, and the hierarchical parser can connect those rows to the summary that discusses them.

Thought Provoker: If a RAG system answers from one fragment of a study but misses the “limitations” section three pages later, is that an insight or a dangerous half-truth? This is why keeping context intact matters more than just finding the right words.

The Gatekeeper: Stopping Off-Topic Hallucinations 🧭

One of the biggest problems I hit: ask something out of scope, like “What’s the weather?”, and a vector database will still return its closest match, even if that match is only 20% relevant. The LLM then tries to force that weak match into an answer.

My fix was a similarity gatekeeper:

  • Score threshold: A strict cutoff of 0.75+ similarity. If nothing in the peer-reviewed library clears it, the system stops and doesn’t answer.
  • Result: Instead of inventing a link between weather and seed oils, it stays in its lane. It acts like a careful technical analyst instead of an eager-to-please chatbot.

What I Took Away 🌐

The model matters (this one runs Llama 3 on Groq), but most of the quality in RAG comes from the plumbing.

  • State management: With Chainlit, I had to keep the UI locked until the whole hierarchical tree was loaded into memory. Async here isn’t just about speed, it’s about not letting users ask questions of a half-loaded index.
  • Responsibility: When a tool summarizes medical or scientific research, “close enough” isn’t good enough.

Auto-merging keeps the context, and the similarity cutoff keeps it honest. Together they make the system show its work instead of just generating.

Challenge: If you were building a research assistant, would you want it to be “creative” and fill in gaps, or “rigid” and refuse to answer without 100% certainty?

#RAG#LlamaIndex#Hierarchical RAG#Auto-Merging Retrieval#AI Architecture#Advanced RAG