有没有办法用Javascript对Supabase上的多个列执行全文搜索?

cqoc49vn  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(127)

我尝试过使用许多符号来分隔列;||,|、&&、&,有空格和无空格。
比如说

.textSearch("username, title, description", "...");
.textSearch("username|title|description", "...");

而且什么都没有起作用:(

mnowg1ta

mnowg1ta1#

You could create a SQL function to perform search like this:

create or replace function search_posts(keyword text)
returns setof posts
as
$func$
select 
  * 
from 
  posts
where 
  to_tsvector(username || ' ' || title || ' ' || description) -- concat columns, but be sure to include a space to separate them!
  @@ to_tsquery(keyword);
$func$
language sql;

You can call this function like this:

const {data, error} = await supabase.rpc('search_posts', { keyword: '[YOUR_SEARCH_TERM_HERE]' })

You can read more about textSearch here

相关问题