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: