К содержанию
Статья ещё не опубликована на языке Russian. Показана версия на English.

Document Chunking Strategies for RAG: Fixed, Recursive, Semantic, and Parent-Child

2 мин на чтение

Chunking is the highest-leverage lever in most RAG systems. If a chunk is too small, it loses the surrounding conditions ("only valid on annual plans"). If a chunk is too large, its embedding averages multiple unrelated topics together and lowers retrieval precision.

Comparing Chunking Approaches

StrategyHow It SplitsStrengthsWeaknesses
Fixed-Size + OverlapEvery N tokens with M-token overlapPredictable size, trivial to implementCuts mid-sentence, breaks tables and code blocks
Recursive Character / MarkdownSplits by H2, H3, paragraph, then sentenceRespects natural document hierarchyNeeds special handling for wide tables and lists
Semantic ChunkingSplits where adjacent sentence embeddings divergeClean topical boundaries on long proseSlower indexing; extra embedding calls per sentence
Parent-Child (Small-to-Big)Indexes small child chunks, returns larger parent sectionHigh matching precision + full reading contextSlightly higher storage and token footprint

Structure-Aware Markdown Chunking

For documentation, FAQs, and policies, structure-aware chunking outperforms naive fixed windows. Two practices deliver outsized gains:

  1. Prepend heading breadcrumbs to every chunk: inject Document Title > Section H2 > Subsection H3 at the top of each chunk before embedding so orphaned paragraphs stay anchored to their topic.
  2. Keep atomic blocks intact: never split a Markdown table row or fenced code block mid-element; if a table exceeds the token ceiling, repeat the table header row on each split slice.
ts
export interface StructuredChunk {
  headingPath: string[];
  text: string;
  tokenEstimate: number;
}

export function buildContextualChunkText(
  docTitle: string,
  headingPath: string[],
  body: string
): string {
  const breadcrumb = [docTitle, ...headingPath].filter(Boolean).join(" > ");
  return `${breadcrumb}\n\n${body.trim()}`;
}

Choosing Chunk Sizes by Workload

  • Short FAQ / policy lookups: 250 to 450 tokens with 10 to 15 percent overlap works well for crisp factual recall.
  • Technical procedures and guides: 500 to 800 tokens or Parent-Child retrieval (index 300-token children, pass the 1,000-token parent section to the LLM).
  • Tabular pricing or catalogs: serialize each row with its column headers (Plan: Pro | Monthly Price: $49 | Included Seats: 5) so every row is self-contained when retrieved individually.

Production Checklist

  • Strip navigation chrome, cookie banners, and duplicate footers before chunking crawled pages.
  • Preserve deterministic chunk ordering (chunk_index) so adjacent sibling chunks can be stitched together when needed.
  • Measure chunking changes with Retrieval Evaluation Metrics (Recall@K, MRR, NDCG) instead of eyeballing three test queries.

Next: combine lexical precision with dense semantic recall in Hybrid Search Explained.

Было ли это полезно?