diff --git a/docs/Aggregation/Pipeline/AvgBucket.md b/docs/Aggregation/Pipeline/AvgBucket.md new file mode 100644 index 0000000000000000000000000000000000000000..98618e4bcb05a72cfd6d412e707ced73cb314c16 --- /dev/null +++ b/docs/Aggregation/Pipeline/AvgBucket.md @@ -0,0 +1,54 @@ +# Avg Bucket Aggregation + +> More info about avg bucket aggregation is in the [official elasticsearch docs][1] + +A sibling pipeline aggregation which calculates the (mean) average value 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" + } + } + } + }, + "avg_monthly_sales": { + "avg_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 AvgBucketAggregation('avg_monthly_sales', 'sales_per_month>sales') +); + +$aggArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html diff --git a/src/Aggregation/Pipeline/AvgBucketAggregation.php b/src/Aggregation/Pipeline/AvgBucketAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..69e0f93fefe66e9ba32b80be7249e3b40329e5ad --- /dev/null +++ b/src/Aggregation/Pipeline/AvgBucketAggregation.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 Avg Bucket Pipeline Aggregation. + * + * @link https://goo.gl/SWK2nP + */ +class AvgBucketAggregation extends AbstractPipelineAggregation +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'avg_bucket'; + } +} diff --git a/tests/Aggregation/Pipeline/AvgBucketAggregationTest.php b/tests/Aggregation/Pipeline/AvgBucketAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..05a350ccb0533e7094c9f54a3ebd1d4f7ccd9596 --- /dev/null +++ b/tests/Aggregation/Pipeline/AvgBucketAggregationTest.php @@ -0,0 +1,39 @@ +<?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\AvgBucketAggregation; + +/** + * Unit test for avg_bucket aggregation. + */ +class AvgBucketAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests getArray method. + */ + public function testGetArray() + { + $aggregation = new AvgBucketAggregation('foo', 'foo>bar'); + + $this->assertEquals(['buckets_path' => 'foo>bar'], $aggregation->getArray()); + } + + /** + * Tests getType method. + */ + public function testAvgBucketAggregationGetType() + { + $aggregation = new AvgBucketAggregation('foo', 'foo>bar'); + $this->assertEquals('avg_bucket', $aggregation->getType()); + } +}