Skip to content

Commit 11427ae

Browse files
committed
Add the Sampler aggregation
1 parent ec541e1 commit 11427ae

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file based on the
88
### Bugfixes
99

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

1213
### Improvements
1314

lib/Elastica/Aggregation/Sampler.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Elastica\Aggregation;
4+
5+
/**
6+
* Class Sampler.
7+
*
8+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html
9+
*/
10+
class Sampler extends AbstractAggregation
11+
{
12+
/**
13+
* Set the number of top-scoring documents to be returned from each shard.
14+
*
15+
* @param int $shardSize
16+
*
17+
* @return $this
18+
*/
19+
public function setShardSize(int $shardSize): self
20+
{
21+
return $this->setParam('shard_size', $shardSize);
22+
}
23+
}

lib/Elastica/QueryBuilder/DSL/Aggregation.php

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Elastica\Aggregation\Percentiles;
2525
use Elastica\Aggregation\Range;
2626
use Elastica\Aggregation\ReverseNested;
27+
use Elastica\Aggregation\Sampler;
2728
use Elastica\Aggregation\ScriptedMetric;
2829
use Elastica\Aggregation\SerialDiff;
2930
use Elastica\Aggregation\SignificantTerms;
@@ -553,4 +554,17 @@ public function adjacency_matrix(string $name): AdjacencyMatrix
553554
{
554555
return new AdjacencyMatrix($name);
555556
}
557+
558+
/** sampler aggregation.
559+
*
560+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html
561+
*
562+
* @param string $name
563+
*
564+
* @return Sampler
565+
*/
566+
public function sampler($name): Sampler
567+
{
568+
return new Sampler($name);
569+
}
556570
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Elastica\Test\Aggregation;
4+
5+
use Elastica\Aggregation\Sampler;
6+
use Elastica\Aggregation\Sum;
7+
use Elastica\Document;
8+
use Elastica\Index;
9+
use Elastica\Query;
10+
11+
class SamplerTest extends BaseAggregationTest
12+
{
13+
protected function _getIndexForTest(): Index
14+
{
15+
$index = $this->_createIndex(null, true, 2);
16+
17+
$routing1 = 'first_routing';
18+
$routing2 = 'second_routing';
19+
20+
$index->addDocuments([
21+
(new Document(1, ['price' => 5]))->setRouting($routing1),
22+
(new Document(2, ['price' => 8]))->setRouting($routing1),
23+
(new Document(3, ['price' => 1]))->setRouting($routing1),
24+
(new Document(4, ['price' => 3]))->setRouting($routing2),
25+
(new Document(5, ['price' => 1.5]))->setRouting($routing2),
26+
]);
27+
28+
$index->refresh();
29+
30+
return $index;
31+
}
32+
33+
/**
34+
* @group unit
35+
*/
36+
public function testToArray()
37+
{
38+
$expected = [
39+
'sampler' => [
40+
'shard_size' => 1,
41+
],
42+
'aggs' => [
43+
'price_sum' => [
44+
'sum' => [
45+
'field' => 'price',
46+
],
47+
],
48+
],
49+
];
50+
51+
$agg = new Sampler('price_sampler');
52+
$agg->setShardSize(1);
53+
54+
$childAgg = new Sum('price_sum');
55+
$childAgg->setField('price');
56+
57+
$agg->addAggregation($childAgg);
58+
59+
$this->assertEquals($expected, $agg->toArray());
60+
}
61+
62+
/**
63+
* @dataProvider shardSizeProvider
64+
* @group functional
65+
*
66+
* @param int $shardSize
67+
* @param int $docCount
68+
*/
69+
public function testSamplerAggregation(int $shardSize, int $docCount)
70+
{
71+
$agg = new Sampler('price_sampler');
72+
$agg->setShardSize($shardSize);
73+
74+
$childAgg = new Sum('price_sum');
75+
$childAgg->setField('price');
76+
77+
$agg->addAggregation($childAgg);
78+
79+
$query = new Query();
80+
$query->addAggregation($agg);
81+
$results = $this->_getIndexForTest()->search($query)->getAggregation('price_sampler');
82+
83+
$this->assertEquals($docCount, $results['doc_count']);
84+
}
85+
86+
public function shardSizeProvider()
87+
{
88+
return [
89+
[1, 2],
90+
[2, 4],
91+
[3, 5],
92+
];
93+
}
94+
}

test/Elastica/QueryBuilder/DSL/AggregationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function testInterface()
5757
$this->_assertImplemented($aggregationDSL, 'avg_bucket', Aggregation\AvgBucket::class, ['name']);
5858
$this->_assertImplemented($aggregationDSL, 'sum_bucket', Aggregation\SumBucket::class, ['name']);
5959
$this->_assertImplemented($aggregationDSL, 'adjacency_matrix', Aggregation\AdjacencyMatrix::class, ['name']);
60+
$this->_assertImplemented($aggregationDSL, 'sampler', Aggregation\Sampler::class, ['name']);
6061

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

0 commit comments

Comments
 (0)