Skip to content

Add search results with complete highlighted fields - #67

Open
dblock wants to merge 1 commit into
mongoid:masterfrom
dblock:dblock/highlighting
Open

dblock wants to merge 1 commit into
mongoid:masterfrom
dblock:dblock/highlighting

Conversation

@dblock

@dblock dblock commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #11.

Adds fulltext_search_with_highlights so callers receive documents, scores, and complete highlighted field values together:

Artwork.fulltext_search_with_highlights("ghost", fields: [:title]).each do |result|
  result.document          # the original artwork
  result.score             # its search score
  result.highlights[:title] # "A <b>ghost</b> story"
end

Results preserve search ordering, criteria, filters, and max_results. Existing fulltext_search behavior is unchanged. fields selects indexed fields to display; omitted, it includes all fields in the selected index. Unknown fields raise ArgumentError, and index: selects among multiple indexes.

Highlights contain full strings, never snippets. Arrays preserve order and nil elements; nil scalars stay nil. Unmatched text is escaped and retained, including titles when another field matched. Default markup is <b>...</b>; a block can customize it:

Artwork.fulltext_search_with_highlights("ghost", fields: [:title]) do |match|
  "<mark>#{match}</mark>"
end

The block receives an HTML-escaped match after overlapping and adjacent matches are merged. Its returned markup is inserted verbatim and must be trusted; surrounding text is escaped automatically. Output is intended for HTML element content, not attributes, scripts, or URLs.

Also adds document.fulltext_highlights for already-loaded documents and a standalone highlighter without a Mongoid/database dependency. Computed readers, arrays, Unicode normalization, and localized fields are covered. No positional index storage, document mutation, or index rebuild is required.

Matching follows the existing bounded ngrams, not exact substring matching: partial-word highlights and unmarked portions are possible. Stale indexes can return documents without current highlights. Applications must explicitly choose safe fields to display.

Includes regression coverage and README examples. Rebased onto current master.

Prepared by an AI on behalf of @dblock.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@dblock
dblock force-pushed the dblock/highlighting branch 2 times, most recently from 7fb273d to cd1c850 Compare October 18, 2026 14:27
@dblock dblock changed the title Add independent post-search highlighting without positional storage Add search results with complete highlighted fields Oct 18, 2026
Map sampled query term matches back to original grapheme ranges, return plain and escaped HTML snippets, and expose an optional document helper without changing search result types. No database reads or index migration are needed. Implements mongoid#11 originally requested by @luizkowalski.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dblock
dblock force-pushed the dblock/highlighting branch from cd1c850 to be7b73d Compare October 18, 2026 14:35
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.

Return the highlight on search

1 participant