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']); } diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index c7cec018..c0f96ee2 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,87 @@ 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, + ], + ], + ], + ], + 'anotherAggregation' => ['terms' => ['field' => 'anotherField', '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,] + ], + ], + ], + '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); + $builder = new SearchCommand(self::TEST_INDEX, $query); + $builder->setIndex(self::TEST_INDEX); + + $subject = new Finder($client, $builder); + $results = $subject->find(); + + self::assertCount(2, $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 [