diff --git a/DSL/Aggregation/CardinalityAggregation.php b/DSL/Aggregation/CardinalityAggregation.php new file mode 100644 index 00000000..070f77b3 --- /dev/null +++ b/DSL/Aggregation/CardinalityAggregation.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\DSL\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait; + +/** + * Difference values counter. + */ +class CardinalityAggregation extends AbstractAggregation +{ + use MetricTrait; + + /** + * @var int + */ + private $precisionThreshold; + + /** + * @var bool + */ + private $rehash; + + /** + * {@inheritdoc} + */ + public function getArray() + { + if (!$this->getField()) { + return new \stdClass(); + } + + $out['field'] = $this->getField(); + if ($this->getPrecisionThreshold()) { + $out['precision_threshold'] = $this->getPrecisionThreshold(); + } + + if ($this->isRehash() !== null) { + $out['rehash'] = $this->isRehash(); + } + + return $out; + } + + /** + * Precision threshold. + * + * @param int $precision Precision Threshold. + */ + public function setPrecisionThreshold($precision) + { + $this->precisionThreshold = $precision; + } + + /** + * @return int + */ + public function getPrecisionThreshold() + { + return $this->precisionThreshold; + } + + /** + * @return bool + */ + public function isRehash() + { + return $this->rehash; + } + + /** + * @param bool $rehash + */ + public function setRehash($rehash) + { + $this->rehash = $rehash; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'cardinality'; + } +} diff --git a/Tests/Functional/DSL/Aggregation/CardinalityAggregationTest.php b/Tests/Functional/DSL/Aggregation/CardinalityAggregationTest.php new file mode 100644 index 00000000..2bf88a77 --- /dev/null +++ b/Tests/Functional/DSL/Aggregation/CardinalityAggregationTest.php @@ -0,0 +1,96 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Functional\DSL\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\CardinalityAggregation; +use ONGR\ElasticsearchBundle\ORM\Repository; +use ONGR\ElasticsearchBundle\Result\Aggregation\ValueAggregation; +use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase; + +class CardinalityAggregationTest extends ElasticsearchTestCase +{ + /** + * {@inheritdoc} + */ + protected function getDataArray() + { + return [ + 'default' => [ + 'product' => [ + [ + '_id' => 1, + 'title' => 'foo', + 'price' => 10.45, + ], + [ + '_id' => 2, + 'title' => 'bar', + 'price' => 32, + ], + [ + '_id' => 3, + 'title' => 'bar', + 'price' => 32, + ], + ], + ], + ]; + } + + /** + * Data provider for testCardinalityAggregation(). + * + * @return array + */ + public function getCardinalityAggregationData() + { + $out = []; + + // Case #0 without any threshold or rehash. + $out[] = ['threshold' => null, 'rehash' => null, 'expectedResults' => 2]; + + // Case #1 with threshold. + $out[] = ['threshold' => 1, 'rehash' => null, 'expectedResults' => 2]; + + // Case #2 with threshold and rehash. + $out[] = ['threshold' => 4000, 'rehash' => true, 'expectedResults' => 2]; + + return $out; + } + + /** + * Test for cardinality aggregation. + * + * @param int $threshold + * @param bool $rehash + * @param int $expectedResults + * + * @dataProvider getCardinalityAggregationData() + */ + public function testCardinalityAggregation($threshold, $rehash, $expectedResults) + { + /** @var Repository $repo */ + $repo = $this->getManager()->getRepository('AcmeTestBundle:Product'); + + $aggregation = new CardinalityAggregation('test_agg'); + $aggregation->setField('price'); + $aggregation->setPrecisionThreshold($threshold); + $aggregation->setRehash($rehash); + + $search = $repo->createSearch()->addAggregation($aggregation); + $results = $repo->execute($search, Repository::RESULTS_OBJECT); + + /** @var ValueAggregation $result */ + $result = $results->getAggregations()['test_agg']; + $this->assertEquals($expectedResults, $result->getValue()['value']); + } +}