Skip to content

Commit cfa3b38

Browse files
Composite aggregation supports now parameters of source aggregations
1 parent 197eedd commit cfa3b38

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/Aggregation/Bucketing/CompositeAggregation.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CompositeAggregation extends AbstractAggregation
3333
* Inner aggregations container init.
3434
*
3535
* @param string $name
36-
* @param BuilderInterface[] $sources
36+
* @param AbstractAggregation[] $sources
3737
*/
3838
public function __construct($name, $sources = [])
3939
{
@@ -45,16 +45,20 @@ public function __construct($name, $sources = [])
4545
}
4646

4747
/**
48-
* @param BuilderInterface $agg
48+
* @param AbstractAggregation $agg
4949
*
5050
* @throws \LogicException
5151
*
5252
* @return self
5353
*/
54-
public function addSource(BuilderInterface $agg)
54+
public function addSource(AbstractAggregation $agg)
5555
{
56+
$array = $agg->getArray();
57+
58+
$array = is_array($array) ? array_merge($array, $agg->getParameters()) : $array;
59+
5660
$this->sources[] = [
57-
$agg->getName() => [ $agg->getType() => $agg->getArray() ]
61+
$agg->getName() => [ $agg->getType() => $array ]
5862
];
5963

6064
return $this;

tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,76 @@ public function testGetType()
4747
$result = $aggregation->getType();
4848
$this->assertEquals('composite', $result);
4949
}
50+
51+
public function testTermsSourceWithOrderParameter()
52+
{
53+
$compositeAgg = new CompositeAggregation('composite_with_order');
54+
$termsAgg = new TermsAggregation('test_term_agg', 'test_field');
55+
$termsAgg->addParameter('order', 'asc');
56+
$compositeAgg->addSource($termsAgg);
57+
58+
$expectedResult = [
59+
'composite' => [
60+
'sources' => [
61+
[
62+
'test_term_agg' => [ 'terms' => ['field' => 'test_field', 'order' => 'asc'] ],
63+
]
64+
]
65+
],
66+
];
67+
68+
$this->assertEquals($expectedResult, $compositeAgg->toArray());
69+
}
70+
71+
72+
public function testTermsSourceWithDescOrderParameter()
73+
{
74+
$compositeAgg = new CompositeAggregation('composite_with_order');
75+
$termsAgg = new TermsAggregation('test_term_agg', 'test_field');
76+
$termsAgg->addParameter('order', 'desc');
77+
$compositeAgg->addSource($termsAgg);
78+
79+
$expectedResult = [
80+
'composite' => [
81+
'sources' => [
82+
[
83+
'test_term_agg' => [ 'terms' => ['field' => 'test_field', 'order' => 'desc'] ],
84+
]
85+
]
86+
],
87+
];
88+
89+
$this->assertEquals($expectedResult, $compositeAgg->toArray());
90+
}
91+
92+
93+
public function testMultipleSourcesWithDifferentOrders()
94+
{
95+
$compositeAgg = new CompositeAggregation('composite_with_order');
96+
97+
$termsAgg = new TermsAggregation('test_term_agg_1', 'test_field');
98+
$termsAgg->addParameter('order', 'desc');
99+
$compositeAgg->addSource($termsAgg);
100+
101+
$termsAgg = new TermsAggregation('test_term_agg_2', 'test_field');
102+
$termsAgg->addParameter('order', 'asc');
103+
$compositeAgg->addSource($termsAgg);
104+
105+
$expectedResult = [
106+
'composite' => [
107+
'sources' => [
108+
[
109+
'test_term_agg_1' => [ 'terms' => ['field' => 'test_field', 'order' => 'desc'] ],
110+
],
111+
[
112+
'test_term_agg_2' => [ 'terms' => ['field' => 'test_field', 'order' => 'asc'] ],
113+
]
114+
]
115+
],
116+
];
117+
118+
$this->assertEquals($expectedResult, $compositeAgg->toArray());
119+
}
120+
121+
50122
}

0 commit comments

Comments
 (0)