Skip to content
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
95 changes: 95 additions & 0 deletions DSL/Aggregation/CardinalityAggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* 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';
}
}
96 changes: 96 additions & 0 deletions Tests/Functional/DSL/Aggregation/CardinalityAggregationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* 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']);
}
}