Repository navigation
search: pick snippet window that covers the most query terms - #25
Open
ashishbhateja wants to merge 1 commit into
Open
ashishbhateja wants to merge 1 commit into
ashishbhateja wants to merge 1 commit into
Conversation
snippet() previously anchored on the earliest occurrence of any matched term, so a multi-term query like "yoga equanimity" would show the opening paragraph where "yoga" first appears, potentially missing the later passage where both terms cluster. The function now collects every occurrence position for every matched term, evaluates each as a candidate window anchor, and picks the start that maximises the count of distinct query terms visible within the 120-char window. Single-term queries are unaffected (one term, one anchor choice). Adds one smoke test that demonstrates the improvement: "yoga" appears both at position 0 and near "equanimity" late in the body; the new algorithm finds the later cluster and includes both terms in the snippet. Advances #3 (result snippets / search quality).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Improves the
snippet()function insrc/search.jsso that multi-term queries anchor the preview window on the passage where the greatest number of query terms cluster together, rather than always centering on the earliest occurrence of any term.Before: for a query like
"yoga equanimity",snippet()found the first occurrence of"yoga"in the article (often in an opening paragraph) and cut a 120-char window around it. If"equanimity"appeared 150+ chars later, it was invisible in the snippet — giving the reader a preview with no context for half the query.After:
snippet()now collects every occurrence position for every matched term, evaluates each as a candidate window anchor, and picks the start that maximises the count of distinct query terms visible within the window. Single-term queries are unaffected (one term, one anchor).The implementation stays dependency-free and O(positions × terms) per field — comfortably within the scale of a magazine article.
Why
Advances #3 (result snippets / search quality). Better snippets help readers confirm at a glance that a result actually discusses what they searched for, which matters especially for the thematic, multi-word queries this magazine's content invites ("integral yoga", "silence equanimity", "vedanta discernment").
Checklist
node scripts/smoke.mjspasses (20 checks, up from 19 — new test for multi-term cluster case)Generated by Claude Code