Skip to content
Snippets Groups Projects
Commit cfa3b38e authored by Oliver Skroblin's avatar Oliver Skroblin
Browse files

Composite aggregation supports now parameters of source aggregations

parent 197eedda
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment