From 104ff706356e792db78f71275d4cb9c0a589d5fe Mon Sep 17 00:00:00 2001 From: Menno Braam Date: Tue, 12 Sep 2023 20:06:22 +0200 Subject: [PATCH 1/4] Add support for getting aggregations on nested aggregation results --- src/Application/Results.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Application/Results.php b/src/Application/Results.php index 3eda4280..7d2d8b45 100644 --- a/src/Application/Results.php +++ b/src/Application/Results.php @@ -30,6 +30,15 @@ public function aggregations(): array $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { + if (array_key_exists('doc_count', $rawAggregation)) { + foreach ($rawAggregation as $nestedAggregationName => $rawNestedAggregation) { + if (isset($rawNestedAggregation['buckets'])) { + $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); + } + } + continue; + } + $aggregations[] = new AggregationResult($name, $rawAggregation['buckets']); } From 7fc6e30f3e23b52078d3a47bfe5587b848c5c6ed Mon Sep 17 00:00:00 2001 From: Menno Braam Date: Wed, 13 Sep 2023 10:51:54 +0200 Subject: [PATCH 2/4] Add test coverage for nested aggregation results --- tests/Unit/FinderTest.php | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index c7cec018..9f3a23bf 100644 --- a/tests/Unit/FinderTest.php +++ b/tests/Unit/FinderTest.php @@ -18,6 +18,7 @@ use JeroenG\Explorer\Infrastructure\Scout\ScoutSearchCommandBuilder; use Mockery; use Mockery\Adapter\Phpunit\MockeryTestCase; +use JeroenG\Explorer\Domain\Aggregations\NestedAggregation; class FinderTest extends MockeryTestCase { @@ -362,6 +363,82 @@ public function test_it_adds_aggregates(): void self::assertEquals('myKey', $specificAggregationValue['key']); } + public function test_it_adds_nested_aggregations(): void + { + $client = Mockery::mock(Client::class); + $client->expects('search') + ->with([ + 'index' => self::TEST_INDEX, + 'body' => [ + 'query' => [ + 'bool' => [ + 'must' => [], + 'should' => [], + 'filter' => [], + ], + ], + 'aggs' => [ + 'nestedAggregation' => [ + 'nested' => [ + 'path' => 'nestedAggregation', + ], + 'aggs' => [ + 'someField' => [ + 'terms' => [ + 'field' => 'nestedAggregation.someField', + 'size' => 10, + ], + ], + ], + ], + ], + ], + ]) + ->andReturn([ + 'hits' => [ + 'total' => ['value' => 1], + 'hits' => [$this->hit()], + ], + 'aggregations' => [ + 'nestedAggregation' => [ + 'doc_count' => 42, + 'someField' => [ + 'doc_count_error_upper_bound' => 0, + 'sum_other_doc_count' => 0, + 'buckets' => [ + ['key' => 'someKey', 'doc_count' => 6,] + ], + ], + ], + ], + ]); + + $query = Query::with(new BoolQuery()); + $nestedAggregation = new NestedAggregation('nestedAggregation'); + $nestedAggregation->add('someField', new TermsAggregation('nestedAggregation.someField')); + $query->addAggregation('nestedAggregation',$nestedAggregation); + $builder = new SearchCommand(self::TEST_INDEX, $query); + $builder->setIndex(self::TEST_INDEX); + + $subject = new Finder($client, $builder); + $results = $subject->find(); + + self::assertCount(1, $results->aggregations()); + + $nestedAggregation = $results->aggregations()[0]; + + self::assertInstanceOf(AggregationResult::class, $nestedAggregation); + self::assertEquals('someField', $nestedAggregation->name()); + self::assertCount(1, $nestedAggregation->values()); + + $nestedAggregationValue = $nestedAggregation->values()[0]; + + + self::assertEquals(6, $nestedAggregationValue['doc_count']); + self::assertEquals('someKey', $nestedAggregationValue['key']); + } + + private function hit(int $id = 1, float $score = 1.0): array { return [ From d29b2d406ba50c63bb2ad7476a8fb91517cf4627 Mon Sep 17 00:00:00 2001 From: Menno Braam Date: Wed, 13 Sep 2023 11:03:37 +0200 Subject: [PATCH 3/4] Codestyle --- tests/Unit/FinderTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index 9f3a23bf..c12ca50f 100644 --- a/tests/Unit/FinderTest.php +++ b/tests/Unit/FinderTest.php @@ -433,12 +433,10 @@ public function test_it_adds_nested_aggregations(): void $nestedAggregationValue = $nestedAggregation->values()[0]; - self::assertEquals(6, $nestedAggregationValue['doc_count']); self::assertEquals('someKey', $nestedAggregationValue['key']); } - private function hit(int $id = 1, float $score = 1.0): array { return [ From c3c497f17058e3022138e3c92c7ce90177b45606 Mon Sep 17 00:00:00 2001 From: Menno Braam Date: Wed, 13 Sep 2023 12:42:50 +0200 Subject: [PATCH 4/4] Catch the escaped mutation --- tests/Unit/FinderTest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index c12ca50f..c0f96ee2 100644 --- a/tests/Unit/FinderTest.php +++ b/tests/Unit/FinderTest.php @@ -391,6 +391,7 @@ public function test_it_adds_nested_aggregations(): void ], ], ], + 'anotherAggregation' => ['terms' => ['field' => 'anotherField', 'size' => 10]] ], ], ]) @@ -410,10 +411,16 @@ public function test_it_adds_nested_aggregations(): void ], ], ], + 'specificAggregation' => [ + 'buckets' => [ + ['key' => 'myKey', 'doc_count' => 42] + ] + ], ], ]); $query = Query::with(new BoolQuery()); + $query->addAggregation('anotherAggregation', new TermsAggregation('anotherField')); $nestedAggregation = new NestedAggregation('nestedAggregation'); $nestedAggregation->add('someField', new TermsAggregation('nestedAggregation.someField')); $query->addAggregation('nestedAggregation',$nestedAggregation); @@ -423,7 +430,7 @@ public function test_it_adds_nested_aggregations(): void $subject = new Finder($client, $builder); $results = $subject->find(); - self::assertCount(1, $results->aggregations()); + self::assertCount(2, $results->aggregations()); $nestedAggregation = $results->aggregations()[0];