From 755a1ff60dbc326a1ae5f95e70bbcf192e972785 Mon Sep 17 00:00:00 2001 From: Aevann Date: Wed, 28 Feb 2024 19:58:24 +0200 Subject: [PATCH] fix exact searching --- files/routes/search.py | 8 ++++++-- migrations/20240228-fix-exact-searching.sql | 2 ++ schema.sql | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 migrations/20240228-fix-exact-searching.sql diff --git a/files/routes/search.py b/files/routes/search.py index faaf37653..25de65918 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -229,9 +229,11 @@ def searchcomments(v): if 'q' in criteria: text = criteria['full_text'] + if '"' in text: regconfig = "simple" + else: regconfig = "english" comments = comments.filter( Comment.body_ts.bool_op("@@")( - func.websearch_to_tsquery("english", text) + func.websearch_to_tsquery(regconfig, text) ) ) @@ -328,9 +330,11 @@ def searchmessages(v): if 'q' in criteria: text = criteria['full_text'] + if '"' in text: regconfig = "simple" + else: regconfig = "english" comments = comments.filter( Comment.body_ts.bool_op("@@")( - func.websearch_to_tsquery("english", text) + func.websearch_to_tsquery(regconfig, text) ) ) diff --git a/migrations/20240228-fix-exact-searching.sql b/migrations/20240228-fix-exact-searching.sql new file mode 100644 index 000000000..4ea79040d --- /dev/null +++ b/migrations/20240228-fix-exact-searching.sql @@ -0,0 +1,2 @@ +alter table comments drop column body_ts; +alter table comments add column body_ts tsvector GENERATED ALWAYS AS (to_tsvector('simple'::regconfig, (body)::text)) STORED; diff --git a/schema.sql b/schema.sql index 876d75091..28e4fc80f 100644 --- a/schema.sql +++ b/schema.sql @@ -459,7 +459,7 @@ CREATE TABLE public.comments ( queened boolean NOT NULL, sharpened boolean NOT NULL, num_of_pinned_children integer NOT NULL, - body_ts tsvector GENERATED ALWAYS AS (to_tsvector('english'::regconfig, (body)::text)) STORED, + body_ts tsvector GENERATED ALWAYS AS (to_tsvector('simple'::regconfig, (body)::text)) STORED, distinguished boolean NOT NULL ); @@ -3074,4 +3074,3 @@ ALTER TABLE ONLY public.comments -- -- PostgreSQL database dump complete -- -