wcs_search_tokens: last part, search function using our tokens (#86527)
gitea/wcs/pipeline/head There was a failure building this commit
Details
gitea/wcs/pipeline/head There was a failure building this commit
Details
This commit is contained in:
parent
2a4459bfe4
commit
2c328847ee
21
wcs/sql.py
21
wcs/sql.py
|
@ -1727,10 +1727,23 @@ BEGIN
|
|||
END;
|
||||
$function$;""")
|
||||
|
||||
# Second part : insert and update triggers
|
||||
# Second part: insert and update triggers
|
||||
cur.execute("CREATE OR REPLACE TRIGGER wcs_all_forms_fts_trg_ins AFTER INSERT ON wcs_all_forms FOR EACH ROW WHEN (NEW.fts IS NOT NULL) EXECUTE PROCEDURE wcs_search_tokens_trigger_fn();")
|
||||
cur.execute("CREATE OR REPLACE TRIGGER wcs_all_forms_fts_trg_upd AFTER UPDATE OF fts ON wcs_all_forms FOR EACH ROW WHEN (NEW.fts IS NOT NULL) EXECUTE PROCEDURE wcs_search_tokens_trigger_fn();")
|
||||
|
||||
# And last: functions to use this brand new table
|
||||
cur.execute("CREATE OR REPLACE AGGREGATE tsquery_agg_or (tsquery) (sfunc=tsquery_or, stype=tsquery);")
|
||||
cur.execute("CREATE OR REPLACE AGGREGATE tsquery_agg_and (tsquery) (sfunc=tsquery_and, stype=tsquery);")
|
||||
cur.execute("""CREATE OR REPLACE FUNCTION public.wcs_tsquery(text)
|
||||
RETURNS tsquery
|
||||
LANGUAGE sql
|
||||
STABLE
|
||||
AS $function$
|
||||
with
|
||||
tokenized as (select unnest(regexp_split_to_array($1, '\s+')) w),
|
||||
super_tokenized as (select w, coalesce(tsquery_agg_or(plainto_tsquery(token) order by token <-> w desc), plainto_tsquery(w)) tokens from tokenized left join wcs_search_tokens on token % w group by w)
|
||||
select tsquery_agg_and(tokens) from super_tokenized;
|
||||
$function$;""")
|
||||
|
||||
def init_search_tokens_data(cur):
|
||||
cur.execute("INSERT INTO wcs_search_tokens SELECT unnest(tsvector_to_array(fts)) FROM wcs_all_forms ON CONFLICT(token) DO NOTHING;")
|
||||
|
@ -1820,7 +1833,7 @@ class SqlMixin:
|
|||
|
||||
sql_statement = (
|
||||
'''SELECT id FROM %s
|
||||
WHERE fts @@ plainto_tsquery(%%(value)s)'''
|
||||
WHERE fts @@ wcs_tsquery(%%(value)s)'''
|
||||
% cls._table_name
|
||||
)
|
||||
cur.execute(sql_statement, {'value': FtsMatch.get_fts_value(query)})
|
||||
|
@ -2286,7 +2299,7 @@ class SqlMixin:
|
|||
except IndexError:
|
||||
pass
|
||||
else:
|
||||
sql_statement += ' ORDER BY ts_rank(fts, plainto_tsquery(%%(c%s)s)) DESC' % id(fts.value)
|
||||
sql_statement += ' ORDER BY ts_rank(fts, wcs_tsquery(%%(c%s)s)) DESC' % id(fts.value)
|
||||
else:
|
||||
sql_statement += cls.get_order_by_clause(order_by)
|
||||
cur.execute(sql_statement, parameters)
|
||||
|
@ -4904,7 +4917,7 @@ class SearchableFormDef(SqlMixin):
|
|||
def search(cls, obj_type, string):
|
||||
_, cur = get_connection_and_cursor()
|
||||
cur.execute(
|
||||
'SELECT object_id FROM searchable_formdefs WHERE fts @@ plainto_tsquery(%s)',
|
||||
'SELECT object_id FROM searchable_formdefs WHERE fts @@ wcs_tsquery(%s)',
|
||||
(FtsMatch.get_fts_value(string),),
|
||||
)
|
||||
ids = [x[0] for x in cur.fetchall()]
|
||||
|
|
Loading…
Reference in New Issue