Module 4 · Retrieval
Lesson 4.7
Parent-child and compression
Parent-child retrieval searches small pieces, then returns the larger section that holds them.
The problem
Why it matters
Without it
A large section covers many topics, so one vector stands for all of them. A question about one detail can score low against it.
With it
The search compares the question with short pieces, which score sharply. The model then reads the whole section, so no detail is lost.
How it works
Cut the section into child pieces
Each sentence of a section becomes one child piece. Every child stores the id of its parent, which is the section named reset. In a real system the parent id is saved with each child in the database.
Child text Parent id Hold the button for 10 seconds reset The light then blinks blue reset Wait one minute before you sign in reset The three children of the reset section. Each one points to the same parent. const section = { id: "reset" };const sentences = [ "Hold the button for 10 seconds", "The light then blinks blue", "Wait one minute before you sign in",];const children = sentences.map((text) => { return { text, parent: section.id };});console.log(children[1]);Output
{ text: 'The light then blinks blue', parent: 'reset' }Score each child against the question
The question is compared with each child, not with the whole section. The question words share 1 word with the first child, 2 with the second, and 0 with the third. A short child gives a sharper score, since no other sentence dilutes it.
Shared words between the question and each child. const query = ["what", "does", "the", "light", "do"];const children = [ "Hold the button for 10 seconds", "The light then blinks blue", "Wait one minute before you sign in",];for (const text of children) { const words = text.toLowerCase().split(" "); console.log(query.filter((w) => words.includes(w)).length, text);}Output
1 Hold the button for 10 seconds 2 The light then blinks blue 0 Wait one minute before you sign in
The best child points to its parent
The best child is "The light then blinks blue", with 2 shared words. The search returns its parent, the whole reset section, so the model reads the instructions around the matched sentence. Those neighbors often hold the warning or the next action.
- Matched child
- Parent id: reset
- Whole section
The path from the matched child to the text the model reads. const sentences = [ "Hold the button for 10 seconds", "The light then blinks blue", "Wait one minute before you sign in",];const best = sentences[1];console.log("Best child:", best);console.log("Parent text:", sentences.join(". ") + ".");Output
Best child: The light then blinks blue Parent text: Hold the button for 10 seconds. The light then blinks blue. Wait one minute before you sign in.
Compression keeps the sentences that help
A long parent can be cut down to the sentences that answer the question. The wait sentence does not help with the light question, so compression drops it. The model gets two sentences instead of three, which keeps the prompt short.
Full parent
- Hold the button for 10 seconds
- The light then blinks blue
- Wait one minute before you sign in
Compressed
- Hold the button for 10 seconds
- The light then blinks blue
The parent text before and after compression, for the light question.
Common mistakes
What goes wrong
- Cutting children too small. A single word has no meaning, so it matches the wrong parents.
- Returning the child alone. The model then lacks the context around the matched sentence.
- Losing the parent pointer when the documents are loaded again. A child with no parent cannot return its section.
Use it when and skip it when
Is it the right tool?
Use it when
- Sections are long, and each one covers several topics.
- The answer needs the text around a matched sentence, such as a step and its warning.
Skip it when
- Every chunk is already short, such as one-line FAQ entries.
- The whole parent is too large for the model, and no compression step is planned.
Words to know
Terms in this lesson
- Parent
- The larger section that holds child pieces.
- Child
- A small piece of a parent, indexed for search.
- Compression
- Removing text that does not help answer the question.
- Pointer
- A stored id that links one record to another.