Improve searches

This commit is contained in:
DariusIII
2026-04-22 23:39:48 +02:00
parent 32b0abd44d
commit e18d92fe79
2 changed files with 38 additions and 2 deletions
@@ -531,6 +531,22 @@ class ManticoreSearchDriver implements SearchDriverInterface
return trim($result);
}
private static function scopePreparedQueryToField(string $preparedQuery, string $fieldSelector = ''): string
{
$preparedQuery = trim($preparedQuery);
if ($preparedQuery === '') {
return '';
}
$scopedQuery = '('.$preparedQuery.')';
if ($fieldSelector === '') {
return $scopedQuery;
}
return $fieldSelector.' '.$scopedQuery;
}
/**
* Check if a search query contains negation operators (! or - prefix on words).
*
@@ -1223,7 +1239,7 @@ class ManticoreSearchDriver implements SearchDriverInterface
if (! empty($value)) {
$preparedValue = self::prepareUserSearchQuery($value);
if (! empty($preparedValue)) {
$terms[] = '@@relaxed @'.$key.' '.$preparedValue;
$terms[] = '@@relaxed '.self::scopePreparedQueryToField($preparedValue, '@'.$key);
}
}
}
@@ -1255,7 +1271,7 @@ class ManticoreSearchDriver implements SearchDriverInterface
}
}
$searchExpr = '@@relaxed '.$searchColumns.' '.$preparedSearch;
$searchExpr = '@@relaxed '.self::scopePreparedQueryToField($preparedSearch, $searchColumns);
} else {
return [];
}
+20
View File
@@ -123,6 +123,26 @@ class ManticoreSearchQueryTest extends TestCase
$this->assertSame($expected, $result);
}
#[Test]
public function it_scopes_multi_word_queries_to_the_requested_field(): void
{
$reflection = new ReflectionClass(ManticoreSearchDriver::class);
$method = $reflection->getMethod('scopePreparedQueryToField');
$this->assertSame(
'@searchname (Resurrection 2025)',
$method->invoke(null, 'Resurrection 2025', '@searchname')
);
$this->assertSame(
'@(searchname,plainsearchname) (Resurrection 2025)',
$method->invoke(null, 'Resurrection 2025', '@(searchname,plainsearchname)')
);
$this->assertSame(
'(Resurrection 2025)',
$method->invoke(null, 'Resurrection 2025')
);
}
public static function edgeCaseQueriesProvider(): array
{
return [