From 36d4aed1f58671914e19577af6736681175cc5ef Mon Sep 17 00:00:00 2001 From: Aevann Date: Mon, 15 May 2023 01:52:15 +0300 Subject: [PATCH] restore exact searching for posts --- files/routes/search.py | 10 +++++++++- files/templates/search.html | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/files/routes/search.py b/files/routes/search.py index 8cfc31944..c6073a82d 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -19,6 +19,7 @@ valid_params = [ 'post', 'before', 'after', + 'exact', 'title', 'sentto', search_operator_hole, @@ -91,7 +92,14 @@ def searchposts(v:User): ), 403 posts = posts.filter(Submission.author_id == author.id) - if 'q' in criteria: + if 'exact' in criteria and 'full_text' in criteria: + regex_str = '[[:<:]]'+criteria['full_text']+'[[:>:]]' # https://docs.oracle.com/cd/E17952_01/mysql-5.5-en/regexp.html "word boundaries" + if 'title' in criteria: + words = [Submission.title.regexp_match(regex_str)] + else: + words = [or_(Submission.title.regexp_match(regex_str), Submission.body.regexp_match(regex_str))] + posts = posts.filter(*words) + elif 'q' in criteria: if('title' in criteria): words = [or_(Submission.title.ilike('%'+x+'%')) \ for x in criteria['q']] diff --git a/files/templates/search.html b/files/templates/search.html index 7558000c7..89c85ca32 100644 --- a/files/templates/search.html +++ b/files/templates/search.html @@ -78,6 +78,10 @@
Post Title Only:
+
+
Exact Match Only:
+ +
{% endif %} {% endif %}