-
Notifications
You must be signed in to change notification settings - Fork 733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Extended Stats Bucket #1756
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
714756d
add Extended Stats Bucket
nicolassing 3e58dae
remove useless documentation
nicolassing f68022a
add function return type
nicolassing 3be8893
add doc
nicolassing 9cec59b
Merge remote-tracking branch 'upstream/master'
nicolassing 512a2f1
CS
nicolassing dd8dbc1
fix method visibility
nicolassing 848bd8c
make bucketsPath parameter mandatory in constructor
nicolassing 984ac60
update changelog
nicolassing 2f51705
remove control on bucketsPath param. Its required in the constructor.
nicolassing 00ac03a
fix tests
nicolassing a4038f6
remove useless condition
nicolassing 5821d84
CS
nicolassing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Elastica\Aggregation; | ||
|
||
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 | ||
{ | ||
public function __construct(string $name, string $bucketsPath) | ||
{ | ||
parent::__construct($name); | ||
|
||
if (null !== $bucketsPath) { | ||
$this->setBucketsPath($bucketsPath); | ||
} | ||
} | ||
|
||
public function setBucketsPath(string $bucketsPath): self | ||
{ | ||
return $this->setParam('buckets_path', $bucketsPath); | ||
} | ||
|
||
public function setGapPolicy(string $gapPolicy): self | ||
{ | ||
return $this->setParam('gap_policy', $gapPolicy); | ||
} | ||
|
||
public function setFormat(string $format): self | ||
{ | ||
return $this->setParam('format', $format); | ||
} | ||
|
||
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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes ! I was correcting it ^^ |
||
throw new InvalidException('Buckets path is required'); | ||
} | ||
|
||
return parent::toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Elastica\Test\Aggregation; | ||
|
||
use Elastica\Aggregation\ExtendedStatsBucket; | ||
use Elastica\Aggregation\Histogram; | ||
use Elastica\Aggregation\Max; | ||
use Elastica\Document; | ||
use Elastica\Exception\InvalidException; | ||
use Elastica\Index; | ||
use Elastica\Query; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ExtendedStatsBucketTest extends BaseAggregationTest | ||
{ | ||
/** | ||
* @group functional | ||
*/ | ||
public function testExtendedStatBucketAggregation(): void | ||
{ | ||
$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(): void | ||
{ | ||
$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(): void | ||
{ | ||
$this->expectException(InvalidException::class); | ||
|
||
$serialDiffAgg = new ExtendedStatsBucket('bucket_part'); | ||
$serialDiffAgg->toArray(); | ||
} | ||
|
||
private 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the if condition is now always
true