-
Notifications
You must be signed in to change notification settings - Fork 732
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add avg_bucket & sum_bucket to aggregations
- Loading branch information
Josselin Henrot
committed
Jan 26, 2018
1 parent
a00e6b7
commit 68d81e0
Showing
7 changed files
with
361 additions
and
2 deletions.
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,75 @@ | ||
<?php | ||
namespace Elastica\Aggregation; | ||
|
||
use Elastica\Exception\InvalidException; | ||
|
||
/** | ||
* Class AvgBucket. | ||
* | ||
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html | ||
*/ | ||
class AvgBucket extends AbstractAggregation | ||
{ | ||
/** | ||
* @param string $name | ||
* @param array|null $bucketsPath | ||
*/ | ||
public function __construct($name, $bucketsPath = null) | ||
{ | ||
parent::__construct($name); | ||
|
||
if ($bucketsPath !== null) { | ||
$this->setBucketsPath($bucketsPath); | ||
} | ||
} | ||
|
||
/** | ||
* Set the buckets_path for this aggregation. | ||
* | ||
* @param string $bucketsPath | ||
* | ||
* @return $this | ||
*/ | ||
public function setBucketsPath($bucketsPath) | ||
{ | ||
return $this->setParam('buckets_path', $bucketsPath); | ||
} | ||
|
||
/** | ||
* Set the gap policy for this aggregation. | ||
* | ||
* @param string $gapPolicy | ||
* | ||
* @return $this | ||
*/ | ||
public function setGapPolicy($gapPolicy) | ||
{ | ||
return $this->setParam('gap_policy', $gapPolicy); | ||
} | ||
|
||
/** | ||
* Set the format for this aggregation. | ||
* | ||
* @param string $format | ||
* | ||
* @return $this | ||
*/ | ||
public function setFormat($format) | ||
{ | ||
return $this->setParam('format', $format); | ||
} | ||
|
||
/** | ||
* @throws InvalidException If buckets path or script is not set | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
if (!$this->hasParam('buckets_path')) { | ||
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,75 @@ | ||
<?php | ||
namespace Elastica\Aggregation; | ||
|
||
use Elastica\Exception\InvalidException; | ||
|
||
/** | ||
* Class SumBucket. | ||
* | ||
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html | ||
*/ | ||
class SumBucket extends AbstractAggregation | ||
{ | ||
/** | ||
* @param string $name | ||
* @param array|null $bucketsPath | ||
*/ | ||
public function __construct($name, $bucketsPath = null) | ||
{ | ||
parent::__construct($name); | ||
|
||
if ($bucketsPath !== null) { | ||
$this->setBucketsPath($bucketsPath); | ||
} | ||
} | ||
|
||
/** | ||
* Set the buckets_path for this aggregation. | ||
* | ||
* @param string $bucketsPath | ||
* | ||
* @return $this | ||
*/ | ||
public function setBucketsPath($bucketsPath) | ||
{ | ||
return $this->setParam('buckets_path', $bucketsPath); | ||
} | ||
|
||
/** | ||
* Set the gap policy for this aggregation. | ||
* | ||
* @param string $gapPolicy | ||
* | ||
* @return $this | ||
*/ | ||
public function setGapPolicy($gapPolicy) | ||
{ | ||
return $this->setParam('gap_policy', $gapPolicy); | ||
} | ||
|
||
/** | ||
* Set the format for this aggregation. | ||
* | ||
* @param string $format | ||
* | ||
* @return $this | ||
*/ | ||
public function setFormat($format) | ||
{ | ||
return $this->setParam('format', $format); | ||
} | ||
|
||
/** | ||
* @throws InvalidException If buckets path or script is not set | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
if (!$this->hasParam('buckets_path')) { | ||
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
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,87 @@ | ||
<?php | ||
namespace Elastica\Test\Aggregation; | ||
|
||
use Elastica\Aggregation\AvgBucket; | ||
use Elastica\Aggregation\Avg; | ||
use Elastica\Aggregation\Terms; | ||
use Elastica\Document; | ||
use Elastica\Query; | ||
|
||
class AvgBucketTest extends BaseAggregationTest | ||
{ | ||
protected function _getIndexForTest() | ||
{ | ||
$index = $this->_createIndex(); | ||
|
||
$index->getType('test')->addDocuments([ | ||
Document::create(['page' => 1, 'likes' => 180]), | ||
Document::create(['page' => 1, 'likes' => 156]), | ||
Document::create(['page' => 2, 'likes' => 155]), | ||
]); | ||
|
||
$index->refresh(); | ||
|
||
return $index; | ||
} | ||
|
||
/** | ||
* @group functional | ||
*/ | ||
public function testAvgBucketAggregation() | ||
{ | ||
$this->_checkScriptInlineSetting(); | ||
|
||
$avgBucketAggregation = new AvgBucket( | ||
'avg_likes_by_page', | ||
'pages > avg_likes' | ||
); | ||
|
||
$sumLikes = new Avg('avg_likes'); | ||
$sumLikes->setField('likes'); | ||
|
||
$groupByPage = new Terms('pages'); | ||
$groupByPage | ||
->setField('page') | ||
->setSize(2) | ||
->addAggregation($sumLikes); | ||
|
||
$query = Query::create([])->addAggregation($groupByPage)->addAggregation($avgBucketAggregation); | ||
|
||
$results = $this->_getIndexForTest()->search($query)->getAggregation('avg_likes_by_page'); | ||
|
||
$this->assertEquals(161.5, $results['value']); | ||
} | ||
|
||
/** | ||
* @group unit | ||
*/ | ||
public function testConstructThroughSetters() | ||
{ | ||
$serialDiffAgg = new AvgBucket('avg_bucket'); | ||
|
||
$serialDiffAgg | ||
->setBucketsPath('pages > avg_likes_by_page') | ||
->setFormat('test_format') | ||
->setGapPolicy(10); | ||
|
||
$expected = [ | ||
'avg_bucket' => [ | ||
'buckets_path' => 'pages > avg_likes_by_page', | ||
'format' => 'test_format', | ||
'gap_policy' => 10, | ||
], | ||
]; | ||
|
||
$this->assertEquals($expected, $serialDiffAgg->toArray()); | ||
} | ||
|
||
/** | ||
* @group unit | ||
* @expectedException \Elastica\Exception\InvalidException | ||
*/ | ||
public function testToArrayInvalidBucketsPath() | ||
{ | ||
$serialDiffAgg = new AvgBucket('avg_bucket'); | ||
$serialDiffAgg->toArray(); | ||
} | ||
} |
Oops, something went wrong.