Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BucketSelector aggregation #1554

Merged
merged 11 commits into from Dec 17, 2018
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file based on the

### Added

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

### Improvements
Expand Down Expand Up @@ -54,6 +55,7 @@ All notable changes to this project will be documented in this file based on the
"number_of_significant_value_digits" : 3
}
```

* 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)

## [6.0.2](https://github.com/ruflin/Elastica/compare/6.0.1...6.0.2)
Expand Down
52 changes: 52 additions & 0 deletions lib/Elastica/Aggregation/BucketSelector.php
@@ -0,0 +1,52 @@
<?php
namespace Elastica\Aggregation;

/**
* Class BucketSelector.
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html
*/
class BucketSelector extends AbstractSimpleAggregation
{
/**
* @param string $name
* @param array|null $bucketsPath
* @param string|null $script
*/
public function __construct(string $name, array $bucketsPath = null, string $script = null)
{
parent::__construct($name);

if ($bucketsPath !== null) {
$this->setBucketsPath($bucketsPath);
}

if ($script !== null) {
$this->setScript($script);
}
}

/**
* Set the buckets_path for this aggregation.
*
* @param array $bucketsPath
*
* @return $this
*/
public function setBucketsPath($bucketsPath)
{
return $this->setParam('buckets_path', $bucketsPath);
}

/**
* Set the gap policy for this aggregation.
*
* @param string $gapPolicy
*
* @return $this
*/
public function setGapPolicy(string $gapPolicy = 'skip')
{
return $this->setParam('gap_policy', $gapPolicy);
}
}

90 changes: 90 additions & 0 deletions test/Elastica/Aggregation/BucketSelectorTest.php
@@ -0,0 +1,90 @@
<?php
namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\BucketSelector;
use Elastica\Aggregation\DateHistogram;
use Elastica\Aggregation\Max;
use Elastica\Document;
use Elastica\Query;

class BucketSelectorTest extends BaseAggregationTest
{
protected function _getIndexForTest()
{
$index = $this->_createIndex();

$index->getType('test')->addDocuments([
new Document(1, ['date' => '2018-12-01', 'value' => 1]),
new Document(2, ['date' => '2018-12-02', 'value' => 2]),
new Document(3, ['date' => '2018-12-03', 'value' => 5]),
new Document(4, ['date' => '2018-12-04', 'value' => 4]),
new Document(5, ['date' => '2018-12-05', 'value' => 6]),
new Document(6, ['date' => '2018-12-06', 'value' => 9]),
new Document(7, ['date' => '2018-12-07', 'value' => 11]),
new Document(8, ['date' => '2018-12-08', 'value' => 4]),
new Document(9, ['date' => '2018-12-09', 'value' => 7]),
new Document(10, ['date' => '2018-12-10', 'value' => 4]),
]);

$index->refresh();

return $index;
}

/**
* @group unit
*/
public function testToArray()
{
$expected = [
'max' => [
'field' => 'value',
],
'aggs' => [
'selector_agg' => [
'bucket_selector' => [
'buckets_path' => ['max' => 'max_agg'],
'script' => 'params.max > 5',
],
],
],
];

$maxAgg = new Max('max_agg');
$maxAgg->setField('value');

$selectorAgg = new BucketSelector('selector_agg', ['max' => 'max_agg'], 'params.max > 5');
$maxAgg->addAggregation($selectorAgg);

$this->assertEquals($expected, $maxAgg->toArray());
}

/**
* @group functional
*/
public function testMaxAggregation()
{
$index = $this->_getIndexForTest();

$dateHistogramAgg = new DateHistogram('histogram_agg', 'date', 'day');
$dateHistogramAgg->setFormat('yyyy-MM-dd');

$maxAgg = new Max('max_agg');
$maxAgg->setField('value');
$dateHistogramAgg->addAggregation($maxAgg);

$bucketAgg = new BucketSelector('selector_agg', ['max' => 'max_agg'], 'params.max > 5');
$dateHistogramAgg->addAggregation($bucketAgg);

$query = new Query();
$query->addAggregation($dateHistogramAgg);

$dateHistogramAggResult = $index->search($query)->getAggregation('histogram_agg')['buckets'];

$this->assertEquals(4, count($dateHistogramAggResult));
$this->assertEquals(6, $dateHistogramAggResult[0]['max_agg']['value']);
$this->assertEquals(9, $dateHistogramAggResult[1]['max_agg']['value']);
$this->assertEquals(11, $dateHistogramAggResult[2]['max_agg']['value']);
$this->assertEquals(7, $dateHistogramAggResult[3]['max_agg']['value']);
}
}