optimize cleanup search query shop api
This commit is contained in:
@@ -88,6 +88,7 @@ final readonly class CommerceQueryParser
|
||||
);
|
||||
$value = preg_replace($this->config->getPromptSanitizePattern(), ' ', $value) ?? $value;
|
||||
$value = preg_replace($this->config->getWhitespaceCollapsePattern(), ' ', $value) ?? $value;
|
||||
$value = $this->applySearchTokenCorrections($value);
|
||||
|
||||
return trim($value);
|
||||
}
|
||||
@@ -222,7 +223,7 @@ final readonly class CommerceQueryParser
|
||||
fn(string $token): bool => mb_strlen($token) > $this->config->getMinSearchTokenLength()
|
||||
);
|
||||
|
||||
$tokens = $this->filterSearchTokens($tokens);
|
||||
$tokens = $this->normalizeSearchTokens($tokens);
|
||||
|
||||
return trim(implode(' ', $tokens));
|
||||
}
|
||||
@@ -237,8 +238,7 @@ final readonly class CommerceQueryParser
|
||||
fn(string $token): bool => mb_strlen($token) >= $this->config->getMinDirectProductTokenLength()
|
||||
);
|
||||
|
||||
$tokens = $this->filterSearchTokens($tokens);
|
||||
$tokens = array_values(array_unique($tokens));
|
||||
$tokens = $this->normalizeSearchTokens($tokens);
|
||||
|
||||
return trim(implode(' ', $tokens));
|
||||
}
|
||||
@@ -282,11 +282,9 @@ final readonly class CommerceQueryParser
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->isSearchControlToken($token)) {
|
||||
continue;
|
||||
foreach ($this->normalizeSearchTokens([$token]) as $normalizedToken) {
|
||||
$tokens[$normalizedToken] = $normalizedToken;
|
||||
}
|
||||
|
||||
$tokens[$token] = $token;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,10 +297,52 @@ final readonly class CommerceQueryParser
|
||||
*/
|
||||
private function filterSearchTokens(array $tokens): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$tokens,
|
||||
fn(string $token): bool => !$this->isSearchControlToken($token)
|
||||
));
|
||||
return $this->normalizeSearchTokens($tokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $tokens
|
||||
* @return string[]
|
||||
*/
|
||||
private function normalizeSearchTokens(array $tokens): array
|
||||
{
|
||||
$normalizedTokens = [];
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
$token = trim(mb_strtolower((string) $token, 'UTF-8'));
|
||||
|
||||
if ($token === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$token = $this->config->getSearchTokenCorrections()[$token] ?? $token;
|
||||
$token = $this->config->getSearchTokenCanonicalMap()[$token] ?? $token;
|
||||
|
||||
if ($this->isSearchControlToken($token)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalizedTokens[$token] = $token;
|
||||
}
|
||||
|
||||
return array_values($normalizedTokens);
|
||||
}
|
||||
|
||||
private function applySearchTokenCorrections(string $text): string
|
||||
{
|
||||
if ($text === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
foreach ($this->config->getSearchTokenCorrections() as $from => $to) {
|
||||
$text = preg_replace(
|
||||
'/\b' . preg_quote((string) $from, '/') . '\b/u',
|
||||
(string) $to,
|
||||
$text
|
||||
) ?? $text;
|
||||
}
|
||||
|
||||
return preg_replace($this->config->getWhitespaceCollapsePattern(), ' ', $text) ?? $text;
|
||||
}
|
||||
|
||||
private function isSearchControlToken(string $token): bool
|
||||
|
||||
Reference in New Issue
Block a user