diff --git a/src/Application/AggregationResult.php b/src/Application/AggregationResult.php index 738045b..e541780 100644 --- a/src/Application/AggregationResult.php +++ b/src/Application/AggregationResult.php @@ -6,7 +6,7 @@ class AggregationResult { - public function __construct(private string $name, private array $buckets) + public function __construct(private string $name, private array $values) { } @@ -17,6 +17,6 @@ public function name(): string public function values(): array { - return $this->buckets; + return $this->values; } } diff --git a/src/Application/Results.php b/src/Application/Results.php index 7d2d8b4..2a93531 100644 --- a/src/Application/Results.php +++ b/src/Application/Results.php @@ -39,7 +39,7 @@ public function aggregations(): array continue; } - $aggregations[] = new AggregationResult($name, $rawAggregation['buckets']); + $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation); } return $aggregations; diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index c0f96ee..94665b0 100644 --- a/tests/Unit/FinderTest.php +++ b/tests/Unit/FinderTest.php @@ -8,6 +8,7 @@ use InvalidArgumentException; use JeroenG\Explorer\Application\AggregationResult; use JeroenG\Explorer\Application\SearchCommand; +use JeroenG\Explorer\Domain\Aggregations\MaxAggregation; use JeroenG\Explorer\Domain\Aggregations\TermsAggregation; use JeroenG\Explorer\Domain\Query\Query; use JeroenG\Explorer\Domain\Syntax\Compound\BoolQuery; @@ -317,7 +318,8 @@ public function test_it_adds_aggregates(): void ], 'aggs' => [ 'specificAggregation' => ['terms' => ['field' => 'specificField', 'size' => 10]], - 'anotherAggregation' => ['terms' => ['field' => 'anotherField', 'size' => 10]] + 'anotherAggregation' => ['terms' => ['field' => 'anotherField', 'size' => 10]], + 'metricAggregation' => ['max' => ['field' => 'yetAnotherField']], ], ], ]) @@ -337,19 +339,23 @@ public function test_it_adds_aggregates(): void ['key' => 'anotherKey', 'doc_count' => 6] ] ], + 'metricAggregation' => [ + 'value' => 10, + ] ] ]); $query = Query::with(new BoolQuery()); $query->addAggregation('specificAggregation', new TermsAggregation('specificField')); $query->addAggregation('anotherAggregation', new TermsAggregation('anotherField')); + $query->addAggregation('metricAggregation', new MaxAggregation('yetAnotherField')); $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()); + self::assertCount(3, $results->aggregations()); $specificAggregation = $results->aggregations()[0]; @@ -361,6 +367,11 @@ public function test_it_adds_aggregates(): void self::assertEquals(42, $specificAggregationValue['doc_count']); self::assertEquals('myKey', $specificAggregationValue['key']); + + $metricAggregation = $results->aggregations()[2]; + + self::assertArrayHasKey('value', $metricAggregation->values()); + self::assertEquals(10, $metricAggregation->values()['value']); } public function test_it_adds_nested_aggregations(): void