10. Full-Text Search with Postgres
Enterprise Search Without a Separate Service
Postgres includes a powerful built-in full-text search engine. For most applications, it eliminates the need for Elasticsearch or Algolia — at a fraction of the operational complexity. Supabase exposes this via the textSearch filter in the client SDK.
Full-text search is built on two key concepts: tsvector and tsquery. A tsvector is a pre-processed version of your text — words are reduced to their stems (stemming), common words are removed (stopwords), and the terms are sorted alphabetically. This preparation makes searching extremely fast. A tsquery is your search query — it can include boolean operators like AND, OR, and NOT. The @@ operator matches a tsvector against a tsquery. To make this performant, you add a GIN index on the tsvector column. This creates an inverted index that maps each word to the rows that contain it. Full-text search in Postgres is production-tested at massive scale. It's used by companies with billions of documents.
Core Concepts
The tsvector data type stores a processed version of text. The processing includes stemming — converting words to their root form. For example, running becomes run. Stopwords like the and is are removed. The processed text is stored as lexemes with position information. This allows phrase searches and relevance ranking. The tsquery data type represents a search query. It can combine terms with & (AND), | (OR), ! (NOT), and -> (FOLLOWED BY). The GIN index on a tsvector column enables instant lookups. Lexemes are the normalized word stems stored in tsvector. Understanding these concepts is essential for building effective search. For example, searching for searches will also find searching and searched because they share the lexeme search. This is the power of stemming.
A processed, normalized form of text — words reduced to their stems, stop words removed. Example: running cats → run:1 cat:2
A search query that can combine terms with & (AND), | (OR), ! (NOT), and <-> (FOLLOWED BY). Example: postgres & search
Generalized Inverted Index. Stores a mapping from each lexeme to the rows containing it. Makes text search instant on millions of rows.
The normalized word stems stored in tsvector. searches, searching, searched all become the lexeme search.
-- Add a generated tsvector column for fast search ALTER TABLE posts ADD COLUMN search_vector TSVECTOR GENERATED ALWAYS AS ( setweight(to_tsvector('english', COALESCE(title, '')), 'A') || setweight(to_tsvector('english', COALESCE(body, '')), 'B') ) STORED; -- Create a GIN index on the generated column CREATE INDEX idx_posts_search ON posts USING GIN(search_vector);
Using textSearch in the SDK
The Supabase client SDK provides a convenient textSearch method. It accepts a search vector column name and a query string. The type parameter controls how the query is parsed. The websearch type mimics Google's search syntax — it supports AND, OR, quoted phrases, and negation. The plain type requires all words to be present. The phrase type requires the exact phrase. Choose the type based on your use case. For most user-facing search, websearch is the best choice. The config parameter specifies the language dictionary to use. Use english for English text, french for French, etc. The SDK builds the appropriate SQL query for you. This simplifies implementing search in your application.
// Basic text search const { data } = await supabase .from('posts') .select('id, title, body') .textSearch('search_vector', 'supabase realtime', { type: 'websearch', config: 'english' });
setweight() to assign A/B/C/D weights to different fields. Title matches (A) rank higher than body matches (B). This lets you build relevance-ordered results that surface the most useful content first.Advanced: Trigram Similarity for Fuzzy Search
Trigram similarity is the standard solution for fuzzy search — finding matches even when the user has typos. The pg_trgm extension breaks text into groups of three consecutive characters. It then calculates the similarity between strings based on matching trigrams. This catches typos and spelling variations. For example, supabaes will match supabase with high similarity. To use trigram similarity, create a GIN index with the gin_trgm_ops operator class. Then use the % operator to check similarity. The threshold is configurable with pg_trgm.similarity_threshold. This is a powerful addition to full-text search. Use it for search autocomplete, product search, or any user-facing search where typos are common.
-- pg_trgm: similarity search (finds 'postgress' when user types 'postgres') CREATE EXTENSION IF NOT EXISTS pg_trgm; CREATE INDEX idx_posts_title_trgm ON posts USING GIN(title gin_trgm_ops); -- Find titles similar to a typo-tolerant query SELECT title, similarity(title, 'supabaes') AS score FROM posts WHERE title % 'supabaes' ORDER BY score DESC LIMIT 10;
Knowledge Check
Ready to test your understanding of 10. Full-Text Search with Postgres?