From f9bbfb8436ae0260d3e49811b6c2f13a8ae9d228 Mon Sep 17 00:00:00 2001 From: Aniket Das Date: Mon, 19 Feb 2024 00:51:28 +0530 Subject: [PATCH 1/2] add `whereNotIn` support --- src/Factories/SearchParametersFactory.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Factories/SearchParametersFactory.php b/src/Factories/SearchParametersFactory.php index 6bd4105..ce67884 100644 --- a/src/Factories/SearchParametersFactory.php +++ b/src/Factories/SearchParametersFactory.php @@ -77,6 +77,18 @@ protected function makeFilter(Builder $builder): ?array $filter = $filter->merge($whereIns); } + 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 + ] + ]]); + } + return $filter->isEmpty() ? null : $filter->all(); } From aadbced4cd8486131d20f18a191e5fab2ec80af3 Mon Sep 17 00:00:00 2001 From: Ivan Babenko Date: Mon, 27 May 2024 19:39:21 +0200 Subject: [PATCH 2/2] Add whereNotIn support --- src/Factories/SearchParametersFactory.php | 20 ++++++--- tests/Integration/Engine/EngineSearchTest.php | 18 +++++++- .../Factories/SearchParametersFactoryTest.php | 43 ++++++++++++++++++- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/Factories/SearchParametersFactory.php b/src/Factories/SearchParametersFactory.php index ce67884..11affb1 100644 --- a/src/Factories/SearchParametersFactory.php +++ b/src/Factories/SearchParametersFactory.php @@ -74,7 +74,13 @@ 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')) { @@ -82,11 +88,13 @@ protected function makeFilter(Builder $builder): ?array '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(); diff --git a/tests/Integration/Engine/EngineSearchTest.php b/tests/Integration/Engine/EngineSearchTest.php index 321bb61..0b8a475 100644 --- a/tests/Integration/Engine/EngineSearchTest.php +++ b/tests/Integration/Engine/EngineSearchTest.php @@ -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'); @@ -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' => 'foo@test.com']); + + $target = factory(Client::class)->create(['email' => 'bar@test.com']); + $found = Client::search()->whereNotIn('email', ['foo@test.com'])->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(); diff --git a/tests/Integration/Factories/SearchParametersFactoryTest.php b/tests/Integration/Factories/SearchParametersFactoryTest.php index 59b59c6..3108909 100644 --- a/tests/Integration/Factories/SearchParametersFactoryTest.php +++ b/tests/Integration/Factories/SearchParametersFactoryTest.php @@ -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'); @@ -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]]], + ], + ], + ], ], ], ],