Sub topic 16
Searching Titles with Full-Text Search
This chapter assumes basic SELECT statements, PostgreSQL tables, and indexes. You should also have the books table from earlier chapters, with a non-null title column.
A normal comparison such as WHERE title ILIKE '%database%' searches characters in a string. It works for small tables, but it does not understand words, ranking, stemming, or search operators. PostgreSQL full-text search converts text into searchable words and uses an index to find matching rows efficiently.
Turning titles into searchable documents
A tsvector is PostgreSQL's structured representation of a document's words. A tsquery is a search expression that describes the words to find.
Add a generated column to the books table:
ALTER TABLE books
ADD COLUMN search tsvector
GENERATED ALWAYS AS (to_tsvector('english', title)) STORED;The english configuration removes common stop words and reduces related words to a common stem. For example, searches for run can match forms such as running, depending on the configuration.
The column is generated automatically whenever title changes. STORED means PostgreSQL saves the calculated value instead of calculating it during every search.
Create a GIN index on the generated column:
CREATE INDEX books_search_idx
ON books
USING gin (search);A GIN index is designed for values containing multiple searchable elements, such as words in a tsvector. It usually performs better than a B-tree index for full-text containment searches.
Running searches
Use websearch_to_tsquery to turn familiar search text into a query:
SELECT book_id, title
FROM books
WHERE search @@ websearch_to_tsquery('english', 'distributed systems');The @@ operator tests whether the tsvector matches the tsquery. With web-style syntax, users can write phrases in quotes, use OR, and prefix terms with - to exclude them:
SELECT title
FROM books
WHERE search @@ websearch_to_tsquery(
'english',
'"database design" OR PostgreSQL -beginner'
);This searches for the phrase database design or the word PostgreSQL, while excluding titles containing beginner.
You can also show which parts matched:
SELECT title,
ts_headline(
'english',
title,
websearch_to_tsquery('english', 'postgresql')
) AS highlighted_title
FROM books
WHERE search @@ websearch_to_tsquery('english', 'postgresql');ts_headline returns a formatted version of the title with matching terms marked. Treat this output as display text, not as trusted HTML.
Trade-offs and common errors
Full-text search is useful for words and phrases, but it is not a general substring search. A search for data may not match an arbitrary fragment such as metadata. Use ILIKE or a trigram index when users need substring or typo-tolerant matching.
A GIN index makes searches faster, but it adds storage and makes writes slightly more expensive. Create it when search is a real query pattern, then confirm its use with EXPLAIN (ANALYZE).
Exercise
Write a query that finds books whose titles contain either "SQL" or "PostgreSQL", while excluding titles containing "beginner".
Show answer
SELECT title FROM books WHERE search @@ websearchtotsquery('english', 'SQL OR PostgreSQL -beginner');