Browse Source

update Filter::searchQuery method

main
ghost 1 year ago
parent
commit
3d6bc54b66
  1. 58
      library/filter.php

58
library/filter.php

@ -79,34 +79,52 @@ class Filter {
static public function searchQuery(string $query, string $mode = 'default') { static public function searchQuery(string $query, string $mode = 'default') {
// Prepare user-friendly search request (default mode)
// https://sphinxsearch.com/docs/current.html#extended-syntax
if ($mode == 'default') { if ($mode == 'default') {
// Remove single char words // Remove extra separators
$words = []; $query = preg_replace('/[\s]+/', ' ', $query);
foreach ((array) explode(' ', $query) as $word) {
if (mb_strlen($word) > 1) { $query = trim($query);
$words[] = $word;
}
}
if ($words) {
$query = implode(' ', $words);
}
// Remove SphinxQL special chars // Remove single char words
$query = str_replace(['\\', '/', '~', '@', '!', '"', '(', ')'], ['\\\\', '\/', '\~', '\@', '\!', '\"', '\(', '\)'], $query); $words = [];
foreach ((array) explode(' ', $query) as $word) {
// Replace query separators to the MAYBE operator if (mb_strlen($word) > 1) {
$query = str_ireplace(['MAYBE'], ['__MAYBE__'], $query); $words[] = $word;
$query = preg_replace('/[\W\s]+/ui', '__SEPARATOR__', $query); }
$query = trim($query, '__SEPARATOR__'); }
$query = str_ireplace(['__SEPARATOR__', '__MAYBE__'], [' MAYBE ', ' \MAYBE '], $query);
if ($words) {
$query = implode(' ', $words);
}
// Quote reserved keyword operators
$operators = [
'MAYBE',
'AND',
'OR',
'NOT',
'SENTENCE',
'NEAR',
'ZONE',
'ZONESPAN',
'PARAGRAPH',
'\\', '/', '~', '@', '!', '"', '(', ')', '|', '?', '%', '-', '>', '<', ':', ';'
];
foreach ($operators as $operator) {
$query = str_ireplace($operator, '\\' . $operator, $query);
}
} }
$query = trim($query); // Apply query semantics
$query = str_replace(' ', ' | ', $query);
return $query; return trim($query);
} }
static public function plural(int $number, array $texts) { static public function plural(int $number, array $texts) {

Loading…
Cancel
Save