Skip to content
Snippets Groups Projects
Commit 861fea28 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Merge branch 'patch-terms-aggregation' of...

Merge branch 'patch-terms-aggregation' of https://github.com/chyzas/ElasticsearchBundle into chyzas-patch-terms-aggregation

# Conflicts:
#	DSL/Aggregation/TermsAggregation.php
parents 773a6531 644fbd55
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......
......@@ -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;
}
}
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