Skip to content

Commit

Permalink
Merge pull request #216 from quintenbuis/support-non-bucket-based-agg…
Browse files Browse the repository at this point in the history
…regations

Fixes non-bucket based aggregations throwing exception
  • Loading branch information
Jeroen-G authored Sep 21, 2023
2 parents c7f3aa0 + 5da9305 commit fc68836
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Application/AggregationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AggregationResult
{
public function __construct(private string $name, private array $buckets)
public function __construct(private string $name, private array $values)
{
}

Expand All @@ -17,6 +17,6 @@ public function name(): string

public function values(): array
{
return $this->buckets;
return $this->values;
}
}
2 changes: 1 addition & 1 deletion src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function aggregations(): array
continue;
}

$aggregations[] = new AggregationResult($name, $rawAggregation['buckets']);
$aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);
}

return $aggregations;
Expand Down
15 changes: 13 additions & 2 deletions tests/Unit/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']],
],
],
])
Expand All @@ -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];

Expand All @@ -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
Expand Down

0 comments on commit fc68836

Please sign in to comment.