Skip to content

Commit

Permalink
fix: corrected MySQL fulltext search with relevance ranking, closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 23, 2023
1 parent 1bc9cf8 commit a051611
Showing 1 changed file with 9 additions and 53 deletions.
62 changes: 9 additions & 53 deletions phpmyfaq/src/phpMyFAQ/Search/Database/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

namespace phpMyFAQ\Search\Database;

use mysqli_result;
use phpMyFAQ\Configuration;
use phpMyFAQ\Search\Exception;
use phpMyFAQ\Search\SearchDatabase;
use stdClass;

/**
* Class Mysqli
Expand Down Expand Up @@ -54,8 +51,8 @@ public function search(string $searchTerm): mixed
$columns = $this->getResultColumns();

if ($this->relevanceSupport && $relevance) {
$columns .= $this->getMatchingColumnsAsResult($searchTerm);
$orderBy = 'ORDER BY ' . $this->getMatchingOrder() . ' DESC';
$columns .= ', ' . $this->setRelevanceRanking($searchTerm);
$orderBy = 'ORDER BY score DESC';
} else {
$orderBy = '';
}
Expand Down Expand Up @@ -118,58 +115,17 @@ public function search(string $searchTerm): mixed

return $this->resultSet;
}

return false;
}

/**
* Add the matching columns into the columns for the result set.
*
*
* Add the matching columns into the columns for the relevance ranking
*/
public function getMatchingColumnsAsResult(string $searchTerm): string
public function setRelevanceRanking(string $searchTerm): string
{
$resultColumns = '';

foreach ($this->matchingColumns as $matchColumn) {
$column = sprintf(
"MATCH (%s) AGAINST ('*%s*' IN NATURAL LANGUAGE MODE) AS relevance_%s",
$matchColumn,
$this->config->getDb()->escape($searchTerm),
substr(strstr($matchColumn, '.'), 1)
);

$resultColumns .= ', ' . $column;
}

return $resultColumns;
}

/**
* Returns the part of the SQL query with the order by.
*
* The order is calculate by weight depend on the search.relevance order
*/
public function getMatchingOrder(): string
{
$list = explode(',', (string) $this->config->get('search.relevance'));
$count = count($list);
$order = '';

foreach ($list as $field) {
$string = sprintf(
'(relevance_%s * %d)',
$field,
$count
);
if (empty($order)) {
$order .= $string;
} else {
$order .= ' + ' . $string;
}
--$count;
}

return '(' . $order . ')';
return sprintf(
"MATCH (%s) AGAINST ('%s' IN BOOLEAN MODE) as score",
$this->getMatchingColumns(),
$this->config->getDb()->escape($searchTerm)
);
}
}

0 comments on commit a051611

Please sign in to comment.