Course map

Topic 065 / Core

Full-text search basics: database full-text search, when a search engine is needed (large scale → 209)

Chapter 5.3 · Indexing, Search, and Query Performance

What and why

Matching words inside text needs more than an exact-match index: "running" should match a search for "run," and results should rank by relevance, not just match. Database full-text search builds an inverted index (term → documents) inside the database engine itself — adequate for most catalogs and content until scale or feature needs outgrow it. Builds on: → 64 (indexing).

Diagram

"Wireless Running Headphones"
   │ tokenize + stem
   ▼
{wireless, run, headphone}  ──► inverted index: run → [doc 7, doc 12, doc 41]

How it works

  1. Tokenize text into terms on indexing.
  2. Normalize each term (lowercase, stemming — "running" → "run").
  3. Build an inverted index: term → list of document ids (and positions, for ranking).
  4. At query time, tokenize and normalize the query the same way, look up matching postings.
  5. Rank results by a relevance score (term frequency and similar signals) rather than returning an unordered match list.

Practical example

ALTER TABLE product ADD COLUMN search_vector tsvector;
UPDATE product SET search_vector = to_tsvector('english', name);
CREATE INDEX idx_product_search ON product USING GIN (search_vector);

SELECT name, ts_rank(search_vector, query) AS rank
FROM product, to_tsquery('english', 'wireless & running') AS query
WHERE search_vector @@ query
ORDER BY rank DESC;

LIKE '%running%' on the same column cannot use this index, cannot match "run" against "running," and returns no ranking at all (illustrative query shapes, not measured timings).

When to use it

  • Use when: catalog or content search at moderate scale, where stemming and basic relevance are enough.
  • Avoid when: faceted navigation, typo tolerance, or relevance tuning at real scale are required — that calls for a dedicated search engine (→ 209).
  • Failure scenarios: LIKE '%term%' on a large table — a full scan, no stemming, no ranking, and unusable once the table or query volume grows.
  • Simpler alternative: plain LIKE for a genuinely tiny table where none of the above matters.

Practice

(10–20 min, lab Postgres). Add a tsvector column and a GIN index to a small product table, then compare EXPLAIN and result quality for a full-text query against LIKE '%…%' for the same search term, including one case where stemming matters (e.g., searching "run" should match a row containing "running"). Done when the full-text query uses the index and matches the stemmed case that LIKE misses.

Shared lab setup

Key takeaway

Database full-text search covers most catalog and content search; reach for a dedicated engine only when faceting, tuning, or volume outgrow it. Done when you can: build a basic full-text index and name the two things LIKE can never give you. Used next in → 209 (dedicated search engine at scale); Project 5.

References

None — this topic makes no external claims.

Source directory

Search the guide