Skip to content
Snippets Groups Projects
Commit d5ed6105 authored by Martynas Sudintas's avatar Martynas Sudintas
Browse files

Merge pull request #339 from chyzas/patch-refactoring-cardinality-agg

Refactored cardinality aggregation
parents b45f5b06 0a15ccd4
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\DSL\Aggregation; namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait; use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
/** /**
* Difference values counter. * Difference values counter.
...@@ -19,6 +20,7 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait; ...@@ -19,6 +20,7 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
class CardinalityAggregation extends AbstractAggregation class CardinalityAggregation extends AbstractAggregation
{ {
use MetricTrait; use MetricTrait;
use ScriptAwareTrait;
/** /**
* @var int * @var int
...@@ -30,33 +32,24 @@ class CardinalityAggregation extends AbstractAggregation ...@@ -30,33 +32,24 @@ class CardinalityAggregation extends AbstractAggregation
*/ */
private $rehash; private $rehash;
/**
* @var string
*/
private $script;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getArray() public function getArray()
{ {
$out = []; $out = array_filter(
[
if ($this->getField()) { 'field' => $this->getField(),
$out['field'] = $this->getField(); 'script' => $this->getScript(),
} elseif ($this->getScript()) { 'precision_threshold' => $this->getPrecisionThreshold(),
$out['script'] = $this->getScript(); 'rehash' => $this->isRehash(),
} else { ],
throw new \LogicException('Cardinality aggregation must have field or script set.'); function ($val) {
} return ($val || is_bool($val));
}
if ($this->getPrecisionThreshold()) { );
$out['precision_threshold'] = $this->getPrecisionThreshold();
} $this->checkRequiredFields($out);
if ($this->isRehash()) {
$out['rehash'] = $this->isRehash();
}
return $out; return $out;
} }
...@@ -96,26 +89,24 @@ class CardinalityAggregation extends AbstractAggregation ...@@ -96,26 +89,24 @@ class CardinalityAggregation extends AbstractAggregation
} }
/** /**
* @return string * {@inheritdoc}
*/
public function getScript()
{
return $this->script;
}
/**
* @param string $script
*/ */
public function setScript($script) public function getType()
{ {
$this->script = $script; return 'cardinality';
} }
/** /**
* {@inheritdoc} * Checks if required fields are set.
*
* @param array $fields
*
* @throws \LogicException
*/ */
public function getType() private function checkRequiredFields($fields)
{ {
return 'cardinality'; if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) {
throw new \LogicException('Cardinality aggregation must have field or script set.');
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment