/supabase-tutorials

How to do full-text search in Supabase?

Learn how to implement full-text search in Supabase with our step-by-step guide covering project setup, PostgreSQL configuration, and API integration.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to do full-text search in Supabase?

 

Step 1: Set Up Supabase Project

 

  1. Create a Supabase account or log in: Visit the Supabase website and sign up or log in to your existing account.

  2. Create a new project: After logging in, create a new project by clicking the "New Project" button in the dashboard. Provide the necessary details, such as project name, database password, and region.

 

Step 2: Set Up Your Database

 

  1. Navigate to the SQL Editor: In the Supabase dashboard, click on the "SQL Editor" tab to access the SQL console.

  2. Set up a new table: Create a table where you plan to store the data you want to perform a full-text search on. Use the SQL editor to execute the following SQL command to create a sample table:

    
    CREATE TABLE articles (
      id SERIAL PRIMARY KEY,
      title TEXT,
      content TEXT
    );
    
  3. Add data to your table: Insert some sample data into your new table. Here's an example SQL insert statement:

    
    INSERT INTO articles (title, content) 
    VALUES
    ('First Article', 'This is the content of the first article.'),
    ('Second Article', 'Content for the second article goes here.'),
    ('Third Article', 'And this is the third article content.');
    

 

Step 3: Enable Full-Text Search

 

  1. Create a tsvector column: In order to take advantage of full-text search in PostgreSQL, you'll need to create a column of type tsvector. Add the following column to your table:

    
    ALTER TABLE articles
    ADD COLUMN tsv tsvector;
    
  2. Update the tsvector column: Populate the tsv column with data from your text columns using the to_tsvector function:

    
    UPDATE articles
    SET tsv = to\_tsvector('english', title || ' ' || content);
    
  3. Create a trigger to automatically update the tsvector column: Enable automatic updates of the tsv column whenever the data changes:

    
    CREATE OR REPLACE FUNCTION articles_tsvector_update() RETURNS trigger AS $$
    BEGIN
      NEW.tsv :=
        to\_tsvector('english', NEW.title || ' ' || NEW.content);
      RETURN NEW;
    END;
    $$ LANGUAGE plpgsql;
    
    CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
    ON articles FOR EACH ROW EXECUTE PROCEDURE articles_tsvector_update();
    

 

Step 4: Perform a Full-Text Search Query

 

  1. Use the to_tsquery function: Use the to_tsquery function to perform full-text searches on your data. Here's an example query:

    
    SELECT \* FROM articles
    WHERE tsv @@ to\_tsquery('english', 'content');
    
  2. Use the plainto_tsquery function for plain text search: For a more straightforward text search without operators, use plainto_tsquery:

    
    SELECT \* FROM articles
    WHERE tsv @@ plainto\_tsquery('english', 'first article');
    

 

Step 5: Integrate Full-Text Search with Supabase API

 

  1. Get your API URL and anon key: In the Supabase dashboard, navigate to the "Settings" section and locate the API details, including the URL and anon key.

  2. Set up your client: Integrate the Supabase client into your application using the API URL and anon key. Install the Supabase client library if you haven't already:

    
    npm install @supabase/supabase-js
    
  3. Initialize the Supabase client: Initialize the client in your JavaScript application:

    
    import { createClient } from '@supabase/supabase-js';
    
    const supabaseUrl = 'https://your-project.supabase.co';
    const supabaseAnonKey = 'your-anon-key';
    
    const supabase = createClient(supabaseUrl, supabaseAnonKey);
    
  4. Query data using full-text search: Use the Supabase client to perform full-text search queries:

    
    const { data, error } = await supabase
      .from('articles')
      .select('\*')
      .filter('tsv', 'fts(to\_tsquery)', 'first & article');
    
    if (error) console.log('Error:', error);
    else console.log('Data:', data);
    

 

Conclusion

 

Congratulations! You have successfully set up and implemented full-text search in Supabase. By following the steps outlined above, you can efficiently search through large datasets using powerful PostgreSQL full-text search capabilities within your Supabase projects. Make sure to explore further customization options, such as modifying search configurations, to better suit your application's needs.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022