diff --git a/docs/Aggregation/Pipeline/ExtendedStatsBucket.md b/docs/Aggregation/Pipeline/ExtendedStatsBucket.md new file mode 100644 index 0000000000000000000000000000000000000000..e1fa5a3915c8c605b4961999e386a8491029f50d --- /dev/null +++ b/docs/Aggregation/Pipeline/ExtendedStatsBucket.md @@ -0,0 +1,52 @@ +# Extended Stats Bucket Aggregation + +> More info about extended stats bucket aggregation is in the [official elasticsearch docs][1] + +This aggregation provides a few more statistics (sum of squares, standard deviation, etc) compared to the stats_bucket` aggregation. + +## Simple example + +```JSON +{ + "aggs" : { + "sales_per_month" : { + "date_histogram" : { + "field" : "date", + "interval" : "month" + }, + "aggs": { + "sales": { + "sum": { + "field": "price" + } + } + } + }, + "stats_monthly_sales": { + "extended_stats_bucket": { + "buckets_paths": "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 ExtendedStatsBucketAggregation('stats_monthly_sales', 'sales_per_month>sales') +); + +$aggArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-extended-stats-bucket-aggregation.html \ No newline at end of file diff --git a/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php b/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..aa8697a7b9617b5bb81aa9b5154c92ee5a6717ce --- /dev/null +++ b/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.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 Extended Stats Bucket Pipeline Aggregation. + * + * @link https://goo.gl/rn8vtA + */ +class ExtendedStatsBucketAggregation extends AbstractPipelineAggregation +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'extended_stats_bucket'; + } +} diff --git a/tests/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php b/tests/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..48689073b22ba01899d0a10d7eb4127fbf78a995 --- /dev/null +++ b/tests/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.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\ExtendedStatsBucketAggregation; + +/** + * Unit test for stats bucket aggregation. + */ +class ExtendedStatsBucketAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $aggregation = new ExtendedStatsBucketAggregation('acme', 'test'); + + $expected = [ + 'extended_stats_bucket' => [ + 'buckets_path' => 'test', + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +}