Sub topic 10
Highlighting Matches with `ts_headline`
This chapter assumes you know PostgreSQL tsvector, tsquery, the @@ operator, and basic SELECT statements. You will use those concepts to add search-result snippets with ts_headline.
What `ts_headline` does
ts_headline is a PostgreSQL function that extracts a readable fragment from the original document and marks terms that match a tsquery. It does not search a tsvector. Instead, it receives the original text, such as an article body.
A typical search has two stages:
- Use
@@to find matching rows. - Use
ts_headlineonly for those rows to create snippets for display.
This keeps snippet generation separate from filtering and avoids doing extra work for rows that will not appear in the results.
SELECT
a.id,
a.title,
ts_headline(
'english',
a.body,
q.query,
'StartSel=<mark>, StopSel=</mark>, MaxWords=35, MinWords=15'
) AS snippet
FROM articles AS a
CROSS JOIN websearch_to_tsquery('english', 'full text search') AS q(query)
WHERE a.search @@ q.query
ORDER BY ts_rank(a.search, q.query) DESC
LIMIT 10;The first argument is the text search configuration. The second is the original document. The third is the tsquery. The options string controls the generated fragment.
| Option | Purpose |
|---|---|
| `StartSel` | Text inserted before a matching term |
| `StopSel` | Text inserted after a matching term |
| `MaxWords` | Approximate upper limit for the fragment |
| `MinWords` | Preferred minimum fragment size |
<mark> is useful for HTML output, but escape the original content appropriately before inserting the result into a web page. Do not assume that generated markup is safe for every output format.
Snippets are not stored search data
The tsvector stores normalized lexemes, positions, and weights. ts_headline reads the original text so it can preserve words and surrounding context. Consequently, it can show running even when the search matched a normalized lexeme such as run.
The configuration must agree with the configuration used to build the query. A mismatch can produce missing highlights or unexpected results.
Common errors and trade-offs
- Calling
ts_headlinefor every row before filtering wastes CPU. - Applying it to very large documents can be expensive.
- A snippet is presentation output, not a substitute for the original document.
- The default markers may not match your application’s output format.
- Search terms can contain punctuation or operators, so prefer
websearch_to_tsqueryfor ordinary user input.
ts_headline chooses fragments based on matching terms and options. It does not guarantee a particular sentence boundary or exact character count. Test the result with representative documents, including documents with no nearby matches and documents containing multiple matches.
Exercise
Why should a query filter rows with @@ before calling ts_headline?
Show answer
ts_headline processes the original document to create a snippet. Filtering first prevents PostgreSQL from generating snippets for rows that will not be displayed.