diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php index 6282b196cd3b539bb3d6745559c6642f66b5fa43..8f1f8d8a633a7023f7e8102a1f372b46cc4abbe7 100644 --- a/src/Aggregation/Bucketing/CompositeAggregation.php +++ b/src/Aggregation/Bucketing/CompositeAggregation.php @@ -43,7 +43,7 @@ class CompositeAggregation extends AbstractAggregation * Inner aggregations container init. * * @param string $name - * @param BuilderInterface[] $sources + * @param AbstractAggregation[] $sources */ public function __construct($name, $sources = []) { @@ -55,16 +55,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/src/Search.php b/src/Search.php index fbbd9fc4cec3a433e533919411af91fee8c37079..37c208ba10fdc173d5eb580f42f780b4f79e83ba 100644 --- a/src/Search.php +++ b/src/Search.php @@ -481,6 +481,24 @@ class Search return $this; } + /** + * @return bool + */ + public function isTrackTotalHits() + { + return $this->trackTotalHits; + } + + /** + * @param bool $trackTotalHits + * @return $this + */ + public function setTrackTotalHits(bool $trackTotalHits) + { + $this->trackTotalHits = $trackTotalHits; + return $this; + } + /** * @return int */ diff --git a/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php index b3bd6253cc84f39409200cfd732ec90cd9ce64ee..40ecf789b3489a117b72d7be26c3c6f65c7ffcc2 100644 --- a/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php @@ -95,4 +95,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()); + } + + }