diff --git a/docs/Aggregation/Pipeline/StatsBucket.md b/docs/Aggregation/Pipeline/StatsBucket.md new file mode 100644 index 0000000000000000000000000000000000000000..98aac8753b80c38903325e9d6eea553298249b89 --- /dev/null +++ b/docs/Aggregation/Pipeline/StatsBucket.md @@ -0,0 +1,54 @@ +# Stats Aggregation + +> More info about stats bucket aggregation is in the [official elasticsearch docs][1] + +A sibling pipeline aggregation which calculates a variety of stats 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" + } + } + } + }, + "stats_monthly_sales": { + "stats_bucket": { + "buckets_path": "sales_per_month>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') +); + +$search->addAggregation($dateAggregation); +$search->addAggregation( + new StatsBucketAggregation('stats_monthly_sales', 'sales_per_month>sales') +); + +$aggArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html \ No newline at end of file diff --git a/src/Aggregation/Pipeline/StatsBucketAggregation.php b/src/Aggregation/Pipeline/StatsBucketAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..43d6b62052d55e43ac8f4335acd2157ff6ae42c8 --- /dev/null +++ b/src/Aggregation/Pipeline/StatsBucketAggregation.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 Stats Bucket Pipeline Aggregation. + * + * @link https://goo.gl/apQXaQ + */ +class StatsBucketAggregation extends AbstractPipelineAggregation +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'stats_bucket'; + } +} diff --git a/tests/Aggregation/Pipeline/StatsBucketAggregationTest.php b/tests/Aggregation/Pipeline/StatsBucketAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..47fa412a9fe3f7662b1813f80cf5a36a171e5687 --- /dev/null +++ b/tests/Aggregation/Pipeline/StatsBucketAggregationTest.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\StatsBucketAggregation; + +/** + * Unit test for stats bucket aggregation. + */ +class StatsBucketAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $aggregation = new StatsBucketAggregation('acme', 'test'); + + $expected = [ + 'stats_bucket' => [ + 'buckets_path' => 'test', + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +}