Module 3 · 7 lessons

Retrieval and vector search in depth

How documents are cut up, turned into numbers and searched, and why mixing word search with meaning search works best.

Before you start

From this track: Modules 0 to 2. Each module uses only ideas from the ones before it.

Builds on: Search indexes and autocomplete, Indexes and storage engines

Already know these? Start with the first lesson below.

Later, to go deeper: Message queues and Kafka

1. Cutting documents into chunks

The picture

Cut it up, then search

A long document is cut into short, slightly overlapping chunks, and each chunk gets its own embedding.

Scene 1 of 4 · One long policy

The refund policy has 15,000 words: far too long and too mixed to match one question.

Refund policyRefund policy15,000 wordsChunkerChunkerVector indexVector index
  • Refund policy — healthy — 15,000 words
  • Chunker — healthy
  • Vector index — healthy
Chunks for this policy
not shown yet
  1. One long policy. The refund policy has 15,000 words: far too long and too mixed to match one question.
  2. Cut into chunks. The chunker cuts it into pieces of 200 new words each: 75 chunks in all.
  3. Overlap the edges. Each chunk also repeats the last sentence of the one before, so no idea is cut in half.
  4. One embedding each. Each chunk gets its own embedding, so the index holds 75 of them for this one policy.
  5. Now you try. Change the chunk size, and see how many chunks the 15,000-word policy becomes. For example: 25 chunks of 600 new words, over this example's 500-word guide. Chunks this big mix several topics, so each embedding is a blur of all of them.

Scene 1 of 4: One long policy. The refund policy has 15,000 words: far too long and too mixed to match one question.

Key points

  1. A whole document is too long and too mixed to match one question.
  2. Each chunk holds a few paragraphs about one idea.
  3. Repeating a sentence across the cut keeps ideas whole.
  4. Every chunk gets its own embedding in the index.

Before a document can be searched by meaning, it is cut into short pieces called , often a few paragraphs each. Each chunk gets its own .

A chunk that is too long mixes several topics, so its embedding is vague and matches poorly. A chunk that is too short loses the sentences around it that explain it.

Many systems let neighbouring chunks overlap by a sentence or two. Then an idea split across a boundary still appears whole in one chunk.

Example. A 15,000-word refund policy might become about 75 chunks of 200 words each, each one repeating the last sentence of the chunk before it.

In system design: Chunk size and overlap are choices you tune by testing real questions, not fixed rules.

Where it goes wrong

  • Cutting at a fixed word count can split a table or a list in half. Cut at headings and paragraphs where you can.
  • A chunk can lose what it is about, such as which product a page covers. Adding the page title to each chunk helps.

In an interview

  • Say how you chose chunk size and overlap, and how you tested them on real questions.

Check yourself

What goes wrong when chunks are far too long?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A 6,000-word guide is cut into chunks of 200 words, with no overlap. About how many chunks is that?

Judge a design

Chunks are cut every 200 words, and one cut splits a price table in half. Questions about prices fail. What is the better design?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

3. Mixing word search and meaning search

The picture

Search by meaning finds a refund ticket for the question "money back", even with no word in common. But it can miss exact terms, such as an error code or a product name.

Word search, also called keyword search, is the opposite: it finds exact terms, but misses different words with the same meaning.

runs both and merges the two ranked lists, so a chunk found by either one can reach the top.

A common way to merge is reciprocal rank fusion: each list gives a chunk 1 ÷ (60 + its rank), and the scores add up. The 60 is a common setting, not a fixed rule.

Example. Pick "I want my money back" above. Word search finds nothing, since no ticket says "money", but meaning search finds the refund tickets, and the merged list keeps them.

In system design: Many systems use hybrid search, because people mix exact names with everyday words.

Where it goes wrong

  • Word search and meaning search score on different scales, so adding raw scores lets one side win. Merge by rank instead, as rank fusion does.
  • Word search can miss a word spelt wrong, so meaning search is still needed beside it.

In an interview

  • Give an example query that each kind of search misses, such as an error code or a question in other words.

Check yourself

A user searches for the error code "E-4012". Why might meaning search alone miss it?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

With rank fusion, each list gives 1 ÷ (60 + rank). A chunk is 1st in word search and 3rd in meaning search. What is its score?

Judge a design

A team merges word search and meaning search by adding their raw scores, and word search wins almost every time. Why?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

4. Reranking the best few

The picture

A careful second look

A reranker reads the question with each top result and rescores them, so the best few reach the prompt.

Scene 1 of 4 · A rough first search

A fast first search returns the top 50 chunks in 15 ms, with a sign-in tip at number 1.

Help appHelp appFirst searchFirst searchRerankerReranker
  • Help app — healthy
  • First search — healthy
  • Reranker — healthy
"My parcel has been stuck for a week": first-search rank, reranker score, new rank
ChunkWasScoreNow
Sign-in tip (highlighted)#1?–
Depot delays#2?–
Tracking updates#3?–
Return labels#4?–
Lost-parcel rule#9?–
  1. A rough first search. A fast first search returns the top 50 chunks in 15 ms, with a sign-in tip at number 1.
  2. Why it ranked first. It shares "stuck" and "for a week" with the question, but it is about the app, not parcels.
  3. Read each pair. The reranker reads the question with each chunk; all 50 take about 100 ms more.
  4. The best few. The lost-parcel rule climbs from number 9 to number 1; only the best 2 are sent on to the model.
  5. Now you try. Choose how many first-search results the reranker rescores. For example: Search and rerank take 25 ms, and the reranker never sees the lost-parcel rule. The first search put it at number 9, below the cut, so no amount of care can find it.

Scene 1 of 4: A rough first search. A fast first search returns the top 50 chunks in 15 ms, with a sign-in tip at number 1.

Key points

  1. The first search scores the question and each chunk apart, so it is rough.
  2. A reranker reads the question and a chunk together.
  3. It is slower, so it only rescores the top few dozen.
  4. A chunk the first search ranked too low to rescore is never seen.

The first search is fast but rough. A is a second, slower model that reads the question and each top chunk together, and scores how well they match.

Because it is slow, it only rescores the top 20 to 100 chunks from the first search. Then the best few go into the .

This two-step shape appears in most search systems: fast and rough over everything first, then slow and careful over a few.

Try the stuck parcel question above: word search pulls in a sign-in ticket. A reranker that reads the whole text would push it down.

Example. Illustrative: a first search returns 50 chunks in 15 ms. A reranker scores those 50 in about 100 ms, and the top 2 go into the prompt.

In system design: Reranking raises answer quality but adds delay, so count it in your .

Where it goes wrong

  • A reranker only sees what the first search found. If the right chunk is not in that first list, reranking cannot bring it back.
  • Reranking adds delay to every question. Weigh it against the gain in answer quality.

In an interview

  • Describe the two steps: a fast, rough search over everything, then a slower, careful model over the top few.

Check yourself

Why does a reranker rescore only the top few dozen results?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

The first search takes 20 ms, and a reranker then scores its top 40 in 120 ms. How long does search take before the prompt is built?

Judge a design

The first search often misses the right chunk entirely. A team adds a reranker over its top 20 to fix this. Will it help?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Research paperPassage Re-ranking with BERT (2019)Nogueira and ChoA model that reads the question and a passage together to rescore the first search’s top results.
  • Official docsCross-EncodersSentence Transformers documentationWhy a reranker is more accurate but too slow to run on every document.

Open this lesson on its own page

5. Measuring search quality

The picture

Did search find the right chunk?

Mark the chunk that answers each saved question, then count how often search puts it in the top k.

Scene 1 of 5 · Mark the right chunk

Each of 8 saved questions is marked with the one chunk that holds its answer.

Saved questionsSaved questions8 marked questionsSearchSearchRight chunk in top k?Right chunk in top k?
  • Saved questions — healthy — 8 marked questions
  • Search — healthy
  • Right chunk in top k? — healthy
8 saved questions and where search ranked the chunk that answers each
Saved questionRight chunk rankedIn top k
Refund window for shoes?1st–
Store credit after 45 days?2nd–
Refunds on sale items?1st–
Return with no receipt?4th–
Swap for another size?1st–
Send returns from Canada?Not in top 10–
Who pays return postage?3rd–
Refund to a gift card?1st–
  1. Mark the right chunk. Each of 8 saved questions is marked with the one chunk that holds its answer.
  2. Note each rank. For every question, record where the search placed its marked chunk in the results.
  3. Recall at 1. Only 4 of 8 questions have their chunk in first place.
  4. Recall at 4. Taking the top 4 finds 7 of 8, at the price of a longer prompt.
  5. Never found at all. "Send returns from Canada?" is never found, whatever k is, so the search itself needs fixing.
  6. Now you try. Set k, the number of chunks kept, and count the questions whose right chunk is among them. For example: Recall at 2: 5 of 8 questions. A bigger k finds more right chunks, but each one adds tokens to every prompt.

Scene 1 of 5: Mark the right chunk. Each of 8 saved questions is marked with the one chunk that holds its answer.

Key points

  1. Each saved question names the one chunk that holds its answer.
  2. Recall at k counts how often that chunk lands in the top k.
  3. A bigger k finds more, but every extra chunk lengthens the prompt.
  4. A chunk that is never found needs better chunking, not a bigger k.

A wrong answer has two possible causes: the search missed the right text, or the model misused it. Measure the search on its own, so you know which part to fix.

Take saved questions and mark, for each one, the chunk that holds its answer. Run the search, and note where that chunk ranks.

is the share of questions whose right chunk lands in the top k results. A higher k finds more, but every extra chunk makes the prompt longer and costlier.

A question whose chunk is never found needs a different fix: better chunking, a missing page, or word search for an exact term.

Example. In the panel above, recall at 1 is 4 of 8 questions, and recall at 4 is 7 of 8. The Canada question misses at every k, so its fix lies in chunking or the index.

In system design: Track recall at k beside answer quality, and measure both again whenever chunking, the embedding model or the index settings change.

Where it goes wrong

  • Judging search only by the final answers mixes two causes. Measure recall at k on its own as well.
  • Marked questions grow stale as documents change. Update the marked chunks when pages move or are rewritten.

In an interview

  • Say how you measure search on its own, such as recall at 5 on marked questions.

Check yourself

Recall at 5 is 7 of 8, and one question's right chunk is never found. What is the best next step?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

Out of 10 marked questions, the right chunk is in the top 3 results for 6 of them. What is recall at 3?

Judge a design

A team judges search only by how many final answers are right. After a change, answers get worse. What can the team not tell?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

6. Keeping the index up to date

The picture

When a page changes

An edited page reaches the index as an event: the indexer replaces that page's chunks, so answers follow the change within minutes.

Scene 1 of 5 · The policy changes

At 09:00 someone edits the help page: refunds now close after 14 days, not 30.

Help pagesHelp pagesrefunds: 14 daysChange eventsChange eventsIndexerIndexerVector indexVector indexstill says 30
  • Help pages — healthy — refunds: 14 days
  • Change events — healthy
  • Indexer — healthy
  • Vector index — healthy — still says 30
  1. The policy changes. At 09:00 someone edits the help page: refunds now close after 14 days, not 30.
  2. Wait for the night. A rebuild at 02:00 leaves the old 30-day answer live for 17 hours.
  3. One event per edit. Instead, the edit puts an event on a queue, and the indexer picks up only that page.
  4. Replace, not add. It deletes the page's old chunks and writes the new ones, about 1 minute after the edit.
  5. Keep one model. Switching embedding models needs a second index, filled from every page, before searches move to it.
  6. Now you try. Pick a way to keep the index up to date, and see what a refund question finds at noon. For example: It finds: only the old 30-day chunk. Out of date for 17 hours. The change waits for the 02:00 rebuild, so the old answer stays live for 17 hours.

Scene 1 of 5: The policy changes. At 09:00 someone edits the help page: refunds now close after 14 days, not 30.

Key points

  1. A nightly rebuild leaves changed facts wrong for most of a day.
  2. An event per edit lets one worker re-index only that page.
  3. Replacing a page means deleting its old chunks, not only adding new ones.
  4. A new embedding model means re-embedding every page before switching.

Documents change: a price goes up, a policy changes, a page is deleted. The index must follow, or search keeps finding the old text and the model answers from it.

Rebuilding the whole index on a timer is simple but slow to catch up. A common design sends an event for each edit through a , and a worker re-chunks and re-embeds only that page.

An update must replace, not add: delete the page's old chunks, or both versions stay findable. Storing each chunk's page and version makes this easy.

Vectors from two embedding models cannot be compared. Changing the model means re-embedding every page into a new index, then switching searches over.

Example. Example times: the refund window drops from 30 to 14 days at 09:00. A nightly rebuild at 02:00 serves the old answer for 17 hours; a change event updates the index in about a minute.

In system design: Index updates are a data : events, a queue, workers and deletes. Measure how far the index lags behind its sources.

Where it goes wrong

  • A deleted page stays findable if its chunks are never removed from the index.
  • An indexing worker that falls behind serves old answers with no error. Watch the time from an edit to its index update.

In an interview

  • Describe the update pipeline: change events, a queue, workers that can safely run twice, and a measure of the lag.

Check yourself

A help page is edited, and the indexer adds its new chunks but keeps the old ones. What can go wrong?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A price changes at 14:00, and the index is rebuilt every night at 02:00. How long does search keep finding the old price?

Judge a design

A queue can deliver the same edit event twice. What should the indexing worker do so that a repeat does no harm?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

7. Why a RAG answer is wrong

The picture

What goes wrong
Customer, asks a questionCustomerasks a questionSupport assistant, searches, then asks, NOWSupport assistantsearches, then asksquestion inNOWDocument index, chunks and vectorsDocument indexchunks and vectorsModel, writes the answerModelwrites the answer
  • Customer — asks a question — healthy
  • Support assistant — searches, then asks — healthy — NOW — question in
  • Document index — chunks and vectors — healthy
  • Model — writes the answer — healthy
  • Question or prompt
  • Search
  • Chunks or answer
Step 1 of 7

Step 1 of 7 · Support assistant

Done: A customer asks about the Team plan

What happens
"How much storage does the Team plan include?"
Why
The answer is a fact that changes with each price update, so the assistant looks it up in your documents.
What changes
The assistant searches the index first.
Next
The index finds the 2 closest chunks

Event log

6 more to go. Press a line to go back to that step.

Current pricing chunk
not searched yet
The answer says
nothing yet
Which part failed
not checked yet

The index, closest chunk first

Not searched yet.

Step 1 of 7: Support assistant. A customer asks about the Team plan. "How much storage does the Team plan include?"

What this simplifies. The four chunks and their scores are stated examples, not real embeddings, and the model's answer is fixed for each case. A real index holds thousands of chunks, and a real check reads every claim in a long answer. The method is the same: look at what the search found before you blame the model.

When a answer is wrong, one of two parts failed: the search or the answer. The fixes are different, so find the failing part first. Look at the chunks sent with the prompt. Is the fact the answer needed in one of them?

If it is not, the search failed. The right page may be missing from the index, ranked too low, or beaten by an old copy that was never deleted.

If it is, the answer failed. The model dropped a detail, mixed up two chunks, or added a fact of its own. Asking it to quote its source, and checking each claim against the chunks, catches much of this.

A missing fact also tests the prompt. Tell the model to reply that it does not know when no chunk answers the question. Otherwise it may fill the gap with a number from somewhere else.

Example. In the picture, one question goes wrong three ways. Old copies outrank the current page, so the answer says 1 TB. The new page is not indexed, so the model borrows 200 GB from another plan. The current chunk ranks first, but the answer drops "per user".

In system design: Log the chunk ids sent with every answer, so a bad answer can be traced to the search or the model. Test each part on its own.

Where it goes wrong

  • Changing the prompt when the search failed, or the other way round, wastes days. Look at the chunks that were sent first.
  • Without the chunk ids logged, a bad answer from last week cannot be traced to its cause.

In an interview

  • Walk through one wrong answer: check the chunks sent, then decide whether the search or the answer step failed.

Check yourself

A RAG assistant says a plan costs $20 a month, but the pricing page says $25. The logs show the $25 chunk was sent in the prompt. Which part failed?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

The chunks sent with a wrong answer do not contain the fact it needed. Which part failed?

Judge a design

No chunk answers the question, and the bot gives a price taken from another plan. What prompt change helps?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page