diff --git a/docs/Aggregation/Pipeline/PercentilesBucket.md b/docs/Aggregation/Pipeline/PercentilesBucket.md new file mode 100644 index 0000000000000000000000000000000000000000..2ced38abba501eb2e101c02a852cade51299c068 --- /dev/null +++ b/docs/Aggregation/Pipeline/PercentilesBucket.md @@ -0,0 +1,55 @@ +# Percentiles Bucket Aggregation + +> More info about avg bucket aggregation is in the [official elasticsearch docs][1] + +A sibling pipeline aggregation which calculates percentiles across all bucket of a +specified metric in a sibling aggregation. The specified metric must be numeric and +the sibling aggregation must be a multi-bucket aggregation. + +## Simple example + +```JSON +{ + "aggs" : { + "sales_per_month" : { + "date_histogram" : { + "field" : "date", + "interval" : "month" + }, + "aggs": { + "sales": { + "sum": { + "field": "price" + } + } + } + }, + "sum_monthly_sales": { + "percentiles_bucket": { + "buckets_path": "sales_per_month>sales", + "percents": [ 25.0, 50.0, 75.0 ] + } + } + } +} +``` + +And now the query via DSL: + +```php +$search = new Search(); + +$dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); +$dateAggregation->addAggregation( + new SumAggregation('sales', 'price') +); +$percentilesAggregation = new AvgBucketAggregation('sum_monthly_sales', 'sales_per_month>sales') +$percentilesAggregation->setPercentiles([25.0, 50.0, 75.0]); + +$search->addAggregation($dateAggregation); +$search->addAggregation($percentilesAggregation); + +$aggArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html \ No newline at end of file diff --git a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..d74b88f1ca7ee5259a0751c7925c77868e102a2e --- /dev/null +++ b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php @@ -0,0 +1,63 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; + +/** + * Class representing Percentiles Bucket Pipeline Aggregation. + * + * @link https://goo.gl/bqi7m5 + */ +class PercentilesBucketAggregation extends AbstractPipelineAggregation +{ + /** + * @var array + */ + private $percents; + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'percentiles_bucket'; + } + + /** + * @return array + */ + public function getPercents() + { + return $this->percents; + } + + /** + * @param array $percents + */ + public function setPercents(array $percents) + { + $this->percents = $percents; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + $data = ['buckets_path' => $this->getBucketsPath()]; + + if ($this->getPercents()) { + $data['percents'] = $this->getPercents(); + } + + return $data; + } +} diff --git a/tests/Aggregation/Pipeline/PercentilesBucketAggregationTest.php b/tests/Aggregation/Pipeline/PercentilesBucketAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d4f740f172502ec72b652b4662b421f2c14fd87e --- /dev/null +++ b/tests/Aggregation/Pipeline/PercentilesBucketAggregationTest.php @@ -0,0 +1,38 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Tests\Aggregation\Pipeline; + +use ONGR\ElasticsearchDSL\Aggregation\Pipeline\PercentilesBucketAggregation; + +/** + * Unit test for percentiles bucket aggregation. + */ +class PercentilesBucketAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $aggregation = new PercentilesBucketAggregation('acme', 'test'); + $aggregation->setPercents([25.0, 50.0, 75.0]); + + $expected = [ + 'percentiles_bucket' => [ + 'buckets_path' => 'test', + 'percents' => [25.0, 50.0, 75.0], + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +}