Browse Source

implement extended search mode support #9

main
ghost 2 years ago
parent
commit
0bd765064b
  1. 15
      README.md
  2. 6
      library/filter.php
  3. 7
      public/api.php
  4. 5
      public/search.php

15
README.md

@ -63,6 +63,7 @@ Could be enabled or disabled by `API_SEARCH_ENABLED` option @@ -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 @@ -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 @@ -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

6
library/filter.php

@ -55,9 +55,11 @@ class Filter { @@ -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);

7
public/api.php

@ -30,12 +30,13 @@ if (API_ENABLED) { @@ -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 = [];

5
public/search.php

@ -24,6 +24,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the @@ -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)) { @@ -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 {

Loading…
Cancel
Save