Skip to content

search: pick snippet window that covers the most query terms - #25

Open
ashishbhateja wants to merge 1 commit into
mainfrom
feat/snippet-multi-term-window
Open

ashishbhateja wants to merge 1 commit into
mainfrom
feat/snippet-multi-term-window

Conversation

@ashishbhateja

Copy link
Copy Markdown
Owner

What this changes

Improves the snippet() function in src/search.js so 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.mjs passes (20 checks, up from 19 — new test for multi-term cluster case)
  • No new runtime dependencies
  • For UI changes: keyboard, focus, and semantics considered (no UI changes in this PR)
  • No magazine content committed (developed against the sample edition)

Generated by Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants