Skip to content

fix(search): snippet centres on the window with most term co-occurrence - #10

Open
ashishbhateja wants to merge 1 commit into
mainfrom
fix/snippet-multi-term-coverage
Open

ashishbhateja wants to merge 1 commit into
mainfrom
fix/snippet-multi-term-coverage

Conversation

@ashishbhateja

Copy link
Copy Markdown
Owner

What

For multi-term queries, snippet() previously centred its context window on the earliest matched term in the text, regardless of whether other query terms were nearby. That meant a search for "integral yoga" would show a snippet around a lone "yoga" near the top of the body, even when "integral yoga" appeared together further down — so the reader saw the less relevant context.

This PR changes the selection rule: scan every occurrence of every matched term and pick the position whose context window (of contextChars characters) contains the most query terms. When coverage is equal, the earlier position wins, so single-term behaviour is unchanged.

Changes

  • src/search.js — snippet() replaces the linear "first occurrence" scan with a pass over all term occurrences, scoring each by how many other query terms fall within its window.
  • scripts/smoke.mjs — one new assertion covers the multi-term case: a body with "yoga" alone early and "integral yoga" together 200+ characters later; the snippet must include "integral".

Tests

20 checks passed.   ← was 19

npm run build also passes cleanly.

Issue

Advances #3 (client-side search — result snippets and highlighting).


Generated by Claude Code

For multi-term queries the old code centred the snippet on whichever
matched term appeared earliest in the text, so a search for
"integral yoga" would show context around a lone "yoga" near the top
even when "integral yoga" appeared together further down.

The new implementation scans every occurrence of every matched term and
picks the position whose context window (contextChars wide) contains the
most query terms. Ties break in favour of the earlier position, so
single-term behaviour is unchanged.

A new smoke-test assertion demonstrates and protects the improvement.

Advances #3.

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