diff --git a/docs/Aggregation/Pipeline/CumulativeSum.md b/docs/Aggregation/Pipeline/CumulativeSum.md new file mode 100644 index 0000000000000000000000000000000000000000..febd6feefc2bcc1606ab9e8adcc10435c8473f3a --- /dev/null +++ b/docs/Aggregation/Pipeline/CumulativeSum.md @@ -0,0 +1,55 @@ +# Cumulative Sum Aggregation + +> More info about cumulative sum aggregation is in the [official elasticsearch docs][1] + +A parent pipeline aggregation which calculates the cumulative sum of a specified metric +in a parent histogram (or date_histogram) aggregation. The specified metric must be numeric +and the enclosing histogram must have min_doc_count set to 0 (default for histogram +aggregations). + +## Simple example + +```JSON +{ + "aggs" : { + "sales_per_month" : { + "date_histogram" : { + "field" : "date", + "interval" : "month" + }, + "aggs": { + "sales": { + "sum": { + "field": "price" + } + }, + "cumulative_sales": { + "cumulative_sum": { + "buckets_path": "sales" + } + } + } + } + } +} +``` + +And now the query via DSL: + +```php +$search = new Search(); + +$dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); +$dateAggregation->addAggregation( + new SumAggregation('sales', 'price') +); +$dateAggregation->addAggregation( + new CumulativeSumAggregation('cumulative_sales', 'sales') +); + +$search->addAggregation($dateAggregation); + +$aggArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html \ No newline at end of file diff --git a/src/Aggregation/Pipeline/CumulativeSumAggregation.php b/src/Aggregation/Pipeline/CumulativeSumAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..dff8a9cb7d0d3fc33d9a14236fa8a8f0b1a8fe92 --- /dev/null +++ b/src/Aggregation/Pipeline/CumulativeSumAggregation.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 Cumulative Sum Pipeline Aggregation. + * + * @link https://goo.gl/EUzda6 + */ +class CumulativeSumAggregation extends AbstractPipelineAggregation +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'cumulative_sum'; + } +} diff --git a/tests/Aggregation/Pipeline/CumulativeSumAggregationTest.php b/tests/Aggregation/Pipeline/CumulativeSumAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b2214a1bd5cf4416086209675f77ff18651f0b60 --- /dev/null +++ b/tests/Aggregation/Pipeline/CumulativeSumAggregationTest.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\CumulativeSumAggregation; + +/** + * Unit test for cumulative sum aggregation. + */ +class CumulativeSumAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $aggregation = new CumulativeSumAggregation('acme', 'test'); + + $expected = [ + 'cumulative_sum' => [ + 'buckets_path' => 'test', + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +}