Search Small, Return Big
Search on single sentences, then hand back the larger passage each match sits in. Implement make_children(docs) and small_to_big(docs, query, k=4, window=None, max_tokens=None). Each doc has an id and sections, each with a title and sentences.
- Children.
make_childrenreturns one child per sentence, in document order:{"text", "doc": the doc's id, "section": the section's index in its doc, "pos": the sentence's index in its doc, counted across sections from 0}. - Match small. Score every child with
similarity(query, text)and keep the bestk, best first, dropping any that score 0. Ties keep the children's order. - Return big. Each kept child becomes a passage
{"doc", "span": (first, last), "text"}: positions inclusive,textthe sentences of the span joined with one space. Withwindow=Nonethe span is the child's whole section. Withwindow=wit is the child plus up towsentences either side: it may cross sections, never the edge of its document. - One passage per parent. A child whose sentence is already inside a passage you returned adds nothing: skip it. Passages come out in the order of the child that brought them.
- Budget. With
max_tokens, a passage that would take the totalcount_tokensof the returned texts overmax_tokensis skipped, and later ones are still tried.
The catch. The sentence that matches is often not the one that answers. "Refund requests for annual plans follow a separate rule" matches a refund question well; the next sentence, "They are accepted within 30 days of the renewal date", holds the answer and shares no word with the question. So you search on sentences and return their parents, and a parent's rank comes from its best child, not from how many of its children matched.