Skip to content

Commit

Permalink
[Bug] Handle multiple spaces in text search (#10444)
Browse files Browse the repository at this point in the history
* reduce multi-spaces to one

* multi-space test
  • Loading branch information
vd1992 authored May 23, 2024
1 parent 0f6d236 commit ace1409
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public static function scopeNegationNameAndEmail(Builder $query, ?array $negatio
public static function scopeGeneralSearch(Builder $query, ?string $searchTerm): Builder
{
if ($searchTerm) {
$combinedSearchTerm = trim($searchTerm);
$combinedSearchTerm = trim(preg_replace('/\s{2,}/', ' ', $searchTerm));

$query
// attach the tsquery to every row to use for filtering
Expand Down
62 changes: 62 additions & 0 deletions api/tests/Feature/KeywordSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,66 @@ public function testUserSearchPartialNamesEmailsWithPunctuation()
'id' => $user2->id,
])->assertJsonCount(2, 'data.usersPaginated.data');
}

public function testUserSearchMultiSpacing()
{
$user1 = User::factory()->create([
'first_name' => 'john',
'last_name' => 'smith',
'email' => 'john@test.com',
'current_city' => 'abc',
]);
$user2 = User::factory()->create([
'first_name' => 'bob',
'last_name' => 'johnson',
'email' => 'bob@test.com',
'current_city' => 'efg',
]);
$user3 = User::factory()->create([
'first_name' => 'jones',
'last_name' => 'hall',
'email' => 'johnson@test.com',
'current_city' => 'hij',
]);

// search operators unaffected by multiple spaces being present, two users returned regardless of spacing
$this->actingAs($this->platformAdmin, 'api')->graphQL(
/** @lang GraphQL */
'
query getUsersPaginated($where: UserFilterInput) {
usersPaginated(where: $where) {
data {
id
}
}
}
', [
'where' => [
'generalSearch' => '"johnson" OR john@',
],
])->assertJsonFragment([
'id' => $user1->id,
])->assertJsonFragment([
'id' => $user2->id,
])->assertJsonCount(2, 'data.usersPaginated.data');
$this->actingAs($this->platformAdmin, 'api')->graphQL(
/** @lang GraphQL */
'
query getUsersPaginated($where: UserFilterInput) {
usersPaginated(where: $where) {
data {
id
}
}
}
', [
'where' => [
'generalSearch' => ' "johnson" OR john@ ',
],
])->assertJsonFragment([
'id' => $user1->id,
])->assertJsonFragment([
'id' => $user2->id,
])->assertJsonCount(2, 'data.usersPaginated.data');
}
}

0 comments on commit ace1409

Please sign in to comment.