ugent-library / old-people-service

People service
Apache License 2.0
0 stars 0 forks source link

People suggest: elasticsearch really needed? #13

Closed nicolasfranck closed 1 year ago

nicolasfranck commented 1 year ago

Search in elasticsearch now done as follows:

So search is rather simpel, so could "easily" be converted to a sql statement:

SELECT * FROM person WHERE (first_name ILIKE 'nico%' OR last_name ILIKE 'nico%' OR full_name ILIKE 'nico%') AND (first_name ILIKE 'fra%' OR last_name ILIKE 'fra%' OR full_name ILIKE 'fra%')

Drawbacks:

netsensei commented 1 year ago

As an alternative to a simple LIKE statement, consider using PostgreSQL's full-text search capabilities via tsquery and tsvector:

nics commented 1 year ago

@nicolasfranck good intro https://supabase.com/blog/postgres-full-text-search-vs-the-rest

nicolasfranck commented 1 year ago

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');
nicolasfranck commented 1 year ago

Branch https://github.com/ugent-library/people/tree/ts available.

Important note:

nics commented 1 year ago

@nicolasfranck you can ask the question to the ent team?

nicolasfranck commented 1 year ago

@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

nicolasfranck commented 1 year ago

@nics a bit ugly, but I add those fields at startup: https://github.com/ugent-library/people/commit/60e49dd555766f03f4c3c9acbb40233e32980eb6

nicolasfranck commented 1 year ago

and now also with an index using GIN (not a fan though ;-) )

nicolasfranck commented 1 year ago

There are two ways to create the extra columns: