diff --git a/Aggregation/AbstractAggregation.php b/Aggregation/AbstractAggregation.php index 110a4a54fa85001923a6ea0e1e520877395bb0d0..3dff51ff7679722c54495396b8dba809abca4196 100644 --- a/Aggregation/AbstractAggregation.php +++ b/Aggregation/AbstractAggregation.php @@ -13,12 +13,15 @@ namespace ONGR\ElasticsearchBundle\DSL\Aggregation; use ONGR\ElasticsearchBundle\DSL\NamedBuilderBag; use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface; +use ONGR\ElasticsearchBundle\DSL\ParametersTrait; /** * AbstractAggregation class. */ abstract class AbstractAggregation implements NamedBuilderInterface { + use ParametersTrait; + const PREFIX = 'agg_'; /** @@ -113,7 +116,12 @@ abstract class AbstractAggregation implements NamedBuilderInterface */ public function toArray() { - $result = [$this->getName() => [$this->getType() => $this->getArray()]]; + $array = $this->getArray(); + $result = [ + $this->getName() => [ + $this->getType() => is_array($array) ? $this->processArray($array) : $array, + ], + ]; if ($this->supportsNesting()) { $nestedResult = $this->collectNestedAggregations(); diff --git a/Aggregation/TermsAggregation.php b/Aggregation/TermsAggregation.php index 385156588627f202b64829476e9ba5304065f98c..a12ec388c2c708da74edef14d355241f658338a8 100644 --- a/Aggregation/TermsAggregation.php +++ b/Aggregation/TermsAggregation.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchBundle\DSL\Aggregation; use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait; +use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait; /** * Class representing TermsAggregation. @@ -19,155 +20,7 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait; class TermsAggregation extends AbstractAggregation { use BucketingTrait; - - const MODE_COUNT = '_count'; - const MODE_TERM = '_term'; - const DIRECTION_ASC = 'asc'; - const DIRECTION_DESC = 'desc'; - - /** - * @var int - */ - private $size; - - /** - * @var string - */ - private $orderMode; - - /** - * @var string - */ - private $orderDirection; - - /** - * @var int - */ - private $minDocumentCount; - - /** - * @var string - */ - private $include; - - /** - * @var string - */ - private $includeFlags; - - /** - * @var string - */ - private $exclude; - - /** - * @var string - */ - private $excludeFlags; - - /** - * Sets buckets max count. - * - * @param int $size - */ - public function setSize($size) - { - $this->size = $size; - } - - /** - * Sets buckets ordering. - * - * @param string $mode - * @param string $direction - */ - public function setOrder($mode, $direction = self::DIRECTION_ASC) - { - $this->orderMode = $mode; - $this->orderDirection = $direction; - } - - /** - * Sets minimum hits to consider as term. - * - * @param int $count - */ - public function setMinDocumentCount($count) - { - $this->minDocumentCount = $count; - } - - /** - * Sets include field. - * - * @param string $include Include field. - * @param string $flags Possible flags: - * - CANON_EQ - * - CASE_INSENSITIVE - * - COMMENTS - * - DOTALL - * - LITERAL - * - MULTILINE - * - UNICODE - * - UNICODE_CASE - * - UNICODE_CHARACTER_CLASS - * - UNIX_LINES - * Usage example: - * 'CASE_INSENSITIVE|MULTILINE'. - */ - public function setInclude($include, $flags = '') - { - $this->include = $include; - $this->includeFlags = $flags; - } - - /** - * Sets include field. - * - * @param string $exclude - * @param string $flags - * - * @see Terms::setInclude() - */ - public function setExclude($exclude, $flags = '') - { - $this->exclude = $exclude; - $this->excludeFlags = $flags; - } - - /** - * {@inheritdoc} - */ - public function getArray() - { - $data = ['field' => $this->getField()]; - - if ($this->orderMode && $this->orderDirection) { - $data['order'] = [ - $this->orderMode => $this->orderDirection, - ]; - } - - if ($this->size !== null) { - $data['size'] = $this->size; - } - - if ($this->minDocumentCount !== null) { - $data['min_doc_count'] = $this->minDocumentCount; - } - - $includeResult = $this->getIncludeExclude($this->include, $this->includeFlags); - if ($includeResult) { - $data['include'] = $includeResult; - } - - $excludeResult = $this->getIncludeExclude($this->exclude, $this->excludeFlags); - if ($excludeResult) { - $data['exclude'] = $excludeResult; - } - - return $data; - } + use ScriptAwareTrait; /** * {@inheritdoc} @@ -178,26 +31,17 @@ class TermsAggregation extends AbstractAggregation } /** - * Constructs include/exclude search values. - * - * @param string $pattern - * @param string $flags - * - * @return string|array|null + * {@inheritdoc} */ - protected function getIncludeExclude($pattern, $flags) + public function getArray() { - if ($pattern) { - if (empty($flags)) { - return $pattern; - } else { - return [ - 'pattern' => $pattern, - 'flags' => $flags, - ]; - } - } + $data = array_filter( + [ + 'field' => $this->getField(), + 'script' => $this->getScript(), + ] + ); - return null; + return $data; } }