SupabaseS
Supabaseโ€ข4y ago
LukasV

Integrating Full Text Search with supabase

Hi, I have few questions regarding implementation of Supabase full text search, I was reading the docs here https://supabase.com/docs/guides/database/full-text-search#to_tsvector

Currently I have custom react hook function:

const getFullTextSearchIngredients = async (
  queryString: string | undefined
) => {
  let query = supabase
    .from<SupabaseIngredient>('ingredients')
    .select(
      '*',
      {
        count: 'exact'
      }
    )
    .order('popularity', { ascending: false })
    .limit(20);

  if (queryString) {
    query = query.textSearch('product_name', queryString, {
      type: 'phrase'
    });
  }

  const { data, count, error } = await query;

  if (error) {
    throw new Error(`${error.message}: ${error.details}`);
  }

  if (count === 0) {
    return null;
  }
  return data;
};

I pictured an example query, if you have a look "Chicken, broiler or fryes, breast, skinless..." should've been retrieved in both queries, but is only present in the first one.

Now, I do realise that the ingredient name is very obscure and could be renamed, however as I was reading docs there were different functions mentioned such as: to_tsvector() to_tsquery() @@.

I don't understand what they do, but can they be included in supabase javascript method .textSearch() to improve the search?

Or is it just postgres limitations and I should simply rename the ingredients ๐Ÿ˜
unknown.png
How to use full text search in PostgreSQL.
Was this page helpful?