diff --git a/docs/Aggregation/Pipeline/SerialDifferencing.md b/docs/Aggregation/Pipeline/SerialDifferencing.md new file mode 100644 index 0000000000000000000000000000000000000000..5f3342e9ce44e63a8d1e2ae7cdbee7d3c4acafc0 --- /dev/null +++ b/docs/Aggregation/Pipeline/SerialDifferencing.md @@ -0,0 +1,54 @@ +# Serial Differencing Aggregation + +> More info about serial differencing aggregation is in the [official elasticsearch docs][1] + +Serial differencing is a technique where values in a time series are subtracted from itself at +different time lags or periods + +## Simple example + +```JSON +{ + "aggs": { + "my_date_histo": { + "date_histogram": { + "field": "timestamp", + "interval": "day" + }, + "aggs": { + "the_sum": { + "sum": { + "field": "lemmings" + } + }, + "thirtieth_difference": { + "serial_diff": { + "buckets_path": "the_sum", + "lag" : 30 + } + } + } + } + } +} +``` + +And now the query via DSL: + +```php +$search = new Search(); + +$dateAggregation = new DateHistogramAggregation('my_date_histo', 'timestamp', 'day'); +$dateAggregation->addAggregation( + new SumAggregation('the_sum', 'lemmings') +); +$diffAggregation = new SerialDifferencingAggregation('thirtieth_difference', 'the_sum'); +$diffAggregation->addParameter('lag', 30); +$dateAggregation->addAggregation($diffAggregation); + +$search->addAggregation($dateAggregation); + +$aggArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html \ No newline at end of file diff --git a/src/Aggregation/Pipeline/SerialDifferencingAggregation.php b/src/Aggregation/Pipeline/SerialDifferencingAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..715c0706ef67e97417eaa40667426bad5597d2ea --- /dev/null +++ b/src/Aggregation/Pipeline/SerialDifferencingAggregation.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 Serial Differencing Pipeline Aggregation. + * + * @link https://goo.gl/46ZR4v + */ +class SerialDifferencingAggregation extends AbstractPipelineAggregation +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'serial_diff'; + } +} diff --git a/tests/Aggregation/Pipeline/SerialDifferencingAggregationTest.php b/tests/Aggregation/Pipeline/SerialDifferencingAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..aa52044634afbf46d34670fc44145b9b1470967e --- /dev/null +++ b/tests/Aggregation/Pipeline/SerialDifferencingAggregationTest.php @@ -0,0 +1,38 @@ +<?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\SerialDifferencingAggregation; + +/** + * Unit test for serial differencing aggregation. + */ +class SerialDifferencingAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $aggregation = new SerialDifferencingAggregation('acme', 'test'); + $aggregation->addParameter('lag', '7'); + + $expected = [ + 'serial_diff' => [ + 'buckets_path' => 'test', + 'lag' => '7' + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +}