Skip to content

Commit

Permalink
Deprecate using "interval" configuration for histograms aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Nov 23, 2020
1 parent a323450 commit 05805f5
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -45,6 +45,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Deprecated Match query class and introduced MatchQuery instead for PHP 8.0 compatibility reason [#1799](https://github.com/ruflin/Elastica/pull/1799)
* Deprecated `version`/`version_type` options [(deprecated in `6.7.0`)](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update.html) and added `if_seq_no` / `if_primary_term` that replaced it
* Deprecated passing `bool` or `null` as 2nd argument to `Elastica\Index::create()` [#1828](https://github.com/ruflin/Elastica/pull/1828)
* Deprecated `Elastica\Aggregation\Histogram::__construct` third argument (`$interval`) and `Elastica\Aggregation\Histogram::setInterval` in favor of `setFixedInterval()` and `setCalendarInterval()` [#1874](https://github.com/ruflin/Elastica/pull/1874)
* Deprecated `Elastica\Aggregation\DateHistogram::__construct` third argument (`$interval`) and `Elastica\Aggregation\DateHistogram::setInterval` in favor of `setFixedInterval()` and `setCalendarInterval()` [#1874](https://github.com/ruflin/Elastica/pull/1874)
### Removed
* Removed HHVM proxy detection [#1818](https://github.com/ruflin/Elastica/pull/1818)
### Fixed
Expand Down
35 changes: 33 additions & 2 deletions src/Aggregation/Histogram.php
Expand Up @@ -14,11 +14,16 @@ class Histogram extends AbstractSimpleAggregation
* @param string $field the name of the field on which to perform the aggregation
* @param int|string $interval the interval by which documents will be bucketed
*/
public function __construct(string $name, string $field, $interval)
public function __construct(string $name, string $field, $interval = null)
{
parent::__construct($name);
$this->setField($field);
$this->setInterval($interval);

if (null !== $interval) {
trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s::__construct" third argument is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.', __CLASS__);

$this->setParam('interval', $interval);
}
}

/**
Expand All @@ -30,9 +35,35 @@ public function __construct(string $name, string $field, $interval)
*/
public function setInterval($interval): self
{
trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.', __METHOD__);

return $this->setParam('interval', $interval);
}

/**
* Set the fixed interval by which documents will be bucketed.
*
* @param int|string $interval
*
* @return $this
*/
public function setFixedInterval($interval): self
{
return $this->setParam('fixed_interval', $interval);
}

/**
* Set the calendar interval by which documents will be bucketed.
*
* @param int|string $interval
*
* @return $this
*/
public function setCalendarInterval($interval): self
{
return $this->setParam('calendar_interval', $interval);
}

/**
* Set the bucket sort order.
*
Expand Down
6 changes: 3 additions & 3 deletions src/QueryBuilder/DSL/Aggregation.php
Expand Up @@ -354,9 +354,9 @@ public function ipv4_range(string $name, string $field): IpRange
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
*
* @param string $name the name of this aggregation
* @param string $field the name of the field on which to perform the aggregation
* @param int $interval the interval by which documents will be bucketed
* @param string $name the name of this aggregation
* @param string $field the name of the field on which to perform the aggregation
* @param int|string $interval the interval by which documents will be bucketed
*/
public function histogram(string $name, string $field, $interval): Histogram
{
Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/BucketScriptTest.php
Expand Up @@ -29,7 +29,8 @@ public function testBucketScriptAggregation(): void
'params.dividend / params.divisor'
);

$histogramAggregation = new Histogram('age_groups', 'age', 10);
$histogramAggregation = new Histogram('age_groups', 'age');
$histogramAggregation->setFixedInterval(10);

$histogramAggregation
->addAggregation((new Max('max_weight'))->setField('weight'))
Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/BucketSelectorTest.php
Expand Up @@ -48,7 +48,8 @@ public function testMaxAggregation(): void
{
$index = $this->_getIndexForTest();

$dateHistogramAgg = new DateHistogram('histogram_agg', 'date', 'day');
$dateHistogramAgg = new DateHistogram('histogram_agg', 'date');
$dateHistogramAgg->setFixedInterval('day');
$dateHistogramAgg->setFormat('yyyy-MM-dd');

$maxAgg = new Max('max_agg');
Expand Down
39 changes: 35 additions & 4 deletions tests/Aggregation/DateHistogramTest.php
Expand Up @@ -18,7 +18,35 @@ class DateHistogramTest extends BaseAggregationTest
*/
public function testDateHistogramAggregation(): void
{
$agg = new DateHistogram('hist', 'created', '1h');
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1h');

$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist');

$docCount = 0;
$nonDocCount = 0;
foreach ($results['buckets'] as $bucket) {
if (1 == $bucket['doc_count']) {
++$docCount;
} else {
++$nonDocCount;
}
}
// 3 Documents that were added
$this->assertEquals(3, $docCount);
// 1 document that was generated in between for the missing hour
$this->assertEquals(1, $nonDocCount);
}

/**
* @group functional
*/
public function testDateHistogramCalendarAggregation(): void
{
$agg = new DateHistogram('hist', 'created');
$agg->setCalendarInterval('1h');

$query = new Query();
$query->addAggregation($agg);
Expand All @@ -44,7 +72,8 @@ public function testDateHistogramAggregation(): void
*/
public function testSetOffset(): void
{
$agg = new DateHistogram('hist', 'created', '1h');
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1h');

$agg->setOffset('3m');

Expand All @@ -68,7 +97,8 @@ public function testSetOffsetWorks(): void
{
$this->_checkVersion('1.5');

$agg = new DateHistogram('hist', 'created', '1m');
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1m');
$agg->setOffset('+40s');

$query = new Query();
Expand All @@ -83,7 +113,8 @@ public function testSetOffsetWorks(): void
*/
public function testSetTimezone(): void
{
$agg = new DateHistogram('hist', 'created', '1h');
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1h');

$agg->setTimezone('-02:30');

Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/DerivativeTest.php
Expand Up @@ -47,7 +47,8 @@ public function testMaxAggregation(): void
{
$index = $this->_getIndexForTest();

$dateHistogramAgg = new DateHistogram('histogram_agg', 'date', 'day');
$dateHistogramAgg = new DateHistogram('histogram_agg', 'date');
$dateHistogramAgg->setFixedInterval('day');
$dateHistogramAgg->setFormat('yyyy-MM-dd');

$maxAgg = new Max('max_agg');
Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/ExtendedStatsBucketTest.php
Expand Up @@ -21,7 +21,8 @@ public function testExtendedStatBucketAggregation(): void
{
$bucketScriptAggregation = new ExtendedStatsBucket('result', 'age_groups>max_weight');

$histogramAggregation = new Histogram('age_groups', 'age', 10);
$histogramAggregation = new Histogram('age_groups', 'age');
$histogramAggregation->setFixedInterval(10);

$histogramAggregation->addAggregation((new Max('max_weight'))->setField('weight'));

Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/HistogramTest.php
Expand Up @@ -17,7 +17,8 @@ class HistogramTest extends BaseAggregationTest
*/
public function testHistogramAggregation(): void
{
$agg = new Histogram('hist', 'price', 10);
$agg = new Histogram('hist', 'price');
$agg->setFixedInterval(10);
$agg->setMinimumDocumentCount(0); // should return empty buckets

$query = new Query();
Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/SerialDiffTest.php
Expand Up @@ -20,7 +20,8 @@ class SerialDiffTest extends BaseAggregationTest
*/
public function testSerialDiffAggregation(): void
{
$dateHistogramAggregation = new DateHistogram('measurements', 'measured_at', 'hour');
$dateHistogramAggregation = new DateHistogram('measurements', 'measured_at');
$dateHistogramAggregation->setFixedInterval('hour');

$dateHistogramAggregation
->addAggregation((new Max('max_value'))->setField('value'))
Expand Down
3 changes: 2 additions & 1 deletion tests/Aggregation/StatsBucketTest.php
Expand Up @@ -21,7 +21,8 @@ public function testStatBucketAggregation(): void
{
$bucketScriptAggregation = new StatsBucket('result', 'age_groups>max_weight');

$histogramAggregation = new Histogram('age_groups', 'age', 10);
$histogramAggregation = new Histogram('age_groups', 'age');
$histogramAggregation->setFixedInterval(10);

$histogramAggregation->addAggregation((new Max('max_weight'))->setField('weight'));

Expand Down

0 comments on commit 05805f5

Please sign in to comment.