diff --git a/CHANGELOG.md b/CHANGELOG.md index 038d07fc91..aec256edd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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) diff --git a/lib/Elastica/Aggregation/BucketSelector.php b/lib/Elastica/Aggregation/BucketSelector.php new file mode 100644 index 0000000000..ff19e8ef95 --- /dev/null +++ b/lib/Elastica/Aggregation/BucketSelector.php @@ -0,0 +1,52 @@ +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); + } +} + diff --git a/test/Elastica/Aggregation/BucketSelectorTest.php b/test/Elastica/Aggregation/BucketSelectorTest.php new file mode 100644 index 0000000000..2e66921e63 --- /dev/null +++ b/test/Elastica/Aggregation/BucketSelectorTest.php @@ -0,0 +1,90 @@ +_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']); + } +}