Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Sampler aggregation #1688

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes

### Added
* Added `Sampler` aggregation [#1688](https://github.com/ruflin/Elastica/pull/1688)

### Improvements

Expand Down
23 changes: 23 additions & 0 deletions lib/Elastica/Aggregation/Sampler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Elastica\Aggregation;

/**
* Class Sampler.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html
*/
class Sampler extends AbstractAggregation
{
/**
* Set the number of top-scoring documents to be returned from each shard.
*
* @param int $shardSize
*
* @return $this
*/
public function setShardSize(int $shardSize): self
{
return $this->setParam('shard_size', $shardSize);
}
}
14 changes: 14 additions & 0 deletions lib/Elastica/QueryBuilder/DSL/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Elastica\Aggregation\Percentiles;
use Elastica\Aggregation\Range;
use Elastica\Aggregation\ReverseNested;
use Elastica\Aggregation\Sampler;
use Elastica\Aggregation\ScriptedMetric;
use Elastica\Aggregation\SerialDiff;
use Elastica\Aggregation\SignificantTerms;
Expand Down Expand Up @@ -553,4 +554,17 @@ public function adjacency_matrix(string $name): AdjacencyMatrix
{
return new AdjacencyMatrix($name);
}

/** sampler aggregation.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html
*
* @param string $name
*
* @return Sampler
*/
public function sampler($name): Sampler
{
return new Sampler($name);
}
}
94 changes: 94 additions & 0 deletions test/Elastica/Aggregation/SamplerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\Sampler;
use Elastica\Aggregation\Sum;
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;

class SamplerTest extends BaseAggregationTest
{
protected function _getIndexForTest(): Index
{
$index = $this->_createIndex(null, true, 2);

$routing1 = 'first_routing';
$routing2 = 'second_routing';

$index->addDocuments([
(new Document(1, ['price' => 5]))->setRouting($routing1),
(new Document(2, ['price' => 8]))->setRouting($routing1),
(new Document(3, ['price' => 1]))->setRouting($routing1),
(new Document(4, ['price' => 3]))->setRouting($routing2),
(new Document(5, ['price' => 1.5]))->setRouting($routing2),
]);

$index->refresh();

return $index;
}

/**
* @group unit
*/
public function testToArray()
{
$expected = [
'sampler' => [
'shard_size' => 1,
],
'aggs' => [
'price_sum' => [
'sum' => [
'field' => 'price',
],
],
],
];

$agg = new Sampler('price_sampler');
$agg->setShardSize(1);

$childAgg = new Sum('price_sum');
$childAgg->setField('price');

$agg->addAggregation($childAgg);

$this->assertEquals($expected, $agg->toArray());
}

/**
* @dataProvider shardSizeProvider
* @group functional
*
* @param int $shardSize
* @param int $docCount
*/
public function testSamplerAggregation(int $shardSize, int $docCount)
{
$agg = new Sampler('price_sampler');
$agg->setShardSize($shardSize);

$childAgg = new Sum('price_sum');
$childAgg->setField('price');

$agg->addAggregation($childAgg);

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

$this->assertEquals($docCount, $results['doc_count']);
}

public function shardSizeProvider()
{
return [
[1, 2],
[2, 4],
[3, 5],
];
}
}
1 change: 1 addition & 0 deletions test/Elastica/QueryBuilder/DSL/AggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function testInterface()
$this->_assertImplemented($aggregationDSL, 'avg_bucket', Aggregation\AvgBucket::class, ['name']);
$this->_assertImplemented($aggregationDSL, 'sum_bucket', Aggregation\SumBucket::class, ['name']);
$this->_assertImplemented($aggregationDSL, 'adjacency_matrix', Aggregation\AdjacencyMatrix::class, ['name']);
$this->_assertImplemented($aggregationDSL, 'sampler', Aggregation\Sampler::class, ['name']);

$this->_assertNotImplemented($aggregationDSL, 'children', ['name']);
$this->_assertNotImplemented($aggregationDSL, 'geo_bounds', ['name']);
Expand Down