Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

perf(search): weight names in fts to rank name matches cheaply

fzaninotto965719fe9a0f
diff --git a/supabase/migrations/20261029131652_global_search_rank_title_matches.sql b/supabase/migrations/20261029133628_global_search_rank_name_matches.sql
similarity index 54%
rename from supabase/migrations/20261029131652_global_search_rank_title_matches.sql
rename to supabase/migrations/20261029133628_global_search_rank_name_matches.sql
index 79883604..70b88b21 100644
--- a/supabase/migrations/20261029131652_global_search_rank_title_matches.sql
+++ b/supabase/migrations/20261029133628_global_search_rank_name_matches.sql
@@ -1,3 +1,52 @@
+-- Weight the name A in the fts documents of companies, contacts and deals, so
+-- global_search() can rank name matches first without slowing common words.
+-- db diff turns a generated column change into an invalid "set default": the
+-- fts columns are dropped and rebuilt instead, with their indexes and
+-- statistics target.
+alter table "public"."companies" drop column "fts";
+
+alter table "public"."contacts" drop column "fts";
+
+alter table "public"."deals" drop column "fts";
+
+alter table public.companies add column fts tsvector generated always as (
+ setweight(public.search_document(name), 'A')
+ || public.search_document(sector, description, website::text, phone_number, zipcode, city, state_abbr)
+) stored;
+
+alter table public.contacts add column fts tsvector generated always as (
+ setweight(public.search_document(first_name, last_name), 'A')
+ || public.search_document(
+ title,
+ background,
+ jsonb_path_query_array(email_jsonb, '$[*]."email"')::text,
+ jsonb_path_query_array(phone_jsonb, '$[*]."number"')::text
+ )
+) stored;
+
+alter table public.deals add column fts tsvector generated always as (
+ setweight(public.search_document(name), 'A')
+ || public.search_document(variadic array[description] || categories)
+) stored;
+
+create index companies_fts_idx on public.companies using gin (fts);
+
+create index contacts_fts_idx on public.contacts using gin (fts);
+
+create index deals_fts_idx on public.deals using gin (fts) where archived_at is null;
+
+alter table public.contacts alter column fts set statistics 10000;
+
+set check_function_bodies = off;
+
+CREATE OR REPLACE FUNCTION public.search_name_query(query text)
+ RETURNS tsquery
+ LANGUAGE sql
+ IMMUTABLE PARALLEL SAFE STRICT
+ SET search_path TO ''
+AS $function$ select replace(public.search_query(query)::text, ':*', ':*A')::tsquery $function$
+;
+
CREATE OR REPLACE FUNCTION public.global_search(query text, resources text[] DEFAULT NULL::text[], max_results integer DEFAULT 50)
RETURNS TABLE(resource text, record_id bigint, title text, subtitle text, contact_id bigint, deal_id bigint, date timestamp with time zone, content text)
LANGUAGE plpgsql
@@ -10,10 +59,10 @@ declare
'companies', $q$
select 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at,
concat_ws(' · ', c.sector, c.description, c.website::text, c.phone_number, c.zipcode, c.city, c.state_abbr),
- public.search_document(c.name) @@ %1$L::tsquery as is_title_match
+ %4$s
from public.companies c
- where c.fts @@ %1$L::tsquery
- order by is_title_match desc, c.created_at desc
+ where %3$s
+ order by c.created_at desc
limit %2$s $q$,
'contacts', $q$
select 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),
@@ -21,19 +70,19 @@ declare
concat_ws(' · ', co.title, co.background,
(select string_agg(e ->> 'email', ' ') from jsonb_array_elements(co.email_jsonb) e),
(select string_agg(p ->> 'number', ' ') from jsonb_array_elements(co.phone_jsonb) p)),
- public.search_document(co.first_name, co.last_name) @@ %1$L::tsquery as is_title_match
+ %4$s
from public.contacts co
- where co.fts @@ %1$L::tsquery
- order by is_title_match desc, co.first_seen desc nulls last
+ where %3$s
+ order by co.first_seen desc nulls last
limit %2$s $q$,
'deals', $q$
select 'deals', d.id, d.name,
(select dc.name from public.companies dc where dc.id = d.company_id), null::bigint, d.id, d.created_at,
concat_ws(' · ', variadic d.categories || d.description),
- public.search_document(d.name) @@ %1$L::tsquery as is_title_match
+ %4$s
from public.deals d
- where d.archived_at is null and d.fts @@ %1$L::tsquery
- order by is_title_match desc, d.created_at desc
+ where d.archived_at is null and %3$s
+ order by d.created_at desc
limit %2$s $q$,
'tasks', $q$
select 'tasks', t.id, left(t.text, 200),
@@ -62,14 +111,26 @@ declare
order by dn.date desc nulls last
limit %2$s $q$
);
+ name_tsq tsquery := public.search_name_query(query);
row_limit constant integer := least(coalesce(max_results, 50), 200);
+ columns constant text := 'resource, record_id, title, subtitle, contact_id, deal_id, date, content, is_title_match';
branches text[];
begin
if tsq is null or row_limit < 1 then
return;
end if;
- select array_agg(format('(%s)', format(template, tsq::text, row_limit)))
+ select array_agg(case
+ -- %3$s is the search condition, %4$s whether it is a name match
+ when position('%3$s' in template) > 0 then format(
+ '(select * from ((%s) union all (%s)) b(%s) order by is_title_match desc, date desc nulls last limit %s)',
+ format(template, tsq::text, row_limit, format('fts @@ %L::tsquery', name_tsq::text), 'true'),
+ format(template, tsq::text, row_limit,
+ format('fts @@ %L::tsquery and not fts @@ %L::tsquery', tsq::text, name_tsq::text), 'false'),
+ columns,
+ row_limit)
+ else format('(%s)', format(template, tsq::text, row_limit))
+ end)
into branches
from jsonb_each_text(branch_templates) as b(name, template)
where resources is null or b.name = any(resources);
@@ -80,9 +141,10 @@ begin
return query execute format(
'select resource, record_id, title, subtitle, contact_id, deal_id, date, content
- from (%s) results(resource, record_id, title, subtitle, contact_id, deal_id, date, content, is_title_match)
+ from (%s) results(%s)
order by is_title_match desc, date desc nulls last',
- array_to_string(branches, ' union all ')
+ array_to_string(branches, ' union all '),
+ columns
);
end
$function$
diff --git a/supabase/schemas/08_search.sql b/supabase/schemas/08_search.sql
index 7e85ea14..312846dc 100644
--- a/supabase/schemas/08_search.sql
+++ b/supabase/schemas/08_search.sql
@@ -38,19 +38,26 @@ CREATE OR REPLACE FUNCTION "public"."search_query"("query" "text") RETURNS "tsqu
where word <> ''
$$;
+-- Same as search_query(), restricted to the words weighted A in a document:
+-- the record's name for companies, contacts and deals.
+CREATE OR REPLACE FUNCTION "public"."search_name_query"("query" "text") RETURNS "tsquery"
+ LANGUAGE "sql" IMMUTABLE STRICT PARALLEL SAFE
+ SET "search_path" TO ''
+ AS $$ select replace(public.search_query(query)::text, ':*', ':*A')::tsquery $$;
+
CREATE OR REPLACE FUNCTION "public"."search_attachment_titles"("attachments" "jsonb"[]) RETURNS "text"
LANGUAGE "sql" IMMUTABLE PARALLEL SAFE
SET "search_path" TO ''
AS $$ select string_agg(attachment ->> 'title', ' ') from unnest(attachments) as attachment $$;
alter table public.companies add column fts tsvector generated always as (
- public.search_document(name, sector, description, website::text, phone_number, zipcode, city, state_abbr)
+ setweight(public.search_document(name), 'A')
+ || public.search_document(sector, description, website::text, phone_number, zipcode, city, state_abbr)
) stored;
alter table public.contacts add column fts tsvector generated always as (
- public.search_document(
- first_name,
- last_name,
+ setweight(public.search_document(first_name, last_name), 'A')
+ || public.search_document(
title,
background,
jsonb_path_query_array(email_jsonb, '$[*]."email"')::text,
@@ -59,7 +66,8 @@ alter table public.contacts add column fts tsvector generated always as (
) stored;
alter table public.deals add column fts tsvector generated always as (
- public.search_document(variadic array[name, description] || categories)
+ setweight(public.search_document(name), 'A')
+ || public.search_document(variadic array[description] || categories)
) stored;
alter table public.tasks add column fts tsvector generated always as (
@@ -98,7 +106,9 @@ alter table public.deal_notes alter column fts set statistics 10000;
-- Records whose name matches every query word (a contact named John Doe)
-- rank before those matching elsewhere (a contact whose background mentions
--- John Doe), then newest first.
+-- John Doe), then newest first. Companies, contacts and deals run their branch
+-- twice, once per kind of match, so each half can still walk the date index:
+-- testing the name on every match instead is what makes a common word slow.
-- Each branch takes its own newest max_results matches (at most 200), so the
-- union never holds more than 6 * max_results rows, and the lookups that build
-- display labels only run on those. The cap is per resource, not overall: an
@@ -117,10 +127,10 @@ declare
'companies', $q$
select 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at,
concat_ws(' · ', c.sector, c.description, c.website::text, c.phone_number, c.zipcode, c.city, c.state_abbr),
- public.search_document(c.name) @@ %1$L::tsquery as is_title_match
+ %4$s
from public.companies c
- where c.fts @@ %1$L::tsquery
- order by is_title_match desc, c.created_at desc
+ where %3$s
+ order by c.created_at desc
limit %2$s $q$,
'contacts', $q$
select 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),
@@ -128,19 +138,19 @@ declare
concat_ws(' · ', co.title, co.background,
(select string_agg(e ->> 'email', ' ') from jsonb_array_elements(co.email_jsonb) e),
(select string_agg(p ->> 'number', ' ') from jsonb_array_elements(co.phone_jsonb) p)),
- public.search_document(co.first_name, co.last_name) @@ %1$L::tsquery as is_title_match
+ %4$s
from public.contacts co
- where co.fts @@ %1$L::tsquery
- order by is_title_match desc, co.first_seen desc nulls last
+ where %3$s
+ order by co.first_seen desc nulls last
limit %2$s $q$,
'deals', $q$
select 'deals', d.id, d.name,
(select dc.name from public.companies dc where dc.id = d.company_id), null::bigint, d.id, d.created_at,
concat_ws(' · ', variadic d.categories || d.description),
- public.search_document(d.name) @@ %1$L::tsquery as is_title_match
+ %4$s
from public.deals d
- where d.archived_at is null and d.fts @@ %1$L::tsquery
- order by is_title_match desc, d.created_at desc
+ where d.archived_at is null and %3$s
+ order by d.created_at desc
limit %2$s $q$,
'tasks', $q$
select 'tasks', t.id, left(t.text, 200),
@@ -169,14 +179,26 @@ declare
order by dn.date desc nulls last
limit %2$s $q$
);
+ name_tsq tsquery := public.search_name_query(query);
row_limit constant integer := least(coalesce(max_results, 50), 200);
+ columns constant text := 'resource, record_id, title, subtitle, contact_id, deal_id, date, content, is_title_match';
branches text[];
begin
if tsq is null or row_limit < 1 then
return;
end if;
- select array_agg(format('(%s)', format(template, tsq::text, row_limit)))
+ select array_agg(case
+ -- %3$s is the search condition, %4$s whether it is a name match
+ when position('%3$s' in template) > 0 then format(
+ '(select * from ((%s) union all (%s)) b(%s) order by is_title_match desc, date desc nulls last limit %s)',
+ format(template, tsq::text, row_limit, format('fts @@ %L::tsquery', name_tsq::text), 'true'),
+ format(template, tsq::text, row_limit,
+ format('fts @@ %L::tsquery and not fts @@ %L::tsquery', tsq::text, name_tsq::text), 'false'),
+ columns,
+ row_limit)
+ else format('(%s)', format(template, tsq::text, row_limit))
+ end)
into branches
from jsonb_each_text(branch_templates) as b(name, template)
where resources is null or b.name = any(resources);
@@ -187,9 +209,10 @@ begin
return query execute format(
'select resource, record_id, title, subtitle, contact_id, deal_id, date, content
- from (%s) results(resource, record_id, title, subtitle, contact_id, deal_id, date, content, is_title_match)
+ from (%s) results(%s)
order by is_title_match desc, date desc nulls last',
- array_to_string(branches, ' union all ')
+ array_to_string(branches, ' union all '),
+ columns
);
end
$_$;