Skip to content
Merged
24 changes: 19 additions & 5 deletions phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
*/
class PdoPgsql extends SearchDatabase implements DatabaseInterface
{
/**
* List of relevance columns that were actually added to the SELECT clause.
*
* @var string[]
*/
private array $addedRelevanceColumns = [];

/**
* Constructor.
*/
Expand Down Expand Up @@ -102,6 +109,9 @@ public function getMatchingColumnsAsResult(): string
$weight[$columnName] = array_shift($weights);
}

// Reset the list of added columns
$this->addedRelevanceColumns = [];

foreach ($this->matchingColumns as $matchingColumn) {
$columnName = substr(strstr($matchingColumn, '.'), 1);

Expand All @@ -114,6 +124,7 @@ public function getMatchingColumnsAsResult(): string
);

$resultColumns .= ', ' . $column;
$this->addedRelevanceColumns[] = $columnName;
}
}

Expand All @@ -131,11 +142,14 @@ public function getMatchingOrder(): string
$order = '';

foreach ($list as $field) {
$string = sprintf('relevance_%s DESC', $field);
if ($order === '' || $order === '0') {
$order .= $string;
} else {
$order .= ', ' . $string;
// Only add to ORDER BY if this relevance column was actually added to SELECT
if (in_array($field, $this->addedRelevanceColumns)) {
$string = sprintf('relevance_%s DESC', $field);
if ($order === '' || $order === '0') {
$order .= $string;
} else {
$order .= ', ' . $string;
}
}
}

Expand Down
24 changes: 19 additions & 5 deletions phpmyfaq/src/phpMyFAQ/Search/Database/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
*/
class Pgsql extends SearchDatabase implements DatabaseInterface
{
/**
* List of relevance columns that were actually added to the SELECT clause.
*
* @var string[]
*/
private array $addedRelevanceColumns = [];

/**
* Constructor.
*/
Expand Down Expand Up @@ -102,6 +109,9 @@ public function getMatchingColumnsAsResult(): string
$weight[$columnName] = array_shift($weights);
}

// Reset the list of added columns
$this->addedRelevanceColumns = [];

foreach ($this->matchingColumns as $matchingColumn) {
$columnName = substr(strstr($matchingColumn, '.'), 1);

Expand All @@ -114,6 +124,7 @@ public function getMatchingColumnsAsResult(): string
);

$resultColumns .= ', ' . $column;
$this->addedRelevanceColumns[] = $columnName;
}
}

Expand All @@ -131,11 +142,14 @@ public function getMatchingOrder(): string
$order = '';

foreach ($list as $field) {
$string = sprintf('relevance_%s DESC', $field);
if ($order === '' || $order === '0') {
$order .= $string;
} else {
$order .= ', ' . $string;
// Only add to ORDER BY if this relevance column was actually added to SELECT
if (in_array($field, $this->addedRelevanceColumns)) {
$string = sprintf('relevance_%s DESC', $field);
if ($order === '' || $order === '0') {
$order .= $string;
} else {
$order .= ', ' . $string;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Search/SearchResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function getScore(stdClass $object): float
}

if (isset($object->relevance_content)) {
$score += $object->relevance_thema;
$score += $object->relevance_content;
}

if (isset($object->relevance_keywords)) {
Expand Down