Skip to content

Commit

Permalink
Add whereNotIn support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed May 27, 2024
1 parent cb7cb4b commit aadbced
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/Factories/SearchParametersFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,27 @@ protected function makeFilter(Builder $builder): ?array
'terms' => [$field => $values],
])->values();

$filter = $filter->merge($whereIns);
if ($whereIns->isNotEmpty()) {
$filter->push([
'bool' => [
'must' => $whereIns->all(),
],
]);
}
}

if (property_exists($builder, 'whereNotIns')) {
$whereNotIns = collect($builder->whereNotIns)->map(static fn (array $values, string $field) => [
'terms' => [$field => $values],
])->values();

$filter = $filter->merge([[
'bool' => [
'must_not' => $whereNotIns
]
]]);
if ($whereNotIns->isNotEmpty()) {
$filter->push([
'bool' => [
'must_not' => $whereNotIns->all(),
],
]);
}
}

return $filter->isEmpty() ? null : $filter->all();
Expand Down
18 changes: 17 additions & 1 deletion tests/Integration/Engine/EngineSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function test_search_result_can_be_filtered_with_where_clause(): void
$this->assertEquals($target->toArray(), $found->first()->toArray());
}

public function test_search_result_can_be_filtered_with_wherein_clause(): void
public function test_search_result_can_be_filtered_with_where_in_clause(): void
{
if (!method_exists(Builder::class, 'whereIn')) {
$this->markTestSkipped('Method "whereIn" is not supported by current Scout version');
Expand All @@ -77,6 +77,22 @@ public function test_search_result_can_be_filtered_with_wherein_clause(): void
$this->assertEquals($target->toArray(), $found->first()->toArray());
}

public function test_search_result_can_be_filtered_with_where_not_in_clause(): void
{
if (!method_exists(Builder::class, 'whereNotIn')) {
$this->markTestSkipped('Method "whereNotIn" is not supported by current Scout version');
}

// add some mixins
factory(Client::class, rand(2, 10))->create(['email' => '[email protected]']);

$target = factory(Client::class)->create(['email' => '[email protected]']);
$found = Client::search()->whereNotIn('email', ['[email protected]'])->get();

$this->assertCount(1, $found);
$this->assertEquals($target->toArray(), $found->first()->toArray());
}

public function test_search_result_can_be_sorted(): void
{
$source = factory(Client::class, rand(2, 10))->create()->sortBy('email')->values();
Expand Down
43 changes: 41 additions & 2 deletions tests/Integration/Factories/SearchParametersFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function test_search_parameters_can_be_made_from_builder_with_where_filte
], $searchParameters->toArray());
}

public function test_search_parameters_can_be_made_from_builder_with_wherein_filter(): void
public function test_search_parameters_can_be_made_from_builder_with_where_in_filter(): void
{
if (!method_exists(Builder::class, 'whereIn')) {
$this->markTestSkipped('Method "whereIn" is not supported by current Scout version');
Expand All @@ -104,7 +104,46 @@ public function test_search_parameters_can_be_made_from_builder_with_wherein_fil
'query_string' => ['query' => 'book'],
],
'filter' => [
['terms' => ['author_id' => [1, 2]]],
[
'bool' => [
'must' => [
['terms' => ['author_id' => [1, 2]]],
],
],
],
],
],
],
],
], $searchParameters->toArray());
}

public function test_search_parameters_can_be_made_from_builder_with_where_not_in_filter(): void
{
if (!method_exists(Builder::class, 'whereNotIn')) {
$this->markTestSkipped('Method "whereNotIn" is not supported by current Scout version');
}

$model = new Client();
$builder = (new Builder($model, 'book'))->whereNotIn('author_id', [1, 2]);
$searchParameters = $this->searchParametersFactory->makeFromBuilder($builder);

$this->assertSame([
'index' => $model->searchableAs(),
'body' => [
'query' => [
'bool' => [
'must' => [
'query_string' => ['query' => 'book'],
],
'filter' => [
[
'bool' => [
'must_not' => [
['terms' => ['author_id' => [1, 2]]],
],
],
],
],
],
],
Expand Down

0 comments on commit aadbced

Please sign in to comment.