Skip to content

Commit

Permalink
Add "keyed" behavior to some aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Nov 23, 2020
1 parent 11ababf commit 43e35c0
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `static_lambda` CS rule [#1870](https://github.com/ruflin/Elastica/pull/1870)
* Added `Elastica\Aggregation\DateRange::setTimezone()` [#1847](https://github.com/ruflin/Elastica/pull/1847)
* Added endpoint options support to `Elastica\Index::create()` [#1859](https://github.com/ruflin/Elastica/pull/1859)
* Added `Elastica\Aggregation\DateHistogram::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876)
* Added `Elastica\Aggregation\GeoDistance::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876)
* Added `Elastica\Aggregation\Histogram::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876)
* Added `Elastica\Aggregation\IpRange::setKeyed()` [#1876](https://github.com/ruflin/Elastica/pull/1876)
### Changed
* Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791)
* Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791)
Expand Down
2 changes: 2 additions & 0 deletions src/Aggregation/GeoDistance.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class GeoDistance extends AbstractAggregation
{
use KeyedTrait;

public const DISTANCE_TYPE_ARC = 'arc';
public const DISTANCE_TYPE_PLANE = 'plane';

Expand Down
2 changes: 2 additions & 0 deletions src/Aggregation/Histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class Histogram extends AbstractSimpleAggregation
{
use KeyedTrait;

/**
* @param string $name the name of this aggregation
* @param string $field the name of the field on which to perform the aggregation
Expand Down
2 changes: 2 additions & 0 deletions src/Aggregation/IpRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class IpRange extends AbstractAggregation
{
use KeyedTrait;

/**
* @param string $name the name of this aggregation
* @param string $field the field on which to perform this aggregation
Expand Down
17 changes: 17 additions & 0 deletions src/Aggregation/KeyedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Elastica\Aggregation;

trait KeyedTrait
{
/**
* Setting the keyed flag to true associates a unique string key
* with each bucket and returns the result as a hash rather than an array.
*
* @return $this
*/
public function setKeyed(bool $keyed = true): self
{
return $this->setParam('keyed', $keyed);
}
}
14 changes: 2 additions & 12 deletions src/Aggregation/Percentiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class Percentiles extends AbstractSimpleAggregation
{
use KeyedTrait;

/**
* @param string $name the name of this aggregation
* @param string $field the field on which to perform this aggregation
Expand Down Expand Up @@ -46,18 +48,6 @@ public function setHdr(string $key, float $value): self
return $this->setParam('hdr', $compression);
}

/**
* the keyed flag is set to true which associates a unique string
* key with each bucket and returns the ranges as a hash
* rather than an array.
*
* @return $this
*/
public function setKeyed(bool $keyed = true): self
{
return $this->setParam('keyed', $keyed);
}

/**
* Set which percents must be returned.
*
Expand Down
10 changes: 2 additions & 8 deletions src/Aggregation/PercentilesBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class PercentilesBucket extends AbstractAggregation
{
use KeyedTrait;

/**
* @param string $name the name of this aggregation
* @param string|null $bucketsPath the field on which to perform this aggregation
Expand Down Expand Up @@ -69,12 +71,4 @@ public function setPercents(array $percents): self
{
return $this->setParam('percents', $percents);
}

/**
* Set keyed flag to return the range as an hash instead of an array of key-value pairs.
*/
public function setKeyed(bool $keyed): self
{
return $this->setParam('keyed', $keyed);
}
}
12 changes: 2 additions & 10 deletions src/Aggregation/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class Range extends AbstractSimpleAggregation
{
use KeyedTrait;

/**
* Add a range to this aggregation.
*
Expand Down Expand Up @@ -45,16 +47,6 @@ public function addRange($fromValue = null, $toValue = null, ?string $key = null
return $this->addParam('ranges', $range);
}

/**
* If set to true, a unique string key will be associated with each bucket, and ranges will be returned as an associative array.
*
* @return $this
*/
public function setKeyed(bool $keyed): self
{
return $this->setParam('keyed', $keyed);
}

/**
* @return $this
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Aggregation/DateHistogramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ public function testDateHistogramAggregation(): void
$this->assertEquals(1, $nonDocCount);
}

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

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

$expected = [
'2014-01-29T00:00:00.000Z',
'2014-01-29T01:00:00.000Z',
'2014-01-29T02:00:00.000Z',
'2014-01-29T03:00:00.000Z',
];
$this->assertSame($expected, \array_keys($results['buckets']));
}

/**
* @group unit
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/Aggregation/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ public function testDateRangeAggregation(): void
}
}

/**
* @group functional
*/
public function testDateRangeKeyedAggregation(): void
{
$agg = new DateRange('date');
$agg->setField('created');
$agg->setKeyed();
$agg->addRange(1390958535000)->addRange(null, 1390958535000);

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

$expected = [
'*-1390958535000',
'1390958535000-*',
];
$this->assertSame($expected, \array_keys($results['buckets']));
}

/**
* @group functional
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Aggregation/GeoDistanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public function testGeoDistanceAggregation(): void
$this->assertEquals(2, $results['buckets'][0]['doc_count']);
}

/**
* @group functional
*/
public function testGeoDistanceKeyedAggregation(): void
{
$agg = new GeoDistance('geo', 'location', ['lat' => 32.804654, 'lon' => -117.242594]);
$agg->addRange(null, 100);
$agg->setKeyed();
$agg->setUnit('mi');

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

$expected = [
'*-100.0',
];
$this->assertSame($expected, \array_keys($results['buckets']));
}

protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();
Expand Down
23 changes: 23 additions & 0 deletions tests/Aggregation/HistogramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ public function testHistogramAggregation(): void
$this->assertEquals(2, $buckets[3]['doc_count']);
}

/**
* @group functional
*/
public function testHistogramKeyedAggregation(): void
{
$agg = new Histogram('hist', 'price', 10);
$agg->setMinimumDocumentCount(0); // should return empty buckets
$agg->setKeyed();

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

$expected = [
'0.0',
'10.0',
'20.0',
'30.0',
'40.0',
];
$this->assertSame($expected, \array_keys($results['buckets']));
}

protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();
Expand Down
25 changes: 25 additions & 0 deletions tests/Aggregation/IpRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ public function testIpRangeAggregation(): void
}
}

/**
* @group functional
*/
public function testIpRangeKeyedAggregation(): void
{
$agg = new IpRange('ip', 'address');
$agg->addRange('192.168.1.101');
$agg->addRange(null, '192.168.1.200');
$agg->setKeyed();

$cidrRange = '192.168.1.0/24';
$agg->addMaskRange($cidrRange);

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

$expected = [
'*-192.168.1.200',
'192.168.1.0/24',
'192.168.1.101-*',
];
$this->assertSame($expected, \array_keys($results['buckets']));
}

protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();
Expand Down
20 changes: 20 additions & 0 deletions tests/Aggregation/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ public function testRangeAggregation(): void
$this->assertEquals(2, $results['buckets'][0]['doc_count']);
}

/**
* @group functional
*/
public function testRangeKeyedAggregation(): void
{
$agg = new Range('range');
$agg->setField('price');
$agg->addRange(1.5, 5);
$agg->setKeyed();

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

$expected = [
'1.5-5.0',
];
$this->assertSame($expected, \array_keys($results['buckets']));
}

/**
* @group unit
*/
Expand Down

0 comments on commit 43e35c0

Please sign in to comment.