Skip to content

Commit

Permalink
Aggregation auto_date_histogram (#2051)
Browse files Browse the repository at this point in the history

Co-authored-by: a.navrotsky@educonlines.com <a.navrotsky@educonlines.com>
  • Loading branch information
andriinavrotskii and a.navrotsky@educonlines.com authored Jan 25, 2022
1 parent 4380afd commit 96043fa
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Backward Compatibility Breaks
### Added
* Added support for `symfony/deprecation-contracts` 3.0 by @rguennichi [#2047](https://github.com/ruflin/Elastica/pull/2047)
* Added aggregation `auto_date_histogram` @andriinavrotskii [#2051](https://github.com/ruflin/Elastica/pull/2051)
### Changed
### Deprecated
### Removed
Expand Down
60 changes: 60 additions & 0 deletions src/Aggregation/AutoDateHistogram.php
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);
}
}
48 changes: 48 additions & 0 deletions tests/Aggregation/AutoDateHistogramTest.php
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']
);
}
}

0 comments on commit 96043fa

Please sign in to comment.