Full-Text Search — Complete Guide
Full-Text Search — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 58 of 100
Full-Text Search
SQL ✓ → Advanced
Advanced · 2 — Production · ~10 min · PostgreSQL — JSONB & Modern Features
What is this?
Full-text search tokenizes documents into tsvector and matches tsquery — ranked by ts_rank, accelerated with GIN index on to_tsvector output.
Why should you care?
PostgresVerse blog and product description search beats ILIKE '%phone%' that cannot use indexes.
See it live — copy this example
Run in pgAdmin or psql.
ALTER TABLE products ADD COLUMN search_doc tsvector
GENERATED ALWAYS AS (
to_tsvector('english', coalesce(name,'') || ' ' || coalesce(description,''))
) STORED;
CREATE INDEX idx_products_fts ON products USING GIN (search_doc);
SELECT name, ts_rank(search_doc, query) AS rank
FROM products, plainto_tsquery('english', 'wireless keyboard') query
WHERE search_doc @@ query
ORDER BY rank DESC
LIMIT 10;
What happened?
- Generated tsvector column combines name and description.
- GIN index supports @@ match.
- plainto_tsquery parses user phrase; ts_rank orders relevance.
Practice next
- Add description text column with sample prose.
- Create generated search_doc and GIN index.
- Search varied phrases; try typo — note no fuzzy by default.
- Weight fields with setweight on name vs description.
- Add unaccent extension for café vs cafe matching.
Remember
tsvector + tsquery + GIN is PostgreSQL FTS stack. Generated columns keep index in sync. pg_trgm adds fuzzy/substring complement.
PostgresVerse site search
Marketplace search box uses FTS; ranks in-stock items higher in app layer.
Outcome: Search P95 60ms vs 4s ILIKE on catalog table.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!