Advanced RAG: Chunking Strategies & Hybrid Search Systems

Tutorial · 10 min read · Updated July 2026

Naively splitting code files by fixed character lengths (e.g. 500 tokens) ruins context boundaries. High-accuracy RAG systems require AST (Abstract Syntax Tree) code splitting, parent-child document retrieval, and hybrid dense/sparse vector search.

1. AST Code Splitting vs Fixed-Size Chunking

Fixed-character chunking often slices functions in half, severing variable declarations from their return statements. Tree-sitter AST splitters parse source code into complete semantic units (classes, functions, interface definitions) before vectorization.

2. Parent-Child Document Retrieval

Store small, granular chunks (100 tokens) in the vector database for high-precision vector matching, but return the larger parent document (1000 tokens) or full module context to the LLM during generation.

3. Hybrid Search: BM25 + Vector Embeddings

Combine sparse keyword search (BM25) with dense vector embeddings (bge-m3 / text-embedding-3) using Reciprocal Rank Fusion (RRF) to retrieve both exact code symbols and semantic concepts.