diff --git a/phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php b/phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php index e12e73e124..61d5885242 100644 --- a/phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php +++ b/phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php @@ -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. */ @@ -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); @@ -114,6 +124,7 @@ public function getMatchingColumnsAsResult(): string ); $resultColumns .= ', ' . $column; + $this->addedRelevanceColumns[] = $columnName; } } @@ -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; + } } } diff --git a/phpmyfaq/src/phpMyFAQ/Search/Database/Pgsql.php b/phpmyfaq/src/phpMyFAQ/Search/Database/Pgsql.php index 3590ea4564..d4c659432c 100644 --- a/phpmyfaq/src/phpMyFAQ/Search/Database/Pgsql.php +++ b/phpmyfaq/src/phpMyFAQ/Search/Database/Pgsql.php @@ -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. */ @@ -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); @@ -114,6 +124,7 @@ public function getMatchingColumnsAsResult(): string ); $resultColumns .= ', ' . $column; + $this->addedRelevanceColumns[] = $columnName; } } @@ -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; + } } } diff --git a/phpmyfaq/src/phpMyFAQ/Search/SearchResultSet.php b/phpmyfaq/src/phpMyFAQ/Search/SearchResultSet.php index 4380c0188e..fbd9c463ef 100644 --- a/phpmyfaq/src/phpMyFAQ/Search/SearchResultSet.php +++ b/phpmyfaq/src/phpMyFAQ/Search/SearchResultSet.php @@ -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)) {