From 0bd765064bb7e46a7d2d42c3d2d6e39d555ed3e5 Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 1 May 2023 20:09:28 +0300 Subject: [PATCH] implement extended search mode support #9 --- README.md | 15 ++++++++++++--- library/filter.php | 6 ++++-- public/api.php | 7 ++++--- public/search.php | 5 +++-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a0322dd..1da6253 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Could be enabled or disabled by `API_SEARCH_ENABLED` option GET action=search - required GET query={string} - optional, search request, empty if not provided GET page={int} - optional, search results page, 1 if not provided +GET mode=SphinxQL - optional, enable extended SphinxQL syntax ``` ##### Hosts distribution @@ -91,9 +92,7 @@ GET action=manifest - required #### Search textual filtering -https://sphinxsearch.com/docs/current.html#extended-syntax - -##### Supported constructions +##### Default constructions ``` operator OR: @@ -126,6 +125,16 @@ boosted^1.234 boostedfieldend$^1.234 ``` +##### Extended syntax + +https://sphinxsearch.com/docs/current.html#extended-syntax + +Could be enabled with following attributes + +``` +GET m=SphinxQL +``` + #### Roadmap / ideas * [x] Web pages full text ranking search diff --git a/library/filter.php b/library/filter.php index 897eff1..275b7b2 100644 --- a/library/filter.php +++ b/library/filter.php @@ -55,9 +55,11 @@ class Filter { return $data; } - static public function sphinxSearchQuery(string $query) { + static public function searchQuery(string $query, string $mode = 'default') { - $query = str_replace(['\\', '/', '~', '@', '!', '"', '(', ')'], ['\\\\', '\/', '\~', '\@', '\!', '\"', '\(', '\)'], $query); + if ($mode == 'default') { + $query = str_replace(['\\', '/', '~', '@', '!', '"', '(', ')'], ['\\\\', '\/', '\~', '\@', '\!', '\"', '\(', '\)'], $query); + } $query = trim($query); diff --git a/public/api.php b/public/api.php index 11c6548..6f0ef74 100644 --- a/public/api.php +++ b/public/api.php @@ -30,12 +30,13 @@ if (API_ENABLED) { // Filter request data + $mode = !empty($_GET['mode']) ? Filter::url($_GET['mode']) : 'default'; $query = !empty($_GET['query']) ? Filter::url($_GET['query']) : ''; - $page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1; + $page = !empty($_GET['page']) ? (int) $_GET['page'] : 1; // Make search request - $sphinxResultsTotal = $sphinx->searchHostPagesTotal(Filter::sphinxSearchQuery($query)); - $sphinxResults = $sphinx->searchHostPages(Filter::sphinxSearchQuery($query), $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT, $sphinxResultsTotal); + $sphinxResultsTotal = $sphinx->searchHostPagesTotal(Filter::searchQuery($query, $mode)); + $sphinxResults = $sphinx->searchHostPages(Filter::searchQuery($query, $mode), $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 dbf1f45..2c20bad 100644 --- a/public/search.php +++ b/public/search.php @@ -24,6 +24,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the ]); // Filter request data +$m = !empty($_GET['m']) ? Filter::url($_GET['m']) : 'default'; $q = !empty($_GET['q']) ? Filter::url($_GET['q']) : ''; $p = !empty($_GET['p']) ? (int) $_GET['p'] : 1; @@ -106,8 +107,8 @@ if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) { // Search request if (!empty($q)) { - $resultsTotal = $sphinx->searchHostPagesTotal(Filter::sphinxSearchQuery($q)); - $results = $sphinx->searchHostPages(Filter::sphinxSearchQuery($q), $p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT - WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, $resultsTotal); + $resultsTotal = $sphinx->searchHostPagesTotal(Filter::searchQuery($q, $m)); + $results = $sphinx->searchHostPages(Filter::searchQuery($q, $m), $p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT - WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, $resultsTotal); } else {