summaryrefslogtreecommitdiff
path: root/lib/VNWeb
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2021-11-15 16:23:28 +0100
committerYorhel <git@yorhel.nl>2021-11-15 16:23:28 +0100
commita57a5f30c131219f98f4b0ba4ba88d77bdcecc1b (patch)
tree0176fd3b396b11465c5d9a54a5125ced4a868dc9 /lib/VNWeb
parent9744d51a58b5d12abe3fcadd46debdf2dec6bb83 (diff)
Discussions::Search: Use Postgres websearch_to_tsquery() for better query parsing
Diffstat (limited to 'lib/VNWeb')
-rw-r--r--lib/VNWeb/Discussions/Search.pm14
1 files changed, 6 insertions, 8 deletions
diff --git a/lib/VNWeb/Discussions/Search.pm b/lib/VNWeb/Discussions/Search.pm
index 1925dc8a..802feacf 100644
--- a/lib/VNWeb/Discussions/Search.pm
+++ b/lib/VNWeb/Discussions/Search.pm
@@ -54,14 +54,12 @@ sub noresults_ {
sub posts_ {
my($filt) = @_;
- # Turn query into something suitable for to_tsquery()
- # TODO: Use Postgres 11 websearch_to_tsquery() instead.
- (my $ts = $filt->{bq}) =~ y{+|&:*()="';!?$%^\\[]{}<>~` }{ }s;
- $ts =~ s/ +/ /;
- $ts =~ s/^ //;
- $ts =~ s/ $//;
- $ts =~ s/ / & /g;
- $ts =~ s/(?:^| )-([^ ]+)/ !$1 /;
+ # Use websearch_to_tsquery() to convert the query string into a tsquery.
+ # Also match against an empty string to see if the query doesn't consist of only negative matches.
+ my $ts = tuwf->dbVali('
+ WITH q(q) AS (SELECT websearch_to_tsquery(', \$filt->{bq}, '))
+ SELECT CASE WHEN numnode(q) = 0 OR q @@ \'\' THEN NULL ELSE q END FROM q');
+ return noresults_ if !$ts;
# HACK: The bbcodes are stripped from the original messages when creating
# the headline, so they are guaranteed not to show up in the message. This