0
0
Supabasecloud~30 mins

Full-text search with Postgres in Supabase - Mini Project: Build & Apply

Choose your learning style9 modes available
Full-text search with Postgres
📖 Scenario: You are building a simple blog platform using Supabase, which uses Postgres as its database. You want to add a feature that allows users to search blog posts by keywords in the title and content.
🎯 Goal: Create a Postgres table for blog posts and implement a full-text search configuration that allows searching posts by keywords in the title and content.
📋 What You'll Learn
Create a table called posts with columns id, title, and content.
Add a column search_vector of type tsvector to store searchable text.
Create a trigger to update search_vector automatically when title or content changes.
Write a query that uses full-text search to find posts matching a search phrase.
💡 Why This Matters
🌍 Real World
Full-text search is used in many applications like blogs, e-commerce, and document management to let users find relevant content quickly.
💼 Career
Knowing how to implement full-text search with Postgres is valuable for backend developers and cloud engineers working with databases and search features.
Progress0 / 4 steps
1
Create the posts table
Create a table called posts with columns id as serial primary key, title as text, and content as text.
Supabase
Hint

Use SERIAL PRIMARY KEY for id to auto-increment.

2
Add a search_vector column
Alter the posts table to add a column called search_vector of type tsvector.
Supabase
Hint

The tsvector type stores searchable text for full-text search.

3
Create a trigger to update search_vector
Create a trigger function called posts_search_vector_update that updates search_vector by combining title and content using to_tsvector('english', ...). Then create a trigger on posts that calls this function before insert or update.
Supabase
Hint

The trigger updates search_vector automatically when a post is added or changed.

4
Write a full-text search query
Write a SELECT query to find all columns from posts where search_vector matches the search phrase 'cloud infrastructure' using to_tsquery('english', 'cloud & infrastructure'). Order results by rank using ts_rank.
Supabase
Hint

Use @@ operator to match search_vector with to_tsquery. Use ts_rank to order results by relevance.