6 min read
How to Build a Full-Text Search for Your Blog (Postgres)
Postgres has powerful built-in full-text search. Learn weighted search, ranking, and autocomplete without external services.
R
ruben
Why Postgres FTS
You do not need Elasticsearch or Algolia for blog search. Postgres has built-in full-text search that handles millions of documents with sub-100ms query times. It is free, requires no external service, and is already running in your stack.
Basic FTS Query
SELECT title, ts_rank_cd(search_vector, query) AS rank\nFROM posts, plainto_tsquery('english', 'headless cms seo') query\nWHERE search_vector @@ query\nORDER BY rank DESC\nLIMIT 10;Weighted Search
Weight title matches higher than body matches:
-- A = title (highest weight), B = excerpt, C = body\nUPDATE posts SET search_vector = \n setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n setweight(to_tsvector('english', coalesce(excerpt, '')), 'B') ||\n setweight(to_tsvector('english', coalesce(content_plain, '')), 'C');Autocomplete
Use prefix matching for type-ahead search:
SELECT title FROM posts\nWHERE search_vector @@ to_tsquery('english', 'head:*')\nLIMIT 5;\n-- Returns: "Headless CMS Guide", "Heading Structure SEO", etc.Search Analytics
Log every search query to understand what your readers are looking for. RankFlo stores search queries with result counts and click positions — giving you content gap insights (queries with zero results = topics you should write about).