Skip to content

Commit 83d1a61

Browse files
maltehuebnerp365labs
authored andcommitted
Added BucketSelector aggregation (#1554)
* Added BucketSelector aggregation and test and add php 7.0 compatible type hints.
1 parent a655ed6 commit 83d1a61

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file based on the
99

1010
### Added
1111

12+
* Added `BucketSelector` aggregation [#1554](https://github.com/ruflin/Elastica/pull/1554)
1213
* Added `DerivativeAggregation` [#1553](https://github.com/ruflin/Elastica/pull/1553)
1314

1415
### Improvements
@@ -54,6 +55,7 @@ All notable changes to this project will be documented in this file based on the
5455
"number_of_significant_value_digits" : 3
5556
}
5657
```
58+
5759
* Never implemented the method *Missing* on [`Aggregation\Percentiles`](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-metrics-percentile-aggregation.html) [#1532](https://github.com/ruflin/Elastica/pull/1532)
5860

5961
## [6.0.2](https://github.com/ruflin/Elastica/compare/6.0.1...6.0.2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace Elastica\Aggregation;
3+
4+
/**
5+
* Class BucketSelector.
6+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html
7+
*/
8+
class BucketSelector extends AbstractSimpleAggregation
9+
{
10+
/**
11+
* @param string $name
12+
* @param array|null $bucketsPath
13+
* @param string|null $script
14+
*/
15+
public function __construct(string $name, array $bucketsPath = null, string $script = null)
16+
{
17+
parent::__construct($name);
18+
19+
if ($bucketsPath !== null) {
20+
$this->setBucketsPath($bucketsPath);
21+
}
22+
23+
if ($script !== null) {
24+
$this->setScript($script);
25+
}
26+
}
27+
28+
/**
29+
* Set the buckets_path for this aggregation.
30+
*
31+
* @param array $bucketsPath
32+
*
33+
* @return $this
34+
*/
35+
public function setBucketsPath($bucketsPath)
36+
{
37+
return $this->setParam('buckets_path', $bucketsPath);
38+
}
39+
40+
/**
41+
* Set the gap policy for this aggregation.
42+
*
43+
* @param string $gapPolicy
44+
*
45+
* @return $this
46+
*/
47+
public function setGapPolicy(string $gapPolicy = 'skip')
48+
{
49+
return $this->setParam('gap_policy', $gapPolicy);
50+
}
51+
}
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
namespace Elastica\Test\Aggregation;
3+
4+
use Elastica\Aggregation\BucketSelector;
5+
use Elastica\Aggregation\DateHistogram;
6+
use Elastica\Aggregation\Max;
7+
use Elastica\Document;
8+
use Elastica\Query;
9+
10+
class BucketSelectorTest extends BaseAggregationTest
11+
{
12+
protected function _getIndexForTest()
13+
{
14+
$index = $this->_createIndex();
15+
16+
$index->getType('test')->addDocuments([
17+
new Document(1, ['date' => '2018-12-01', 'value' => 1]),
18+
new Document(2, ['date' => '2018-12-02', 'value' => 2]),
19+
new Document(3, ['date' => '2018-12-03', 'value' => 5]),
20+
new Document(4, ['date' => '2018-12-04', 'value' => 4]),
21+
new Document(5, ['date' => '2018-12-05', 'value' => 6]),
22+
new Document(6, ['date' => '2018-12-06', 'value' => 9]),
23+
new Document(7, ['date' => '2018-12-07', 'value' => 11]),
24+
new Document(8, ['date' => '2018-12-08', 'value' => 4]),
25+
new Document(9, ['date' => '2018-12-09', 'value' => 7]),
26+
new Document(10, ['date' => '2018-12-10', 'value' => 4]),
27+
]);
28+
29+
$index->refresh();
30+
31+
return $index;
32+
}
33+
34+
/**
35+
* @group unit
36+
*/
37+
public function testToArray()
38+
{
39+
$expected = [
40+
'max' => [
41+
'field' => 'value',
42+
],
43+
'aggs' => [
44+
'selector_agg' => [
45+
'bucket_selector' => [
46+
'buckets_path' => ['max' => 'max_agg'],
47+
'script' => 'params.max > 5',
48+
],
49+
],
50+
],
51+
];
52+
53+
$maxAgg = new Max('max_agg');
54+
$maxAgg->setField('value');
55+
56+
$selectorAgg = new BucketSelector('selector_agg', ['max' => 'max_agg'], 'params.max > 5');
57+
$maxAgg->addAggregation($selectorAgg);
58+
59+
$this->assertEquals($expected, $maxAgg->toArray());
60+
}
61+
62+
/**
63+
* @group functional
64+
*/
65+
public function testMaxAggregation()
66+
{
67+
$index = $this->_getIndexForTest();
68+
69+
$dateHistogramAgg = new DateHistogram('histogram_agg', 'date', 'day');
70+
$dateHistogramAgg->setFormat('yyyy-MM-dd');
71+
72+
$maxAgg = new Max('max_agg');
73+
$maxAgg->setField('value');
74+
$dateHistogramAgg->addAggregation($maxAgg);
75+
76+
$bucketAgg = new BucketSelector('selector_agg', ['max' => 'max_agg'], 'params.max > 5');
77+
$dateHistogramAgg->addAggregation($bucketAgg);
78+
79+
$query = new Query();
80+
$query->addAggregation($dateHistogramAgg);
81+
82+
$dateHistogramAggResult = $index->search($query)->getAggregation('histogram_agg')['buckets'];
83+
84+
$this->assertEquals(4, count($dateHistogramAggResult));
85+
$this->assertEquals(6, $dateHistogramAggResult[0]['max_agg']['value']);
86+
$this->assertEquals(9, $dateHistogramAggResult[1]['max_agg']['value']);
87+
$this->assertEquals(11, $dateHistogramAggResult[2]['max_agg']['value']);
88+
$this->assertEquals(7, $dateHistogramAggResult[3]['max_agg']['value']);
89+
}
90+
}

0 commit comments

Comments
 (0)