Full-text search helps you find words or phrases quickly in large text data. It works like a smart search engine inside your database.
Full-text search with Postgres in Supabase
SELECT * FROM table WHERE to_tsvector('english', column) @@ to_tsquery('english', 'search_query');
to_tsvector converts text into searchable tokens.
to_tsquery creates the search query from your keywords.
articles where content contains both 'cloud' and 'infrastructure'.SELECT * FROM articles WHERE to_tsvector('english', content) @@ to_tsquery('english', 'cloud & infrastructure');
SELECT * FROM products WHERE to_tsvector('english', description) @@ to_tsquery('english', 'fast | quick');
SELECT * FROM feedback WHERE to_tsvector('english', comments) @@ to_tsquery('english', 'error:*');
This creates a simple articles table, adds three rows, and searches for articles whose content contains both 'cloud' and 'servers'.
CREATE TABLE articles ( id SERIAL PRIMARY KEY, title TEXT, content TEXT ); INSERT INTO articles (title, content) VALUES ('Cloud Basics', 'Cloud computing is a way to use servers over the internet.'), ('Infrastructure Guide', 'Infrastructure includes hardware and software components.'), ('Fast Search', 'Full-text search helps find words quickly in text data.'); SELECT id, title FROM articles WHERE to_tsvector('english', content) @@ to_tsquery('english', 'cloud & servers');
Full-text search ignores common words like 'the' or 'is' to speed up search.
Use indexes on to_tsvector columns for faster search on large data.
Supabase supports Postgres full-text search natively, so you can run these queries directly.
Full-text search finds words or phrases inside text columns quickly.
Use to_tsvector and to_tsquery functions in Postgres to build search queries.
It works well for blogs, products, feedback, or any text-heavy data.
Practice
to_tsvector in Postgres full-text search?Solution
Step 1: Understand
to_tsvectorfunctionto_tsvectorconverts plain text into a special searchable format called a tsvector, which breaks text into words and normalizes them for searching.Step 2: Identify its role in full-text search
This conversion allows Postgres to quickly find matching words during a search query.Final Answer:
To convert text into a searchable document format -> Option AQuick Check:
to_tsvector= searchable format [OK]
- Confusing to_tsvector with data insertion
- Thinking it deletes text
- Assuming it backs up data
to_tsquery in Postgres?Solution
Step 1: Understand
to_tsqueryusageto_tsquerycreates a search query from a string like 'search & term'. It should be on the right side of the @@ operator.Step 2: Check correct operator usage
The @@ operator checks if the tsvector (usually a column) matches the tsquery. So the column (tsvector) is on the left, andto_tsqueryis on the right.Final Answer:
SELECT * FROM articles WHERE content @@ to_tsquery('search & term'); -> Option DQuick Check:
tsvector @@ tsquery = correct syntax [OK]
- Putting to_tsquery on the left side
- Using = instead of @@ operator
- Passing column name inside to_tsquery
products with a column description, what will this query return?SELECT id FROM products WHERE to_tsvector(description) @@ to_tsquery('red & apple');Solution
Step 1: Analyze the query logic
The query usesto_tsvector(description)to convert descriptions into searchable format, then checks if it matchesto_tsquery('red & apple').Step 2: Understand the & operator in tsquery
The & means AND, so it matches rows containing both 'red' and 'apple' anywhere in the description, not necessarily as a phrase.Final Answer:
All products with descriptions containing both 'red' and 'apple' -> Option AQuick Check:
'red & apple' means both words present [OK]
- Thinking & means OR
- Assuming exact phrase match
- Confusing syntax causing no results
SELECT * FROM feedback WHERE to_tsvector(comment) @@ to_tsquery('happy | sad');What is the most likely reason?
Solution
Step 1: Check query correctness
The syntax and operator usage are correct for full-text search withto_tsvectorandto_tsquery.Step 2: Identify performance and result issues
If no results appear, often thecommentcolumn is not indexed as a tsvector, so the search is slow or ineffective.Final Answer:
Thecommentcolumn is not indexed with a tsvector column -> Option BQuick Check:
Missing tsvector index causes no results [OK]
- Assuming syntax error when query is correct
- Using LIKE instead of full-text search
- Misusing @@ operator
Solution
Step 1: Understand full-text search optimization
Creating a generated column that stores the tsvector form of the text allows fast searching without recalculating on each query.Step 2: Use GIN index for fast lookup
Adding a GIN index on the tsvector column speeds up searches dramatically, making queries efficient and scalable.Step 3: Query with @@ operator for accuracy
Using the @@ operator withto_tsqueryon the indexed tsvector column ensures accurate full-text matching.Final Answer:
Create a generated tsvector column indexed with GIN and query using @@ operator -> Option CQuick Check:
Generated tsvector + GIN index = fast, accurate search [OK]
- Using LIKE instead of full-text search
- Not indexing tsvector columns
- Relying on regex for large text search
Ready to prove it?
Pass the test and this pattern moves unknown
