Skip to content

Commit

Permalink
Add sum_bucket & avg_bucket aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
Josselin Henrot committed Jan 24, 2018
1 parent a00e6b7 commit e831f39
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
75 changes: 75 additions & 0 deletions lib/Elastica/Aggregation/AvgBucket.php
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 array $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();
}
}
75 changes: 75 additions & 0 deletions lib/Elastica/Aggregation/SumBucket.php
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 array $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();
}
}
32 changes: 32 additions & 0 deletions lib/Elastica/QueryBuilder/DSL/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Elastica\QueryBuilder\DSL;

use Elastica\Aggregation\Avg;
use Elastica\Aggregation\AvgBucket;
use Elastica\Aggregation\BucketScript;
use Elastica\Aggregation\Cardinality;
use Elastica\Aggregation\DateHistogram;
Expand All @@ -26,6 +27,7 @@
use Elastica\Aggregation\SignificantTerms;
use Elastica\Aggregation\Stats;
use Elastica\Aggregation\Sum;
use Elastica\Aggregation\SumBucket;
use Elastica\Aggregation\Terms;
use Elastica\Aggregation\TopHits;
use Elastica\Aggregation\ValueCount;
Expand Down Expand Up @@ -94,6 +96,21 @@ public function sum($name)
return new Sum($name);
}

/**
* sum bucket aggregation.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html
*
* @param string $name
* @param array|null $bucketsPath
*
* @return Sum
*/
public function sum_bucket($name, $bucketsPath = null)
{
return new SumBucket($name, $bucketsPath);
}

/**
* avg aggregation.
*
Expand All @@ -108,6 +125,21 @@ public function avg($name)
return new Avg($name);
}

/**
* avg bucket aggregation.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html
*
* @param string $name
* @param array|null $bucketsPath
*
* @return Sum
*/
public function avg_bucket($name, $bucketsPath = null)
{
return new AvgBucket($name, $bucketsPath);
}

/**
* stats aggregation.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/Elastica/QueryBuilder/Version/Version240.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class Version240 extends Version
'min',
'max',
'sum',
'sum_bucket',
'avg',
'avg_bucket',
'stats',
'extended_stats',
'value_count',
Expand Down

0 comments on commit e831f39

Please sign in to comment.