diff --git a/docs/Aggregation/Pipeline/SumBucket.md b/docs/Aggregation/Pipeline/SumBucket.md new file mode 100644 index 0000000000000000000000000000000000000000..175833db27db0e72e53ce5f971cfd6a73e778ae5 --- /dev/null +++ b/docs/Aggregation/Pipeline/SumBucket.md @@ -0,0 +1,53 @@ +# Sum Bucket Aggregation + +> More info about sum bucket aggregation is in the [official elasticsearch docs][1] + +A sibling pipeline aggregation which calculates the sum 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": { + "sum_bucket": { + "buckets_path": "sales_per_month>sales" + } + } + } +} +``` + +And now the query via DSL: + +```php +$dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); +$dateAggregation->addAggregation(new SumAggregation('sales', 'price')); +$sumBucketAggregation = new SumBucketAggregation( + 'sum_monthly_sales', + 'sales_per_month>sales' +) + +$search = new Search(); +$search->addAggregation($dateAggregation); +$search->addAggregation($sumBucketAggregation); + +$queryArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html diff --git a/src/Aggregation/Pipeline/SumBucketAggregation.php b/src/Aggregation/Pipeline/SumBucketAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..b03859aebaf9781d167691ec327b7175e6c8b146 --- /dev/null +++ b/src/Aggregation/Pipeline/SumBucketAggregation.php @@ -0,0 +1,28 @@ +<?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 Max Bucket Pipeline Aggregation. + * + * @link https://goo.gl/8gIfok + */ +class SumBucketAggregation extends AbstractPipelineAggregation +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'sum_bucket'; + } +} diff --git a/tests/Aggregation/Pipeline/SumBucketAggregationTest.php b/tests/Aggregation/Pipeline/SumBucketAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a909354137c9f59ef7ce31a2ab7aca1e00a8d12e --- /dev/null +++ b/tests/Aggregation/Pipeline/SumBucketAggregationTest.php @@ -0,0 +1,36 @@ +<?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\SumBucketAggregation; + +/** + * Unit test for sum bucket aggregation. + */ +class SumBucketAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $aggregation = new SumBucketAggregation('acme', 'test'); + + $expected = [ + 'sum_bucket' => [ + 'buckets_path' => 'test', + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +}