optimize retrieval

This commit is contained in:
team 1
2026-04-24 09:04:43 +02:00
parent 55a61e2e71
commit 63b7011567
3 changed files with 65 additions and 8 deletions

View File

@@ -131,13 +131,13 @@ final readonly class CommerceQueryParser
return [];
}
foreach ($matches[1] as $size) {
$sizes[] = trim($size);
foreach ($matches[1] ?? [] as $size) {
$sizes[] = trim((string) $size);
}
if (preg_match_all($this->intentConfig->getSizeTokenValuePattern(), $prompt, $tokenMatches) !== false) {
foreach ($tokenMatches[1] as $sizeToken) {
$sizes[] = trim($sizeToken);
foreach ($tokenMatches[0] ?? [] as $sizeToken) {
$sizes[] = trim((string) $sizeToken);
}
}
@@ -242,6 +242,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));
return trim(implode(' ', $tokens));
@@ -286,6 +287,10 @@ final readonly class CommerceQueryParser
continue;
}
if ($this->isSearchControlToken($token)) {
continue;
}
$tokens[$token] = $token;
}
}
@@ -299,14 +304,38 @@ final readonly class CommerceQueryParser
*/
private function filterSearchTokens(array $tokens): array
{
$stopWords = $this->config->getFilterSearchTokens();
return array_values(array_filter(
$tokens,
static fn(string $token): bool => !in_array($token, $stopWords, true)
fn(string $token): bool => !$this->isSearchControlToken($token)
));
}
private function isSearchControlToken(string $token): bool
{
$token = trim(mb_strtolower($token));
if ($token === '') {
return true;
}
if (in_array($token, $this->config->getFilterSearchTokens(), true)) {
return true;
}
return in_array($token, [
'shop',
'store',
'produkt',
'produkte',
'artikel',
'kaufen',
'kaufe',
'bestellen',
'bestelle',
'online',
], true);
}
private function isDirectProductQuery(string $prompt): bool
{
if ($prompt === '') {
@@ -328,8 +357,10 @@ final readonly class CommerceQueryParser
PREG_SPLIT_NO_EMPTY
) ?: [];
$tokens = $this->filterSearchTokens($tokens);
return count($tokens) <= $this->config->getDirectProductMaxTokens()
&& preg_match($this->config->getDirectProductDigitPattern(), $prompt) === 1;
&& preg_match($this->config->getDirectProductDigitPattern(), implode(' ', $tokens)) === 1;
}
private function containsModelLikePhrase(string $text): bool