Supabase

How to perform full-text search with Supabase?

Master full-text search with Supabase, the powerful open-source backend. Discover easy-to-follow steps to boost search functionality, and learn optimization tips for more effective results.

Developer profile skeleton
a developer thinking

Overview

Full-text search with Supabase makes it easy to hunt through tons of text data in your PostgreSQL database. Supabase taps into PostgreSQL's strong text search features, so you can index and explore text columns using advanced search tools. The process includes arranging your database structure, setting up text search indexes, and running search queries with solid filtering choices. It’s a handy solution for apps needing fast and useful search results, like content management systems, e-commerce platforms, or sites with user-made content.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to perform full-text search with Supabase?

Step 1: Set Up Supabase

First things first, head over to https://supabase.io/ and sign up for an account. Once you're in, create a new project. Make sure to jot down your project's URL and API key from the dashboard. You'll need these later.

Step 2: Create a Table

Next, go to the "Table Editor" in the Supabase dashboard and whip up a new table. For full-text search, you'll want one or more columns to be of type text. Let's say you create a table called articles with columns id, title, and content.

create table articles (
  id serial primary key,
  title text,
  content text
);

Step 3: Insert Sample Data

Time to add some sample data to your table. This will help you test the full-text search. You can do this using SQL in the SQL Editor:

insert into articles (title, content) values
('Supabase Full-text Search', 'Learn how to implement full-text search with Supabase.'),
('Introduction to Supabase', 'Supabase is an open-source Firebase alternative.'),
('Advanced Supabase Queries', 'Explore advanced queries in Supabase.');

Step 4: Create a Full-Text Search Index

Full-text search in PostgreSQL relies on indexes. So, let's create one for your articles table. Use the SQL Editor for this:

create index articles_search_idx on articles using gin(to_tsvector('english', title || ' ' || content));

This command sets up a GIN (Generalized Inverted Index) on the combined title and content fields, making searches faster.

Step 5: Perform Full-Text Search

Now, you can run full-text search queries. For a basic search, use the @@ operator with to_tsquery in the SQL Editor:

select * from articles
where to_tsvector('english', title || ' ' || content) @@ to_tsquery('full-text & search');

Feel free to tweak the search term 'full-text & search' to whatever you need.

Step 6: Integrate with Application

To bring full-text search into your app, use Supabase client libraries. For JavaScript, start by installing the Supabase client:

npm install @supabase/supabase-js

Then, set up the client and make a query:

import { createClient } from '@supabase/supabase-js'

const supabaseUrl = 'https://your-supabase-url.supabase.co'
const supabaseKey = 'your-public-anon-key'

const supabase = createClient(supabaseUrl, supabaseKey)

async function searchArticles(query) {
  const { data, error } = await supabase
    .from('articles')
    .select('*')
    .filter('tsvector', 'fts', query)

  if (error) {
    console.error('Error fetching articles:', error)
    return []
  }

  return data
}

searchArticles('full-text & search')
  .then(articles => {
    console.log('Search Results:', articles)
  })

Don't forget to replace your-supabase-url and your-public-anon-key with your actual Supabase project's URL and API key.

Step 7: Optimize and Scale

For larger datasets, you might want to optimize index creation and query formulation to strike a balance between performance and accuracy. Keep your indexes updated regularly to maintain search efficiency.

You can use plainto_tsquery for simple searches or phraseto_tsquery for phrase matching to make the search more intuitive:

select * from articles
where to_tsvector('english', title || ' ' || content) @@ plainto_tsquery('full-text search');

select * from articles
where to_tsvector('english', title || ' ' || content) @@ phraseto_tsquery('full-text search');

And there you have it!

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.