-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aggregation auto_date_histogram (#2051)
Co-authored-by: a.navrotsky@educonlines.com <a.navrotsky@educonlines.com>
- Loading branch information
1 parent
4380afd
commit 96043fa
Showing
3 changed files
with
109 additions
and
0 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,60 @@ | ||
<?php | ||
|
||
namespace Elastica\Aggregation; | ||
|
||
use Elastica\Aggregation\Traits\MissingTrait; | ||
|
||
class AutoDateHistogram extends AbstractSimpleAggregation | ||
{ | ||
use MissingTrait; | ||
|
||
public function __construct(string $name, string $field) | ||
{ | ||
parent::__construct($name); | ||
$this->setField($field); | ||
} | ||
|
||
/** | ||
* A target number of buckets. | ||
* The buckets field is optional, and will default to 10 buckets if not specified. | ||
* | ||
* @return $this | ||
*/ | ||
public function setBuckets(int $buckets): self | ||
{ | ||
return $this->setParam('buckets', $buckets); | ||
} | ||
|
||
/** | ||
* Set the format for this aggregation. | ||
* If no format is specified, then it will use the first date format specified in the field mapping. | ||
* | ||
* @return $this | ||
*/ | ||
public function setFormat(string $format): self | ||
{ | ||
return $this->setParam('format', $format); | ||
} | ||
|
||
/** | ||
* Set time_zone option. | ||
* The time_zone parameter can be used to indicate that bucketing should use a different time zone. | ||
* | ||
* @return $this | ||
*/ | ||
public function setTimezone(string $timezone): self | ||
{ | ||
return $this->setParam('time_zone', $timezone); | ||
} | ||
|
||
/** | ||
* The minimum_interval allows the caller to specify the minimum rounding interval that should be used. | ||
* The accepted units: year, month, day, hour, minute, second. | ||
* | ||
* @return $this | ||
*/ | ||
public function setMinimumInterval(string $minimumInterval): self | ||
{ | ||
return $this->setParam('minimum_interval', $minimumInterval); | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
namespace Elastica\Test\Aggregation; | ||
|
||
use Elastica\Aggregation\AutoDateHistogram; | ||
use Elastica\Query; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class AutoDateHistogramTest extends BaseAggregationTest | ||
{ | ||
/** | ||
* @group unit | ||
*/ | ||
public function testAvgBucketAggregation(): void | ||
{ | ||
$aggregationKey = 'aggs'; | ||
$aggregationType = 'auto_date_histogram'; | ||
$aggregationName = 'aggregation_name'; | ||
$fieldName = 'field_name'; | ||
$format = 'yyyy-MM-dd'; | ||
$timeZone = '-01:00'; | ||
$minimumInterval = 'minute'; | ||
|
||
$aggregation = (new AutoDateHistogram('aggregation_name', 'field_name')) | ||
->setBuckets(10) | ||
->setFormat('yyyy-MM-dd') | ||
->setTimezone('-01:00') | ||
->setMinimumInterval('minute') | ||
; | ||
|
||
$query = Query::create([])->addAggregation($aggregation); | ||
|
||
$queryArray = $query->toArray(); | ||
$this->assertTrue(isset($queryArray[$aggregationKey][$aggregationName][$aggregationType]['field'])); | ||
$this->assertEquals($fieldName, $queryArray[$aggregationKey][$aggregationName][$aggregationType]['field']); | ||
$this->assertTrue(isset($queryArray[$aggregationKey][$aggregationName][$aggregationType]['format'])); | ||
$this->assertEquals($format, $queryArray[$aggregationKey][$aggregationName][$aggregationType]['format']); | ||
$this->assertTrue(isset($queryArray[$aggregationKey][$aggregationName][$aggregationType]['time_zone'])); | ||
$this->assertEquals($timeZone, $queryArray[$aggregationKey][$aggregationName][$aggregationType]['time_zone']); | ||
$this->assertTrue(isset($queryArray[$aggregationKey][$aggregationName][$aggregationType]['minimum_interval'])); | ||
$this->assertEquals( | ||
$minimumInterval, | ||
$queryArray[$aggregationKey][$aggregationName][$aggregationType]['minimum_interval'] | ||
); | ||
} | ||
} |