Skip to content

Commit 14ab67d

Browse files
committed
Merge pull request #424 from ongr-io/master
v0.10.2 release
2 parents 5c3889e + a56ec01 commit 14ab67d

File tree

9 files changed

+84
-374
lines changed

9 files changed

+84
-374
lines changed

Annotation/Document.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ final class Document implements DumperInterface
5656
*/
5757
public $dynamic;
5858

59+
/**
60+
* @var array
61+
*/
62+
public $dynamicTemplates;
63+
5964
/**
6065
* @var array
6166
*/
@@ -77,6 +82,7 @@ public function dump(array $exclude = [])
7782
'_all' => $this->all,
7883
'enabled' => $this->enabled,
7984
'dynamic' => $this->dynamic,
85+
'dynamic_templates' => $this->dynamicTemplates,
8086
'transform' => $this->transform,
8187
'dynamic_date_formats' => $this->dynamicDateFormats,
8288
],

Annotation/Suggester/AbstractSuggesterProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class AbstractSuggesterProperty extends AbstractProperty
3535
*
3636
* @Required
3737
*/
38-
public $objectName;
38+
public $objectName;
3939

4040
/**
4141
* @var string

DSL/Aggregation/AbstractAggregation.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
use ONGR\ElasticsearchBundle\DSL\NamedBuilderBag;
1515
use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface;
16+
use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
1617

1718
/**
1819
* AbstractAggregation class.
1920
*/
2021
abstract class AbstractAggregation implements NamedBuilderInterface
2122
{
23+
use ParametersTrait;
24+
2225
const PREFIX = 'agg_';
2326

2427
/**
@@ -113,7 +116,12 @@ public function getAggregations()
113116
*/
114117
public function toArray()
115118
{
116-
$result = [$this->getName() => [$this->getType() => $this->getArray()]];
119+
$array = $this->getArray();
120+
$result = [
121+
$this->getName() => [
122+
$this->getType() => is_array($array) ? $this->processArray($array) : $array,
123+
],
124+
];
117125

118126
if ($this->supportsNesting()) {
119127
$nestedResult = $this->collectNestedAggregations();

DSL/Aggregation/TermsAggregation.php

Lines changed: 11 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -12,162 +12,15 @@
1212
namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
1313

1414
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait;
15+
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
1516

1617
/**
1718
* Class representing TermsAggregation.
1819
*/
1920
class TermsAggregation extends AbstractAggregation
2021
{
2122
use BucketingTrait;
22-
23-
const MODE_COUNT = '_count';
24-
const MODE_TERM = '_term';
25-
const DIRECTION_ASC = 'asc';
26-
const DIRECTION_DESC = 'desc';
27-
28-
/**
29-
* @var int
30-
*/
31-
private $size;
32-
33-
/**
34-
* @var string
35-
*/
36-
private $orderMode;
37-
38-
/**
39-
* @var string
40-
*/
41-
private $orderDirection;
42-
43-
/**
44-
* @var int
45-
*/
46-
private $minDocumentCount;
47-
48-
/**
49-
* @var string
50-
*/
51-
private $include;
52-
53-
/**
54-
* @var string
55-
*/
56-
private $includeFlags;
57-
58-
/**
59-
* @var string
60-
*/
61-
private $exclude;
62-
63-
/**
64-
* @var string
65-
*/
66-
private $excludeFlags;
67-
68-
/**
69-
* Sets buckets max count.
70-
*
71-
* @param int $size
72-
*/
73-
public function setSize($size)
74-
{
75-
$this->size = $size;
76-
}
77-
78-
/**
79-
* Sets buckets ordering.
80-
*
81-
* @param string $mode
82-
* @param string $direction
83-
*/
84-
public function setOrder($mode, $direction = self::DIRECTION_ASC)
85-
{
86-
$this->orderMode = $mode;
87-
$this->orderDirection = $direction;
88-
}
89-
90-
/**
91-
* Sets minimum hits to consider as term.
92-
*
93-
* @param int $count
94-
*/
95-
public function setMinDocumentCount($count)
96-
{
97-
$this->minDocumentCount = $count;
98-
}
99-
100-
/**
101-
* Sets include field.
102-
*
103-
* @param string $include Include field.
104-
* @param string $flags Possible flags:
105-
* - CANON_EQ
106-
* - CASE_INSENSITIVE
107-
* - COMMENTS
108-
* - DOTALL
109-
* - LITERAL
110-
* - MULTILINE
111-
* - UNICODE
112-
* - UNICODE_CASE
113-
* - UNICODE_CHARACTER_CLASS
114-
* - UNIX_LINES
115-
* Usage example:
116-
* 'CASE_INSENSITIVE|MULTILINE'.
117-
*/
118-
public function setInclude($include, $flags = '')
119-
{
120-
$this->include = $include;
121-
$this->includeFlags = $flags;
122-
}
123-
124-
/**
125-
* Sets include field.
126-
*
127-
* @param string $exclude
128-
* @param string $flags
129-
*
130-
* @see Terms::setInclude()
131-
*/
132-
public function setExclude($exclude, $flags = '')
133-
{
134-
$this->exclude = $exclude;
135-
$this->excludeFlags = $flags;
136-
}
137-
138-
/**
139-
* {@inheritdoc}
140-
*/
141-
public function getArray()
142-
{
143-
$data = ['field' => $this->getField()];
144-
145-
if ($this->orderMode && $this->orderDirection) {
146-
$data['order'] = [
147-
$this->orderMode => $this->orderDirection,
148-
];
149-
}
150-
151-
if ($this->size !== null) {
152-
$data['size'] = $this->size;
153-
}
154-
155-
if ($this->minDocumentCount !== null) {
156-
$data['min_doc_count'] = $this->minDocumentCount;
157-
}
158-
159-
$includeResult = $this->getIncludeExclude($this->include, $this->includeFlags);
160-
if ($includeResult) {
161-
$data['include'] = $includeResult;
162-
}
163-
164-
$excludeResult = $this->getIncludeExclude($this->exclude, $this->excludeFlags);
165-
if ($excludeResult) {
166-
$data['exclude'] = $excludeResult;
167-
}
168-
169-
return $data;
170-
}
23+
use ScriptAwareTrait;
17124

17225
/**
17326
* {@inheritdoc}
@@ -178,26 +31,17 @@ public function getType()
17831
}
17932

18033
/**
181-
* Constructs include/exclude search values.
182-
*
183-
* @param string $pattern
184-
* @param string $flags
185-
*
186-
* @return string|array|null
34+
* {@inheritdoc}
18735
*/
188-
protected function getIncludeExclude($pattern, $flags)
36+
public function getArray()
18937
{
190-
if ($pattern) {
191-
if (empty($flags)) {
192-
return $pattern;
193-
} else {
194-
return [
195-
'pattern' => $pattern,
196-
'flags' => $flags,
197-
];
198-
}
199-
}
38+
$data = array_filter(
39+
[
40+
'field' => $this->getField(),
41+
'script' => $this->getScript(),
42+
]
43+
);
20044

201-
return null;
45+
return $data;
20246
}
20347
}

Tests/Functional/Annotation/DocumentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testDocumentMappingWithAllFieldSetToFalse()
3232
'dynamic' => null,
3333
'transform' => null,
3434
'dynamic_date_formats' => null,
35+
'dynamic_templates' => null,
3536
];
3637
$this->assertEquals($expectedResult, $result);
3738
}

Tests/Functional/DSL/Aggregation/TermsAggregationTest.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation;
1515
use ONGR\ElasticsearchBundle\DSL\Query\RangeQuery;
1616
use ONGR\ElasticsearchBundle\ORM\Repository;
17-
use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase;
17+
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
1818

19-
class TermsAggregationTest extends ElasticsearchTestCase
19+
class TermsAggregationTest extends AbstractElasticsearchTestCase
2020
{
2121
/**
2222
* {@inheritdoc}
@@ -107,7 +107,7 @@ public function getTermsAggregationData()
107107
$aggregation = [
108108
'name' => 'test_agg',
109109
'field' => 'surface',
110-
'order' => [TermsAggregation::MODE_TERM, TermsAggregation::DIRECTION_ASC],
110+
'order' => ['_term', 'asc'],
111111
];
112112

113113
$result = [
@@ -134,7 +134,7 @@ public function getTermsAggregationData()
134134
$aggregation = [
135135
'name' => 'test_agg',
136136
'field' => 'surface',
137-
'min_document_cound' => 2,
137+
'min_document_count' => 2,
138138
];
139139

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

287+
/**
288+
* Test for terms aggregation with shard_size, collect_mode, shard_min_doc_count.
289+
*/
290+
public function testTermsAggregationWithCollectMode()
291+
{
292+
/** @var Repository $repo */
293+
$repo = $this->getManager()->getRepository('AcmeTestBundle:Product');
294+
$aggregationFoo = new TermsAggregation('test_foo');
295+
$aggregationFoo->setField('title');
296+
$aggregationFoo->addParameter('size', 5);
297+
$aggregationFoo->addParameter('shard_size', 5);
298+
$aggregationFoo->addParameter('execution_hint', 'map');
299+
$aggregationFoo->addParameter('collect_mode', 'breadth_first');
300+
301+
$aggregationBar = new TermsAggregation('test_bar');
302+
$aggregationBar->setField('title');
303+
$aggregationBar->addParameter('shard_min_doc_count', 5);
304+
$aggregationFoo->addAggregation($aggregationBar);
305+
306+
$search = $repo->createSearch()->addAggregation($aggregationFoo);
307+
$results = $repo->execute($search, Repository::RESULTS_RAW);
308+
$this->assertEquals(3, count($results['aggregations'][$aggregationFoo->getName()]['buckets']));
309+
}
310+
287311
/**
288312
* Builds term aggregation.
289313
*
@@ -297,23 +321,23 @@ private function getAggregation($options)
297321
$term->setField($options['field']);
298322

299323
if (array_key_exists('exclude', $options)) {
300-
$term->setExclude($options['exclude']);
324+
$term->addParameter('exclude', $options['exclude']);
301325
}
302326

303327
if (array_key_exists('include', $options)) {
304-
$term->setInclude($options['include']);
328+
$term->addParameter('include', $options['include']);
305329
}
306330

307331
if (array_key_exists('min_document_count', $options)) {
308-
$term->setMinDocumentCount($options['min_document_count']);
332+
$term->addParameter('min_doc_count', $options['min_document_count']);
309333
}
310334

311335
if (array_key_exists('order', $options)) {
312-
call_user_func_array([$term, 'setOrder'], $options['order']);
336+
$term->addParameter('order', [$options['order'][0] => $options['order'][1]]);
313337
}
314338

315339
if (array_key_exists('size', $options)) {
316-
$term->setSize($options['size']);
340+
$term->addParameter('size', $options['size']);
317341
}
318342

319343
return $term;

Tests/Functional/DSL/Complex/PostFilterAndAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testBoolWithFuzzyQueryAndSortFilter()
8282
$name = 'foo';
8383
$TermsAgg = new TermsAggregation($name);
8484
$TermsAgg->setField('title');
85-
$TermsAgg->setInclude($name);
85+
$TermsAgg->addParameter('include', $name);
8686

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

0 commit comments

Comments
 (0)