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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ All notable changes to this project will be documented in this file based on the
"number_of_significant_value_digits" : 3
}
```

* Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients
maltehuebner marked this conversation as resolved.
Show resolved Hide resolved
* [Field](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random) param for `Elastica\Query\FunctionScore::addRandomScoreFunction`
* [Index Recovery](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html) : the indices recovery API provides insight into on-going index shard recoveries. It was never been implemented into Elastica. [#1537](https://github.com/ruflin/Elastica/pull/1537)
* add parent_id (reference [#1518](https://github.com/ruflin/Elastica/issues/1518)) in QueryBuilder. [#1533]([#1518](https://github.com/ruflin/Elastica/issues/1533))
* implemented ```string_distance``` option in Term Suggestion [#1543](https://github.com/ruflin/Elastica/pull/1543)
* Added `BucketSelector` aggregation [#1554](https://github.com/ruflin/Elastica/pull/1554)

### Improvements

* Using `Elastica\Query\FunctionScore::addRandomScoreFunction` without `$field` parameter is deprecated since ES 6.0 and will fail since ES 7.0

### Deprecated

* 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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']);
}
}