From d7a63f8cbdc00eb50d7f5e65cbef02356285ee03 Mon Sep 17 00:00:00 2001 From: Aevann Date: Wed, 13 Nov 2024 15:10:34 +0200 Subject: [PATCH] make post search work exactly the same as comment search to - stop confusing ppl - future-proof for when posts eventually get too many to search --- files/classes/post.py | 4 ++++ files/routes/search.py | 15 +++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/files/classes/post.py b/files/classes/post.py index 5e9994aa5..5bfce9834 100644 --- a/files/classes/post.py +++ b/files/classes/post.py @@ -69,6 +69,10 @@ class Post(Base): sharpened = Column(Boolean, default=False) ping_cost = Column(Integer, default=0) bump_utc = Column(Integer) + title_ts = Column(TSVECTOR(), server_default=FetchedValue()) + body_ts = Column(TSVECTOR(), server_default=FetchedValue()) + url_ts = Column(TSVECTOR(), server_default=FetchedValue()) + embed_ts = Column(TSVECTOR(), server_default=FetchedValue()) if FEATURES['NSFW_MARKING']: nsfw = Column(Boolean, default=False) diff --git a/files/routes/search.py b/files/routes/search.py index 709e0039c..4083d92fa 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -100,17 +100,16 @@ def searchposts(v): posts = posts.filter(Post.author_id == author.id) if 'q' in criteria: - params = [Post.title] + text = criteria['full_text'] + + params = [Post.title_ts] if 'title_only' not in criteria: - params += [Post.body, Post.url, Post.embed] + params += [Post.body_ts, Post.url_ts, Post.embed_ts] words = [] - for x in criteria['q']: - for param in params: - if x.startswith('"') and x.endswith('"'): - words.append(param.regexp_match(f'[[:<:]]{x[1:-1]}[[:>:]]')) - else: - words.append(param.ilike(f'%{x}%')) + for param in params: + words.append(param.bool_op("@@")(func.websearch_to_tsquery("simple", text))) + posts = posts.filter(or_(*words)) if 'nsfw' in criteria: