A result highlighter generates text fragments from matching documents and visibly marks terms that satisfy a search query. PostgreSQL provides this capability through ts_headline, which can also accept a full-text configuration and output options. section 6.5
Highlight the highest-ranked movie
SELECT id, name, description, ts_headline(description, to_tsquery('pirates')) AS fragments, ts_rank(lexemes, to_tsquery('pirates')) AS rank FROM omdb.movies WHERE lexemes @@ to_tsquery('pirates:B') ORDER BY rank DESC LIMIT 1; The result can return a fragment such as "<b>pirate</b> Captain Jack is in a battle with the ocean itself." The filter searches weighted lexemes, while ts_headline highlights matches in the original description. section 6.5
Listing 6.15 Customizing ts_headline to show additional fragments
SELECT id, name, description,
ts_headline(description, to_tsquery('pirates'),
'MaxFragments=3, MinWords=5, MaxWords=10,
FragmentDelimiter=<ft_end>') AS fragments,
ts_rank(lexemes, to_tsquery('pirates')) AS rank
FROM omdb.movies
WHERE lexemes @@ to_tsquery('pirates:B')
ORDER BY rank DESC LIMIT 1;
Customize headline fragments with MaxFragments, MinWords, MaxWords, and FragmentDelimiter options. section 6.5
