Repository navigation
search: pick snippet window that covers the most distinct query terms - #16
Open
ashishbhateja wants to merge 1 commit into
Open
ashishbhateja wants to merge 1 commit into
ashishbhateja wants to merge 1 commit into
Conversation
The previous snippet() always centred the context window on the earliest occurrence of any matched term. For a multi-term query where the terms cluster together only later in the text (e.g. one isolated early hit and both terms appearing together further on), this produced a snippet showing only one of the matched terms — a poor result for the reader. The new implementation collects every occurrence of every matched term, then picks the candidate position whose window (±half of contextChars) contains the highest count of distinct terms. Single-term behaviour is unchanged; for multi-term queries the displayed snippet is now far more likely to show the words the reader actually searched for, advancing the search-result-quality goal tracked in issue #3. A new smoke test captures the exact failing case: 'silence' appears early (isolated) and again near 'integral' much later; the snippet must now include both.
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 and why
Advances issue #3 (search result quality — snippets and highlighting).
snippet()insrc/search.jspreviously always centred the context window on the earliest occurrence of any matched term. For a multi-term query this produces a poor result when the terms cluster together only later in the text: the reader sees a snippet that shows just one of their search words.Before (
search('integral yoga')where "integral" and "yoga" only co-appear near line 6 of the body, but "yoga" also appears alone in line 1):After:
How
snippet()now collects every occurrence of every matched term in the text, then iterates over those positions and picks the one whose±halfwindow contains the highest count of distinct terms. Single-term behaviour is mathematically identical to before; the change only kicks in when two or more terms are matched.The algorithm is O(occurrences²) which is negligible at magazine scale (a handful of terms, each appearing a few times in a few hundred characters of body text).
Changes
src/search.jssnippet()— best-coverage window selectionscripts/smoke.mjsTests
The new check (
snippet centres on the window covering the most distinct terms) would have failed against the previous implementation, confirming the regression guard is real.Generated by Claude Code