A snippet() snippet
snippet()
is a SQLite function that returns a snippet of text containing the search term. It’s used to highlight search results. This example shows an ActiveRecord scope that enables selection additional fields title_snippet
and body_snippet
from an FTS5 virtual table (created separately).
https://www.sqlite.org/fts5.html#the_snippet_function
app/models/page.rb
scope :with_snippets do
select("pages.*")
.select("snippet(pages_search_index, 0, '<mark>', '</mark>', '…', 32) AS title_snippet")
.select("snippet(pages_search_index, 1, '<mark>', '</mark>', '…', 32) AS body_snippet")
end