From 714756d55968c1ce4a27bc071dc43ac9e667fbb8 Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Tue, 11 Feb 2020 18:22:35 +0100 Subject: [PATCH 01/12] add Extended Stats Bucket --- .../Aggregation/ExtendedStatsBucket.php | 74 +++++++++++++++ .../Aggregation/ExtendedStatsBucketTest.php | 90 +++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 lib/Elastica/Aggregation/ExtendedStatsBucket.php create mode 100644 test/Elastica/Aggregation/ExtendedStatsBucketTest.php diff --git a/lib/Elastica/Aggregation/ExtendedStatsBucket.php b/lib/Elastica/Aggregation/ExtendedStatsBucket.php new file mode 100644 index 0000000000..4230fa3ca0 --- /dev/null +++ b/lib/Elastica/Aggregation/ExtendedStatsBucket.php @@ -0,0 +1,74 @@ +setBucketsPath($bucketsPath); + } + } + + /** + * Set the buckets_path for this aggregation. + * + * @return $this + */ + public function setBucketsPath(string $bucketsPath): self + { + return $this->setParam('buckets_path', $bucketsPath); + } + + /** + * Set the gap policy for this aggregation. + * + * @return $this + */ + public function setGapPolicy(string $gapPolicy): self + { + return $this->setParam('gap_policy', $gapPolicy); + } + + /** + * Set the format for this aggregation. + * + * @return $this + */ + public function setFormat(string $format): self + { + return $this->setParam('format', $format); + } + + /** + * Set the number of standard deviations for this aggregation. + * + * @return $this + */ + public function setSigma(int $sigma): self + { + return $this->setParam('sigma', $sigma); + } + + /** + * @throws InvalidException If buckets path or script is not set + */ + public function toArray(): array + { + if (!$this->hasParam('buckets_path')) { + throw new InvalidException('Buckets path is required'); + } + + return parent::toArray(); + } +} diff --git a/test/Elastica/Aggregation/ExtendedStatsBucketTest.php b/test/Elastica/Aggregation/ExtendedStatsBucketTest.php new file mode 100644 index 0000000000..2805055f76 --- /dev/null +++ b/test/Elastica/Aggregation/ExtendedStatsBucketTest.php @@ -0,0 +1,90 @@ +_createIndex(); + + $index->addDocuments([ + Document::create(['weight' => 60, 'height' => 180, 'age' => 25]), + Document::create(['weight' => 70, 'height' => 156, 'age' => 32]), + Document::create(['weight' => 50, 'height' => 155, 'age' => 45]), + ]); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testExtendedStatBucketAggregation() + { + $bucketScriptAggregation = new ExtendedStatsBucket('result', 'age_groups>max_weight'); + + $histogramAggregation = new Histogram('age_groups', 'age', 10); + + $histogramAggregation->addAggregation((new Max('max_weight'))->setField('weight')); + + $query = Query::create([]) + ->addAggregation($histogramAggregation) + ->addAggregation($bucketScriptAggregation); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('result'); + + $this->assertEquals(3, $results['count']); + $this->assertEquals(50, $results['min']); + $this->assertEquals(70, $results['max']); + $this->assertEquals(60, $results['avg']); + $this->assertEquals(180, $results['sum']); + $this->assertEquals(11000, $results['sum_of_squares']); + $this->assertEquals(66.66666666666667, $results['variance']); + $this->assertEquals(8.16496580927726, $results['std_deviation']); + $this->assertEquals(['upper' => 76.32993161855453, 'lower' => 43.670068381445475], $results['std_deviation_bounds']); + } + + /** + * @group unit + */ + public function testConstructThroughSetters() + { + $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); + + $serialDiffAgg + ->setBucketsPath('age_groups>max_weight') + ->setFormat('test_format') + ->setGapPolicy(10); + + $expected = [ + 'extended_stats_bucket' => [ + 'buckets_path' => 'age_groups>max_weight', + 'format' => 'test_format', + 'gap_policy' => 10, + ], + ]; + + $this->assertEquals($expected, $serialDiffAgg->toArray()); + } + + /** + * @group unit + */ + public function testToArrayInvalidBucketsPath() + { + $this->expectException(\Elastica\Exception\InvalidException::class); + + $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); + $serialDiffAgg->toArray(); + } +} From 3e58dae285eb043c3bd0af2d35aa143c74d9e2ff Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 13 Feb 2020 15:23:46 +0100 Subject: [PATCH 02/12] remove useless documentation --- .../Aggregation/ExtendedStatsBucket.php | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/lib/Elastica/Aggregation/ExtendedStatsBucket.php b/lib/Elastica/Aggregation/ExtendedStatsBucket.php index 4230fa3ca0..cca4cbd1db 100644 --- a/lib/Elastica/Aggregation/ExtendedStatsBucket.php +++ b/lib/Elastica/Aggregation/ExtendedStatsBucket.php @@ -5,8 +5,6 @@ use Elastica\Exception\InvalidException; /** - * Class ExtendedStatsBucket. - * * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-extended-stats-bucket-aggregation.html */ class ExtendedStatsBucket extends AbstractAggregation @@ -20,41 +18,21 @@ public function __construct(string $name, ?string $bucketsPath = null) } } - /** - * Set the buckets_path for this aggregation. - * - * @return $this - */ public function setBucketsPath(string $bucketsPath): self { return $this->setParam('buckets_path', $bucketsPath); } - /** - * Set the gap policy for this aggregation. - * - * @return $this - */ public function setGapPolicy(string $gapPolicy): self { return $this->setParam('gap_policy', $gapPolicy); } - /** - * Set the format for this aggregation. - * - * @return $this - */ public function setFormat(string $format): self { return $this->setParam('format', $format); } - /** - * Set the number of standard deviations for this aggregation. - * - * @return $this - */ public function setSigma(int $sigma): self { return $this->setParam('sigma', $sigma); From f68022a40fad5d322596ffdc90b0d19f461c4074 Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 13 Feb 2020 15:24:10 +0100 Subject: [PATCH 03/12] add function return type --- test/Elastica/Aggregation/ExtendedStatsBucketTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/Elastica/Aggregation/ExtendedStatsBucketTest.php b/test/Elastica/Aggregation/ExtendedStatsBucketTest.php index 2805055f76..686404f3f8 100644 --- a/test/Elastica/Aggregation/ExtendedStatsBucketTest.php +++ b/test/Elastica/Aggregation/ExtendedStatsBucketTest.php @@ -8,6 +8,7 @@ use Elastica\Document; use Elastica\Index; use Elastica\Query; +use Elastica\Exception\InvalidException; class ExtendedStatsBucketTest extends BaseAggregationTest { @@ -29,7 +30,7 @@ protected function _getIndexForTest(): Index /** * @group functional */ - public function testExtendedStatBucketAggregation() + public function testExtendedStatBucketAggregation(): void { $bucketScriptAggregation = new ExtendedStatsBucket('result', 'age_groups>max_weight'); @@ -57,7 +58,7 @@ public function testExtendedStatBucketAggregation() /** * @group unit */ - public function testConstructThroughSetters() + public function testConstructThroughSetters(): void { $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); @@ -80,9 +81,9 @@ public function testConstructThroughSetters() /** * @group unit */ - public function testToArrayInvalidBucketsPath() + public function testToArrayInvalidBucketsPath(): void { - $this->expectException(\Elastica\Exception\InvalidException::class); + $this->expectException(InvalidException::class); $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); $serialDiffAgg->toArray(); From 3be8893d60cdbe755b8e7298ea4bc1463c0ca3cc Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 13 Feb 2020 16:26:26 +0100 Subject: [PATCH 04/12] add doc --- lib/Elastica/Aggregation/ExtendedStatsBucket.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Elastica/Aggregation/ExtendedStatsBucket.php b/lib/Elastica/Aggregation/ExtendedStatsBucket.php index cca4cbd1db..6804ecf3bc 100644 --- a/lib/Elastica/Aggregation/ExtendedStatsBucket.php +++ b/lib/Elastica/Aggregation/ExtendedStatsBucket.php @@ -5,6 +5,8 @@ use Elastica\Exception\InvalidException; /** + * Implements a Extended Stats Bucket Aggregation + * * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-extended-stats-bucket-aggregation.html */ class ExtendedStatsBucket extends AbstractAggregation From 512a2f1c92f6a93081fd896203e060bc11451466 Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Tue, 18 Feb 2020 10:38:31 +0100 Subject: [PATCH 05/12] CS --- src/Aggregation/ExtendedStatsBucket.php | 2 +- tests/Aggregation/ExtendedStatsBucketTest.php | 41 +++++++++++-------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Aggregation/ExtendedStatsBucket.php b/src/Aggregation/ExtendedStatsBucket.php index 6804ecf3bc..2406061cff 100644 --- a/src/Aggregation/ExtendedStatsBucket.php +++ b/src/Aggregation/ExtendedStatsBucket.php @@ -5,7 +5,7 @@ use Elastica\Exception\InvalidException; /** - * Implements a Extended Stats Bucket Aggregation + * Implements a Extended Stats Bucket Aggregation. * * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-extended-stats-bucket-aggregation.html */ diff --git a/tests/Aggregation/ExtendedStatsBucketTest.php b/tests/Aggregation/ExtendedStatsBucketTest.php index 686404f3f8..e467e41c80 100644 --- a/tests/Aggregation/ExtendedStatsBucketTest.php +++ b/tests/Aggregation/ExtendedStatsBucketTest.php @@ -6,27 +6,15 @@ use Elastica\Aggregation\Histogram; use Elastica\Aggregation\Max; use Elastica\Document; +use Elastica\Exception\InvalidException; use Elastica\Index; use Elastica\Query; -use Elastica\Exception\InvalidException; +/** + * @internal + */ class ExtendedStatsBucketTest extends BaseAggregationTest { - protected function _getIndexForTest(): Index - { - $index = $this->_createIndex(); - - $index->addDocuments([ - Document::create(['weight' => 60, 'height' => 180, 'age' => 25]), - Document::create(['weight' => 70, 'height' => 156, 'age' => 32]), - Document::create(['weight' => 50, 'height' => 155, 'age' => 45]), - ]); - - $index->refresh(); - - return $index; - } - /** * @group functional */ @@ -40,7 +28,8 @@ public function testExtendedStatBucketAggregation(): void $query = Query::create([]) ->addAggregation($histogramAggregation) - ->addAggregation($bucketScriptAggregation); + ->addAggregation($bucketScriptAggregation) + ; $results = $this->_getIndexForTest()->search($query)->getAggregation('result'); @@ -65,7 +54,8 @@ public function testConstructThroughSetters(): void $serialDiffAgg ->setBucketsPath('age_groups>max_weight') ->setFormat('test_format') - ->setGapPolicy(10); + ->setGapPolicy(10) + ; $expected = [ 'extended_stats_bucket' => [ @@ -88,4 +78,19 @@ public function testToArrayInvalidBucketsPath(): void $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); $serialDiffAgg->toArray(); } + + protected function _getIndexForTest(): Index + { + $index = $this->_createIndex(); + + $index->addDocuments([ + Document::create(['weight' => 60, 'height' => 180, 'age' => 25]), + Document::create(['weight' => 70, 'height' => 156, 'age' => 32]), + Document::create(['weight' => 50, 'height' => 155, 'age' => 45]), + ]); + + $index->refresh(); + + return $index; + } } From dd8dbc1caa5760a73c2aedc3ecf44b11a550f636 Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Tue, 18 Feb 2020 14:51:01 +0100 Subject: [PATCH 06/12] fix method visibility --- tests/Aggregation/ExtendedStatsBucketTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Aggregation/ExtendedStatsBucketTest.php b/tests/Aggregation/ExtendedStatsBucketTest.php index e467e41c80..cf562cd46f 100644 --- a/tests/Aggregation/ExtendedStatsBucketTest.php +++ b/tests/Aggregation/ExtendedStatsBucketTest.php @@ -79,7 +79,7 @@ public function testToArrayInvalidBucketsPath(): void $serialDiffAgg->toArray(); } - protected function _getIndexForTest(): Index + private function _getIndexForTest(): Index { $index = $this->_createIndex(); From 848bd8c16896b8714867d106171be406f44638da Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 5 Mar 2020 11:15:36 +0100 Subject: [PATCH 07/12] make bucketsPath parameter mandatory in constructor --- src/Aggregation/ExtendedStatsBucket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Aggregation/ExtendedStatsBucket.php b/src/Aggregation/ExtendedStatsBucket.php index 2406061cff..5b4c623522 100644 --- a/src/Aggregation/ExtendedStatsBucket.php +++ b/src/Aggregation/ExtendedStatsBucket.php @@ -11,7 +11,7 @@ */ class ExtendedStatsBucket extends AbstractAggregation { - public function __construct(string $name, ?string $bucketsPath = null) + public function __construct(string $name, string $bucketsPath) { parent::__construct($name); From 984ac609d6e064347de726b09158db00b542a0be Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 5 Mar 2020 11:18:08 +0100 Subject: [PATCH 08/12] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5353374110..8c2c58f647 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added `Elastica\Reindex->setQuery(Elastica\Query\AbstractQuery $query): void` [#1752](https://github.com/ruflin/Elastica/pull/1752) * Added constants `PIPELINE`, `REFRESH_TRUE`, `REFRESH_FALSE`, `REFRESH_WAIT_FOR`, `SLICES` and `SLICES_AUTO` to `Elastica\Reindex` [#1752](https://github.com/ruflin/Elastica/pull/1752) * Added `Elastica\Pipeline->getId(): ?string` [#1752](https://github.com/ruflin/Elastica/pull/1752) +* Added `Elastica\Aggregation\ExtendedStatsBucket` aggregation [#1756](https://github.com/ruflin/Elastica/pull/1756) ### Changed - Updated PHP coding standards to adhere to PSR-12 [#1760](https://github.com/ruflin/Elastica/pull/1760) From 2f51705d726c4b3622dfdf23fbb3dd647d3f77e1 Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 5 Mar 2020 11:57:49 +0100 Subject: [PATCH 09/12] remove control on bucketsPath param. Its required in the constructor. --- src/Aggregation/ExtendedStatsBucket.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Aggregation/ExtendedStatsBucket.php b/src/Aggregation/ExtendedStatsBucket.php index 5b4c623522..6c25173c4f 100644 --- a/src/Aggregation/ExtendedStatsBucket.php +++ b/src/Aggregation/ExtendedStatsBucket.php @@ -39,16 +39,4 @@ public function setSigma(int $sigma): self { return $this->setParam('sigma', $sigma); } - - /** - * @throws InvalidException If buckets path or script is not set - */ - public function toArray(): array - { - if (!$this->hasParam('buckets_path')) { - throw new InvalidException('Buckets path is required'); - } - - return parent::toArray(); - } } From 00ac03a4ba637b4980544761319e31f8a6d2230e Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 5 Mar 2020 11:57:56 +0100 Subject: [PATCH 10/12] fix tests --- tests/Aggregation/ExtendedStatsBucketTest.php | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/tests/Aggregation/ExtendedStatsBucketTest.php b/tests/Aggregation/ExtendedStatsBucketTest.php index cf562cd46f..4b361bf9a7 100644 --- a/tests/Aggregation/ExtendedStatsBucketTest.php +++ b/tests/Aggregation/ExtendedStatsBucketTest.php @@ -47,9 +47,9 @@ public function testExtendedStatBucketAggregation(): void /** * @group unit */ - public function testConstructThroughSetters(): void + public function testOverrideBucketsPathThroughSetters(): void { - $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); + $serialDiffAgg = new ExtendedStatsBucket('bucket_part', 'foobar'); $serialDiffAgg ->setBucketsPath('age_groups>max_weight') @@ -68,17 +68,6 @@ public function testConstructThroughSetters(): void $this->assertEquals($expected, $serialDiffAgg->toArray()); } - /** - * @group unit - */ - public function testToArrayInvalidBucketsPath(): void - { - $this->expectException(InvalidException::class); - - $serialDiffAgg = new ExtendedStatsBucket('bucket_part'); - $serialDiffAgg->toArray(); - } - private function _getIndexForTest(): Index { $index = $this->_createIndex(); From a4038f658b6f43fc414e8560c1f5be4072410131 Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 5 Mar 2020 12:00:14 +0100 Subject: [PATCH 11/12] remove useless condition --- src/Aggregation/ExtendedStatsBucket.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Aggregation/ExtendedStatsBucket.php b/src/Aggregation/ExtendedStatsBucket.php index 6c25173c4f..afa4860c1f 100644 --- a/src/Aggregation/ExtendedStatsBucket.php +++ b/src/Aggregation/ExtendedStatsBucket.php @@ -14,10 +14,7 @@ class ExtendedStatsBucket extends AbstractAggregation public function __construct(string $name, string $bucketsPath) { parent::__construct($name); - - if (null !== $bucketsPath) { - $this->setBucketsPath($bucketsPath); - } + $this->setBucketsPath($bucketsPath); } public function setBucketsPath(string $bucketsPath): self From 5821d84de3d261dd5ca8723300f52e6d3946093a Mon Sep 17 00:00:00 2001 From: Nicolas Assing Date: Thu, 5 Mar 2020 15:11:59 +0100 Subject: [PATCH 12/12] CS --- src/Aggregation/ExtendedStatsBucket.php | 2 -- tests/Aggregation/ExtendedStatsBucketTest.php | 1 - 2 files changed, 3 deletions(-) diff --git a/src/Aggregation/ExtendedStatsBucket.php b/src/Aggregation/ExtendedStatsBucket.php index afa4860c1f..29563e96bf 100644 --- a/src/Aggregation/ExtendedStatsBucket.php +++ b/src/Aggregation/ExtendedStatsBucket.php @@ -2,8 +2,6 @@ namespace Elastica\Aggregation; -use Elastica\Exception\InvalidException; - /** * Implements a Extended Stats Bucket Aggregation. * diff --git a/tests/Aggregation/ExtendedStatsBucketTest.php b/tests/Aggregation/ExtendedStatsBucketTest.php index 4b361bf9a7..d49862d4ad 100644 --- a/tests/Aggregation/ExtendedStatsBucketTest.php +++ b/tests/Aggregation/ExtendedStatsBucketTest.php @@ -6,7 +6,6 @@ use Elastica\Aggregation\Histogram; use Elastica\Aggregation\Max; use Elastica\Document; -use Elastica\Exception\InvalidException; use Elastica\Index; use Elastica\Query;