Browse Source

FTS using AND matching

master
René Wagner 4 months ago
parent
commit
98ab6ca1ab
  1. 4
      faqs.pl
  2. 7
      search.pl

4
faqs.pl

@ -63,12 +63,12 @@ sub faqs
my @tags = $dbh->selectall_array("SELECT id, name FROM tags t LEFT JOIN tags_questions tq ON tq.t_id = t.id WHERE tq.q_id = @$_[0];");
if ( scalar @tags ) {
push @return, ('', 'Tags:');
push @return, ('', '### Tags');
foreach (@tags) {
push @return, sprintf("=> ./faqs.pl?tag=%d %s", @$_[0], @$_[1]);
}
}
push @return, ('', @$_[2], '');
push @return, ('', '### Answer', @$_[2], '');
}
}
$dbh->disconnect();

7
search.pl

@ -24,7 +24,7 @@ if (!defined($ENV{'SERVER_PROTOCOL'}) || $ENV{'SERVER_PROTOCOL'} ne 'GEMINI') {
my $query = lc(uri_unescape($ENV{'QUERY_STRING'}));
if ($query eq '' || $query !~ /^[0-9a-z\s]*$/i)
{
write_response('INPUT', 'Search faqs for the following terms (separate by blank)', undef);
write_response('INPUT', 'Search faqs for the following terms (separate by blank, all terms must match)', undef);
}
my @body = ();
@ -42,8 +42,9 @@ sub search
my @result = ();
my $dbh = DBI->connect($CONF{'dsn'}, '', '', { RaiseError => 1 }) or die $DBI::errstr;
my @matchingtags = $dbh->selectall_array("SELECT DISTINCT t.*, count(tq.q_id) AS count FROM fts_data INNER JOIN tags t ON t.id = fts_data.t_id LEFT JOIN tags_questions tq ON tq.t_id = t.id WHERE fts_data MATCH '\"text\": \"$term\"' GROUP BY t.id ORDER BY rank");
my @matchingfaqs = $dbh->selectall_array("SELECT DISTINCT q_id, q.question FROM fts_data INNER JOIN questions q ON q.id = q_id WHERE fts_data MATCH '\"text\": \"$term\"' ORDER BY rank;");
my $fts_search = join(' AND ', split(/ /, $term));
my @matchingtags = $dbh->selectall_array("SELECT DISTINCT t.*, count(tq.q_id) AS count FROM fts_data INNER JOIN tags t ON t.id = fts_data.t_id LEFT JOIN tags_questions tq ON tq.t_id = t.id WHERE fts_data MATCH '$fts_search' GROUP BY t.id ORDER BY rank");
my @matchingfaqs = $dbh->selectall_array("SELECT DISTINCT q_id, q.question FROM fts_data INNER JOIN questions q ON q.id = q_id WHERE fts_data MATCH '$fts_search' ORDER BY rank;");
$dbh->disconnect();
if ( !scalar @matchingtags && !scalar @matchingfaqs) {

Loading…
Cancel
Save