Chunking

Chunking is the practice of splitting large text into smaller, focused pieces before embedding, so that each piece carries one clear idea and can be found by the right query.

Why This Matters

Picture this: you're building the semantic search feature for EngineerPrep. The goal is simple — a learner types "how does a HashMap work" and gets back the most relevant questions and lessons. You grab a long lesson transcript, call the embedding model once, store the single vector in pgvector, and run your first test query. The result comes back. It looks reasonable. Then you try a more specific query: "HashMap collision resolution." The same giant transcript floats to the top. So does it for "HashMap load factor." And "HashMap vs TreeMap." It matches everything because it talks about everything. Your first instinct is to tune the similarity score. Lower the threshold — more noise. Raise it — miss real results. Nothing fixes it. The problem isn't your query. It isn't pgvector.…

The Simple Idea

An embedding is a list of numbers that captures the meaning of a piece of text. Think of it as a coordinate in a high-dimensional space — similar ideas get coordinates close together. The problem is that a single coordinate can only point to one location . If you try to represent an entire document with a single coordinate, you get something like a weighted average of all its topics — a point that isn't precisely anywhere useful. Chunking is splitting a large text into smaller pieces — called chunks — before you embed it. Each chunk is small enough to carry one clear idea. You embed each chunk separately and store each one as its own row in your database. Now when a learner searches for "HashMap collision resolution," the query vector is compared against many small, focused chunks. The one that actually explains collision resolution scores high. The one about load factors scores low.…

See It in Action

Step 1 — You start with one long document. Imagine a rectangle labeled "HashMap Lesson" — it's tall and wide, representing a full lesson transcript with several topics: what a HashMap is, how hashing works, collision resolution, load factor, and when to use TreeMap instead. One big block. Step 2 — You embed it as-is. Draw a single dot on a 2D map. Label it "HashMap Lesson." Now draw five query dots nearby: "collision resolution," "load factor," "hashing," "TreeMap comparison," "entry lookup." Every one of those queries is roughly the same distance from your single dot. The embedding is a blurry average of all five topics — it matches everything similarly, which means it's precise about nothing. Step 3 — Now you chunk the document. Slice that tall rectangle into five smaller rectangles, each one covering exactly one topic. Each slice is now short enough to carry a single idea. Step 4…