Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions src/Plugin/Fuzzy/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class Payload extends BasePayload {
const MAX_BOOST = 50;
const DECREASE_FACTOR = 1.44;
const ESCAPE_CHARS = '()[]<!|*/-~^$@';
const MATCH_REG_PATTERN = '/MATCH\s*\(\'(.*?)\'(\s*,\s*\w*?)?\s*\)/ius';

/** @var string */
public string $path;
Expand Down Expand Up @@ -108,8 +109,19 @@ protected static function fromSqlRequest(Request $request): static {
$tableName = $matches[1] ?? '';

// Check that we have match
if (!preg_match('/match\s*\(\'(.*?)\'\)/ius', $query, $matches)) {
throw QueryParseError::create("The 'fuzzy' option requires a full-text query");
if (!preg_match(static::MATCH_REG_PATTERN, $query, $matches)) {
throw QueryParseError::create(
"The 'fuzzy' option requires a full-text query. " .
"Use: MATCH('search query') or MATCH('search query', table_name)"
);
}

$searchTableName = $matches[2] ?? null;
if (isset($searchTableName) && trim($searchTableName) === ',') {
throw QueryParseError::create(
'MATCH() clause has a trailing comma but table name is not defined. ' .
"Did you mean to specify a table? Eample: MATCH('search query', table_name)"
);
}

// I did not figure out how to make with regxp case OPTION fuzzy=1 so do this way
Expand Down Expand Up @@ -206,19 +218,20 @@ public function getQueriesRequest(callable $fn): string {
public function getQueriesSQLRequest(callable $fn): string {
/** @var string */
$payload = $this->payload;
preg_match('/MATCH\s*\(\'(.*?)\'\)/ius', $payload, $matches);
preg_match(static::MATCH_REG_PATTERN, $payload, $matches);
$searchValue = $matches[1] ?? '';
$searchTableName = $matches[2] ?? '';
$template = (string)preg_replace(
[
'/MATCH\s*\(\'(.*?)\'\)/ius',
static::MATCH_REG_PATTERN,
'/(fuzzy|distance|preserve)\s*=\s*\d+[,\s]*/ius',
'/(layouts)\s*=\s*\'([a-zA-Z, ]*)\'[,\s]*/ius',
'/option,(?!.*option|.*from)/ius',
'/\soption(?!.*option|.*from)/ius',
'/\s*,\s*facet\s+/ius',
],
[
'MATCH(\'%s\')',
'MATCH(\'%s\'%s)',
'',
'',
'option ',
Expand All @@ -243,7 +256,7 @@ public function getQueriesSQLRequest(callable $fn): string {
$match = $searchValue;
}
Buddy::debug("Fuzzy: match: $match");
$queries = [sprintf($template, $match), ...$this->queries];
$queries = [sprintf($template, $match, $searchTableName), ...$this->queries];
return implode(';', $queries);
}

Expand Down
Loading