Skip to content

Commit

Permalink
Merge pull request #424 from ongr-io/master
Browse files Browse the repository at this point in the history
v0.10.2 release
  • Loading branch information
saimaz committed Sep 16, 2015
2 parents 5c3889e + a56ec01 commit 14ab67d
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 374 deletions.
6 changes: 6 additions & 0 deletions Annotation/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ final class Document implements DumperInterface
*/
public $dynamic;

/**
* @var array
*/
public $dynamicTemplates;

/**
* @var array
*/
Expand All @@ -77,6 +82,7 @@ public function dump(array $exclude = [])
'_all' => $this->all,
'enabled' => $this->enabled,
'dynamic' => $this->dynamic,
'dynamic_templates' => $this->dynamicTemplates,
'transform' => $this->transform,
'dynamic_date_formats' => $this->dynamicDateFormats,
],
Expand Down
2 changes: 1 addition & 1 deletion Annotation/Suggester/AbstractSuggesterProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class AbstractSuggesterProperty extends AbstractProperty
*
* @Required
*/
public $objectName;
public $objectName;

/**
* @var string
Expand Down
10 changes: 9 additions & 1 deletion DSL/Aggregation/AbstractAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

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_';

/**
Expand Down Expand Up @@ -113,7 +116,12 @@ public function getAggregations()
*/
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();
Expand Down
178 changes: 11 additions & 167 deletions DSL/Aggregation/TermsAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,162 +12,15 @@
namespace ONGR\ElasticsearchBundle\DSL\Aggregation;

use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait;
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;

/**
* Class representing TermsAggregation.
*/
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 comment has been minimized.

Copy link
@asev

asev Sep 21, 2015

Contributor

Public methods removed at minor release?
It would be nice to mark methods as deprecated before removing them.

This comment has been minimized.

Copy link
@tautrimas

tautrimas Sep 21, 2015

Contributor

Yeah, that was a pain too, but we already solved it by following this path: ongr-io/FilterManagerBundle@3a7da7a

This comment has been minimized.

Copy link
@saimaz

saimaz Sep 21, 2015

Author Member

Sorry for the inconvenience, but this was very necessary change. We are preparing core foundations to avoid that kind of changes.

{
$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}
Expand All @@ -178,26 +31,17 @@ public function getType()
}

/**
* 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;
}
}
1 change: 1 addition & 0 deletions Tests/Functional/Annotation/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testDocumentMappingWithAllFieldSetToFalse()
'dynamic' => null,
'transform' => null,
'dynamic_date_formats' => null,
'dynamic_templates' => null,
];
$this->assertEquals($expectedResult, $result);
}
Expand Down
42 changes: 33 additions & 9 deletions Tests/Functional/DSL/Aggregation/TermsAggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation;
use ONGR\ElasticsearchBundle\DSL\Query\RangeQuery;
use ONGR\ElasticsearchBundle\ORM\Repository;
use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase;
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;

class TermsAggregationTest extends ElasticsearchTestCase
class TermsAggregationTest extends AbstractElasticsearchTestCase
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -107,7 +107,7 @@ public function getTermsAggregationData()
$aggregation = [
'name' => 'test_agg',
'field' => 'surface',
'order' => [TermsAggregation::MODE_TERM, TermsAggregation::DIRECTION_ASC],
'order' => ['_term', 'asc'],
];

$result = [
Expand All @@ -134,7 +134,7 @@ public function getTermsAggregationData()
$aggregation = [
'name' => 'test_agg',
'field' => 'surface',
'min_document_cound' => 2,
'min_document_count' => 2,
];

$result = [
Expand Down Expand Up @@ -284,6 +284,30 @@ public function testTermsAggregationWithRangeQuery($aggregation, $parameters, $e
$this->assertArraySubset($expectedResult, $results['aggregations']);
}

/**
* Test for terms aggregation with shard_size, collect_mode, shard_min_doc_count.
*/
public function testTermsAggregationWithCollectMode()
{
/** @var Repository $repo */
$repo = $this->getManager()->getRepository('AcmeTestBundle:Product');
$aggregationFoo = new TermsAggregation('test_foo');
$aggregationFoo->setField('title');
$aggregationFoo->addParameter('size', 5);
$aggregationFoo->addParameter('shard_size', 5);
$aggregationFoo->addParameter('execution_hint', 'map');
$aggregationFoo->addParameter('collect_mode', 'breadth_first');

$aggregationBar = new TermsAggregation('test_bar');
$aggregationBar->setField('title');
$aggregationBar->addParameter('shard_min_doc_count', 5);
$aggregationFoo->addAggregation($aggregationBar);

$search = $repo->createSearch()->addAggregation($aggregationFoo);
$results = $repo->execute($search, Repository::RESULTS_RAW);
$this->assertEquals(3, count($results['aggregations'][$aggregationFoo->getName()]['buckets']));
}

/**
* Builds term aggregation.
*
Expand All @@ -297,23 +321,23 @@ private function getAggregation($options)
$term->setField($options['field']);

if (array_key_exists('exclude', $options)) {
$term->setExclude($options['exclude']);
$term->addParameter('exclude', $options['exclude']);
}

if (array_key_exists('include', $options)) {
$term->setInclude($options['include']);
$term->addParameter('include', $options['include']);
}

if (array_key_exists('min_document_count', $options)) {
$term->setMinDocumentCount($options['min_document_count']);
$term->addParameter('min_doc_count', $options['min_document_count']);
}

if (array_key_exists('order', $options)) {
call_user_func_array([$term, 'setOrder'], $options['order']);
$term->addParameter('order', [$options['order'][0] => $options['order'][1]]);
}

if (array_key_exists('size', $options)) {
$term->setSize($options['size']);
$term->addParameter('size', $options['size']);
}

return $term;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testBoolWithFuzzyQueryAndSortFilter()
$name = 'foo';
$TermsAgg = new TermsAggregation($name);
$TermsAgg->setField('title');
$TermsAgg->setInclude($name);
$TermsAgg->addParameter('include', $name);

$filterAgg = new FilterAggregation($name . '-filter');

Expand Down
Loading

0 comments on commit 14ab67d

Please sign in to comment.