From 24f785c3f11cba18aaf5a1e84be458571fe76e88 Mon Sep 17 00:00:00 2001 From: Quinten Buis Date: Thu, 21 Sep 2023 11:25:21 +0200 Subject: [PATCH 1/3] Fixes non-bucket based aggregations throwing exception --- src/Application/AggregationResult.php | 4 ++-- src/Application/Results.php | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Application/AggregationResult.php b/src/Application/AggregationResult.php index 738045b6..e5417806 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 7d2d8b45..603e2e67 100644 --- a/src/Application/Results.php +++ b/src/Application/Results.php @@ -32,14 +32,12 @@ public function aggregations(): array 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']); - } + $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets'] ?? $rawNestedAggregation); } continue; } - $aggregations[] = new AggregationResult($name, $rawAggregation['buckets']); + $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation); } return $aggregations; From d8a50f80feb28ca3b751800d69c66db6f8471b63 Mon Sep 17 00:00:00 2001 From: Quinten Buis Date: Thu, 21 Sep 2023 11:40:35 +0200 Subject: [PATCH 2/3] Adds test to validate --- tests/Unit/FinderTest.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index c0f96ee2..94665b0f 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 From 5da9305c60289647fac800bd84e9266f60af1053 Mon Sep 17 00:00:00 2001 From: Quinten Buis Date: Thu, 21 Sep 2023 11:45:35 +0200 Subject: [PATCH 3/3] Fixes broken test --- src/Application/Results.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Application/Results.php b/src/Application/Results.php index 603e2e67..2a935311 100644 --- a/src/Application/Results.php +++ b/src/Application/Results.php @@ -32,7 +32,9 @@ public function aggregations(): array foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { foreach ($rawAggregation as $nestedAggregationName => $rawNestedAggregation) { - $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets'] ?? $rawNestedAggregation); + if (isset($rawNestedAggregation['buckets'])) { + $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); + } } continue; }