Skip to content

Commit

Permalink
Merge pull request #212 from enrise-mbraam/fix-aggregtation-result-bu…
Browse files Browse the repository at this point in the history
…ckets

Add support for getting aggregations on nested aggregation results
  • Loading branch information
Jeroen-G authored Sep 15, 2023
2 parents 5bca9bc + c3c497f commit 1fdab85
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
82 changes: 82 additions & 0 deletions tests/Unit/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit 1fdab85

Please sign in to comment.