From cfa3b38e376e0d930bc4374da3eeff3b4ae81b30 Mon Sep 17 00:00:00 2001 From: Oliver Skroblin <o.skroblin@shopware.com> Date: Wed, 11 Sep 2019 11:57:14 +0200 Subject: [PATCH] Composite aggregation supports now parameters of source aggregations --- .../Bucketing/CompositeAggregation.php | 12 ++-- .../Bucketing/CompositeAggregationTest.php | 72 +++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php index d36c900..7d42d4f 100644 --- a/src/Aggregation/Bucketing/CompositeAggregation.php +++ b/src/Aggregation/Bucketing/CompositeAggregation.php @@ -33,7 +33,7 @@ class CompositeAggregation extends AbstractAggregation * Inner aggregations container init. * * @param string $name - * @param BuilderInterface[] $sources + * @param AbstractAggregation[] $sources */ public function __construct($name, $sources = []) { @@ -45,16 +45,20 @@ class CompositeAggregation extends AbstractAggregation } /** - * @param BuilderInterface $agg + * @param AbstractAggregation $agg * * @throws \LogicException * * @return self */ - public function addSource(BuilderInterface $agg) + public function addSource(AbstractAggregation $agg) { + $array = $agg->getArray(); + + $array = is_array($array) ? array_merge($array, $agg->getParameters()) : $array; + $this->sources[] = [ - $agg->getName() => [ $agg->getType() => $agg->getArray() ] + $agg->getName() => [ $agg->getType() => $array ] ]; return $this; diff --git a/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php index d109532..70e9c62 100644 --- a/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php @@ -47,4 +47,76 @@ class CompositeAggregationTest extends \PHPUnit\Framework\TestCase $result = $aggregation->getType(); $this->assertEquals('composite', $result); } + + public function testTermsSourceWithOrderParameter() + { + $compositeAgg = new CompositeAggregation('composite_with_order'); + $termsAgg = new TermsAggregation('test_term_agg', 'test_field'); + $termsAgg->addParameter('order', 'asc'); + $compositeAgg->addSource($termsAgg); + + $expectedResult = [ + 'composite' => [ + 'sources' => [ + [ + 'test_term_agg' => [ 'terms' => ['field' => 'test_field', 'order' => 'asc'] ], + ] + ] + ], + ]; + + $this->assertEquals($expectedResult, $compositeAgg->toArray()); + } + + + public function testTermsSourceWithDescOrderParameter() + { + $compositeAgg = new CompositeAggregation('composite_with_order'); + $termsAgg = new TermsAggregation('test_term_agg', 'test_field'); + $termsAgg->addParameter('order', 'desc'); + $compositeAgg->addSource($termsAgg); + + $expectedResult = [ + 'composite' => [ + 'sources' => [ + [ + 'test_term_agg' => [ 'terms' => ['field' => 'test_field', 'order' => 'desc'] ], + ] + ] + ], + ]; + + $this->assertEquals($expectedResult, $compositeAgg->toArray()); + } + + + public function testMultipleSourcesWithDifferentOrders() + { + $compositeAgg = new CompositeAggregation('composite_with_order'); + + $termsAgg = new TermsAggregation('test_term_agg_1', 'test_field'); + $termsAgg->addParameter('order', 'desc'); + $compositeAgg->addSource($termsAgg); + + $termsAgg = new TermsAggregation('test_term_agg_2', 'test_field'); + $termsAgg->addParameter('order', 'asc'); + $compositeAgg->addSource($termsAgg); + + $expectedResult = [ + 'composite' => [ + 'sources' => [ + [ + 'test_term_agg_1' => [ 'terms' => ['field' => 'test_field', 'order' => 'desc'] ], + ], + [ + 'test_term_agg_2' => [ 'terms' => ['field' => 'test_field', 'order' => 'asc'] ], + ] + ] + ], + ]; + + $this->assertEquals($expectedResult, $compositeAgg->toArray()); + } + + } -- GitLab