Sub topic 1
Why LIKE Is Not Enough for Text Search
Prerequisites
This chapter assumes basic SQL, including SELECT, WHERE, and ORDER BY. You should also know how to create PostgreSQL tables and indexes and how to run queries in psql or another SQL client.
LIKE compares text patterns. For example, this query finds titles that start with postgres:
SELECT id, title
FROM articles
WHERE title LIKE 'postgres%';The percent sign (%) matches any sequence of characters. An underscore (_) matches one character. With ILIKE, PostgreSQL performs a case-insensitive pattern comparison:
SELECT id, title
FROM articles
WHERE title ILIKE '%postgres%';This works for simple patterns, but it does not model how people search for language.
What LIKE Cannot Understand
A natural-language search usually expects related word forms to match. A search for run may need to find running and ran. A search for database indexing may need to find those words anywhere in a document, regardless of punctuation or capitalization.
LIKE treats the value as a string of characters. It does not split text into words, remove common words, reduce words to stems, or understand word order.
| Requirement | `LIKE` or `ILIKE` | PostgreSQL full-text search |
|---|---|---|
| Find an exact character pattern | Yes | Not its main purpose |
| Ignore capitalization | `ILIKE` | Usually through configuration |
| Match related word forms | No | Yes, with stemming |
| Search separate words | Manual patterns | Yes |
| Rank matching documents | No | Yes |
| Search efficiently in long documents | Often difficult | Yes, with a suitable index |
A leading wildcard such as '%index%' can require PostgreSQL to inspect every row. Even a pattern without a leading wildcard has limited meaning: title LIKE 'data%' finds a character prefix, not words such as metadata or data-driven according to linguistic rules.
The Full-Text Search Model
PostgreSQL full-text search converts a document into a tsvector. A tsvector is a normalized representation containing lexemes, which are searchable word forms. It can remove stop words such as the, normalize capitalization, and stem words according to a language configuration.
A search string becomes a tsquery. The @@ operator tests whether a document's tsvector satisfies that query.
SELECT to_tsvector('english', 'PostgreSQL indexes make searches faster')
@@ to_tsquery('english', 'index & search') AS matches;The English configuration can normalize indexes to a lexeme related to index and searches to one related to search. This gives the query a linguistic working model instead of a character-pattern model.
Choosing the Right Tool
Full-text search is not automatically better. It may not be appropriate for exact identifiers, short codes, or arbitrary substrings. PostgreSQL also provides pg_trgm for similarity and typo-tolerant matching, which fills a different gap.
For article search, product descriptions, documentation, or comments, full-text search is usually the better foundation. Later chapters build this foundation with query constructors, ranking, stored tsvector values, and indexes.
Exercise
Why will ILIKE '%running%' usually fail to find a document containing ran, while full-text search may find both?
Show answer
ILIKE compares literal characters and does not understand word forms. Full-text search can use a language configuration that normalizes related forms to compatible lexemes.