Closed nicolasfranck closed 1 year ago
As an alternative to a simple LIKE
statement, consider using PostgreSQL's full-text search capabilities via tsquery
and tsvector
:
@nicolasfranck good intro https://supabase.com/blog/postgres-full-text-search-vs-the-rest
add column with type simple
to disable language specific stemming (e.g. Nicolas
becomes nicola
in English stemming):
alter table person add column ts tsvector generated always as (to_tsvector('simple',full_name)) stored;
alter table organization add column ts tsvector generated always as (to_tsvector('simple', jsonb_path_query_array(other_id, '$[*].id'))) stored;
with prepared statements and placeholders (safe against sql insertion) and prefixing:
prepare se(text,text) as select full_name from person where ts @@ to_tsquery('simple',$1 || ':* & ' || $2 || ':*');
execute se('nicol', 'fra');
Branch https://github.com/ugent-library/people/tree/ts available.
Important note:
nico fra
into to_tsquery('simple', 'nico:* & fra:*')
with protection against sql injection@nicolasfranck you can ask the question to the ent team?
@nics https://github.com/ent/ent/issues/3168
Maybe I'll add this as a hook, or on startup. You can always do a raw Exec
@nics a bit ugly, but I add those fields at startup: https://github.com/ugent-library/people/commit/60e49dd555766f03f4c3c9acbb40233e32980eb6
and now also with an index using GIN (not a fan though ;-) )
There are two ways to create the extra columns:
Search in elasticsearch now done as follows:
first_name
,last_name
andfull_name
are copied into a global fieldall
at index timenico fra
becomesall:nico* AND all:fra*
So search is rather simpel, so could "easily" be converted to a sql statement:
Drawbacks:
preferred_first_name