-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Diversified Sampler aggregation
- Loading branch information
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Elastica\Aggregation; | ||
|
||
/** | ||
* Class DiversifiedSampler. | ||
* | ||
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-diversified-sampler-aggregation.html | ||
*/ | ||
class DiversifiedSampler extends AbstractSimpleAggregation | ||
{ | ||
/** | ||
* Set the number of top-scoring documents to be returned from each shard. | ||
* | ||
* @return $this | ||
*/ | ||
public function setShardSize(int $shardSize): self | ||
{ | ||
return $this->setParam('shard_size', $shardSize); | ||
} | ||
|
||
/** | ||
* Set the maximum number of documents to be returned per value. | ||
* | ||
* @return $this | ||
*/ | ||
public function setMaxDocsPerValue(int $max): self | ||
{ | ||
return $this->setParam('max_docs_per_value', $max); | ||
} | ||
|
||
/** | ||
* Instruct Elasticsearch to use direct field data or ordinals/hashes of the field values to execute this aggregation. | ||
* The execution hint will be ignored if it is not applicable. | ||
* | ||
* @return $this | ||
*/ | ||
public function setExecutionHint(string $hint): self | ||
{ | ||
return $this->setParam('execution_hint', $hint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Elastica\Test\Aggregation; | ||
|
||
use Elastica\Aggregation\DiversifiedSampler; | ||
use Elastica\Aggregation\Sum; | ||
use Elastica\Document; | ||
use Elastica\Index; | ||
use Elastica\Mapping; | ||
use Elastica\Query; | ||
|
||
class DiversifiedSamplerTest extends BaseAggregationTest | ||
{ | ||
protected function _getIndexForTest(): Index | ||
{ | ||
$index = $this->_createIndex(null, true, 2); | ||
|
||
$mapping = new Mapping([ | ||
'price' => ['type' => 'integer'], | ||
'color' => ['type' => 'keyword'], | ||
]); | ||
$index->setMapping($mapping); | ||
|
||
$routing1 = 'first_routing'; | ||
$routing2 = 'second_routing'; | ||
|
||
$index->addDocuments([ | ||
(new Document(1, ['price' => 5, 'color' => 'blue']))->setRouting($routing1), | ||
(new Document(2, ['price' => 8, 'color' => 'blue']))->setRouting($routing1), | ||
(new Document(3, ['price' => 1, 'color' => 'blue']))->setRouting($routing1), | ||
(new Document(4, ['price' => 3, 'color' => 'red']))->setRouting($routing1), | ||
(new Document(5, ['price' => 1.5, 'color' => 'red']))->setRouting($routing1), | ||
(new Document(6, ['price' => 2, 'color' => 'green']))->setRouting($routing1), | ||
(new Document(7, ['price' => 5, 'color' => 'blue']))->setRouting($routing2), | ||
(new Document(8, ['price' => 8, 'color' => 'blue']))->setRouting($routing2), | ||
(new Document(9, ['price' => 1, 'color' => 'red']))->setRouting($routing2), | ||
(new Document(10, ['price' => 3, 'color' => 'red']))->setRouting($routing2), | ||
]); | ||
|
||
$index->refresh(); | ||
|
||
return $index; | ||
} | ||
|
||
/** | ||
* @group unit | ||
*/ | ||
public function testToArray() | ||
{ | ||
$expected = [ | ||
'diversified_sampler' => [ | ||
'field' => 'color', | ||
'shard_size' => 1, | ||
'max_docs_per_value' => 2, | ||
'execution_hint' => 'map', | ||
], | ||
'aggs' => [ | ||
'price_sum' => [ | ||
'sum' => [ | ||
'field' => 'price', | ||
], | ||
], | ||
], | ||
]; | ||
|
||
$agg = new DiversifiedSampler('price_diversified_sampler'); | ||
$agg->setField('color'); | ||
|
||
$agg->setShardSize(1); | ||
$agg->setMaxDocsPerValue(2); | ||
$agg->setExecutionHint('map'); | ||
|
||
$childAgg = new Sum('price_sum'); | ||
$childAgg->setField('price'); | ||
|
||
$agg->addAggregation($childAgg); | ||
|
||
$this->assertEquals($expected, $agg->toArray()); | ||
} | ||
|
||
/** | ||
* @dataProvider shardSizeAndMaxDocPerValueProvider | ||
* @group functional | ||
*/ | ||
public function testSamplerAggregation(int $shardSize, int $maxDocPerValue, int $docCount) | ||
{ | ||
$agg = new DiversifiedSampler('price_diversified_sampler'); | ||
$agg->setField('color'); | ||
|
||
$agg->setShardSize($shardSize); | ||
$agg->setMaxDocsPerValue($maxDocPerValue); | ||
|
||
$childAgg = new Sum('price_sum'); | ||
$childAgg->setField('price'); | ||
|
||
$agg->addAggregation($childAgg); | ||
|
||
$query = new Query(); | ||
$query->addAggregation($agg); | ||
$results = $this->_getIndexForTest()->search($query)->getAggregation('price_diversified_sampler'); | ||
|
||
$this->assertEquals($docCount, $results['doc_count']); | ||
} | ||
|
||
public function shardSizeAndMaxDocPerValueProvider() | ||
{ | ||
return [ | ||
[1, 1, 2], | ||
[2, 1, 4], | ||
[3, 1, 5], | ||
[4, 1, 5], | ||
[1, 2, 2], | ||
[2, 2, 4], | ||
[3, 2, 6], | ||
[4, 2, 8], | ||
[5, 2, 9], | ||
[6, 2, 9], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters