From fcee7f62efdd7583008479ca83b4263436a3764a Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 23 Apr 2023 09:29:24 +0300 Subject: [PATCH] fix max_matches error --- library/sphinxql.php | 10 ++++++++-- public/api.php | 2 +- public/search.php | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/library/sphinxql.php b/library/sphinxql.php index 1780b8b..5ad355f 100644 --- a/library/sphinxql.php +++ b/library/sphinxql.php @@ -11,9 +11,15 @@ class SphinxQL { $this->_sphinx->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); } - public function searchHostPages(string $keyword, int $start, int $limit) { + public function searchHostPages(string $keyword, int $start, int $limit, int $maxMatches) { - $query = $this->_sphinx->prepare('SELECT * FROM `hostPage` WHERE MATCH(?) LIMIT ' . (int) $start . ',' . (int) $limit); + $query = $this->_sphinx->prepare('SELECT * FROM `hostPage` + + WHERE MATCH(?) + + LIMIT ' . (int) ($start > $maxMatches ? ($maxMatches > 0 ? $maxMatches - 1 : 0) : $start) . ',' . (int) $limit . ' + + OPTION `max_matches`=' . (int) ($maxMatches > 1 ? $maxMatches : 1)); $query->execute([$keyword]); diff --git a/public/api.php b/public/api.php index eb469bc..22432b1 100644 --- a/public/api.php +++ b/public/api.php @@ -29,8 +29,8 @@ if (API_ENABLED) { $page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1; // Make search request - $sphinxResults = $sphinx->searchHostPages('"' . $query . '"', $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT); $sphinxResultsTotal = $sphinx->searchHostPagesTotal('"' . $query . '"'); + $sphinxResults = $sphinx->searchHostPages('"' . $query . '"', $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT, $sphinxResultsTotal); // Generate results $dbResults = []; diff --git a/public/search.php b/public/search.php index 60a8530..9aa2026 100644 --- a/public/search.php +++ b/public/search.php @@ -106,13 +106,13 @@ if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) { // Search request if (!empty($q)) { - $results = $sphinx->searchHostPages('"' . $q . '"', $p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT - WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT); $resultsTotal = $sphinx->searchHostPagesTotal('"' . $q . '"'); + $results = $sphinx->searchHostPages('"' . $q . '"', $p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT - WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, $resultsTotal); } else { - $results = []; $resultsTotal = 0; + $results = []; } ?>