Skip to content

Commit e8b8619

Browse files
committed
FEATURE: Make amount of buckets configurable via fusion
By default, elasticsearch returns 10 buckets for a term aggregation. This change makes the size value configurable via fusion.
1 parent 1b79d2b commit e8b8619

5 files changed

Lines changed: 55 additions & 38 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Driver/AbstractQuery.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public function addSortFilter($configuration)
9696
/**
9797
* {@inheritdoc}
9898
*/
99-
public function aggregation($name, array $aggregationDefinition, $parentPath = null)
99+
public function aggregation($name, array $aggregationDefinition, $parentPath = '')
100100
{
101101
if (!array_key_exists('aggregations', $this->request)) {
102102
$this->request['aggregations'] = [];
103103
}
104104

105-
if ($parentPath !== null) {
105+
if ((string)$parentPath !== '') {
106106
$this->addSubAggregation($parentPath, $name, $aggregationDefinition);
107107
} else {
108108
$this->request['aggregations'][$name] = $aggregationDefinition;
@@ -116,8 +116,8 @@ public function aggregation($name, array $aggregationDefinition, $parentPath = n
116116
* insert your $aggregationConfiguration under
117117
* $this->request['aggregations']['foo']['aggregations']['bar']['aggregations'][$name]
118118
*
119-
* @param $parentPath
120-
* @param $name
119+
* @param string $parentPath The parent path to add the sub aggregation to
120+
* @param string $name The name to identify the resulting aggregation
121121
* @param array $aggregationConfiguration
122122
* @return QueryInterface
123123
* @throws Exception\QueryBuildingException
@@ -127,9 +127,9 @@ protected function addSubAggregation($parentPath, $name, $aggregationConfigurati
127127
// Find the parentPath
128128
$path =& $this->request['aggregations'];
129129

130-
foreach (explode(".", $parentPath) as $subPart) {
130+
foreach (explode('.', $parentPath) as $subPart) {
131131
if ($path == null || !array_key_exists($subPart, $path)) {
132-
throw new Exception\QueryBuildingException("The parent path " . $subPart . " could not be found when adding a sub aggregation");
132+
throw new Exception\QueryBuildingException(sprintf('The parent path segment "%s" could not be found when adding a sub aggregation to parent path "%s"', $subPart, $parentPath));
133133
}
134134
$path =& $path[$subPart]['aggregations'];
135135
}

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Driver/QueryInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ public function highlight($fragmentSize, $fragmentCount = null);
9191
/**
9292
* This method is used to create any kind of aggregation.
9393
*
94-
* @param string $name
94+
* @param string $name The name to identify the resulting aggregation
9595
* @param array $aggregationDefinition
96-
* @param string $parentPath
96+
* @param string $parentPath ParentPath to define the parent of a sub aggregation
9797
* @return void
9898
* @api
9999
* @throws Exception\QueryBuildingException
100100
*/
101-
public function aggregation($name, array $aggregationDefinition, $parentPath = null);
101+
public function aggregation($name, array $aggregationDefinition, $parentPath = '');
102102

103103
/**
104104
* This method is used to create any kind of suggestion.

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,19 @@ public function queryFilterMultiple($data, $clauseType = 'must')
368368
*
369369
* Access all aggregation data with {nodes.aggregations} in your fluid template
370370
*
371-
* @param $name
372-
* @param $field
373-
* @param string $type
374-
* @param null $parentPath
371+
* @param string $name The name to identify the resulting aggregation
372+
* @param string $field The field to aggregate by
373+
* @param string $type Aggregation type
374+
* @param string $parentPath
375+
* @param int $size The amount of buckets to return
375376
* @return $this
376377
*/
377-
public function fieldBasedAggregation($name, $field, $type = "terms", $parentPath = null)
378+
public function fieldBasedAggregation($name, $field, $type = 'terms', $parentPath = '', $size = 10)
378379
{
379380
$aggregationDefinition = [
380381
$type => [
381-
'field' => $field
382+
'field' => $field,
383+
'size' => $size
382384
]
383385
];
384386

@@ -404,11 +406,11 @@ public function fieldBasedAggregation($name, $field, $type = "terms", $parentPat
404406
*
405407
* @param string $name
406408
* @param array $aggregationDefinition
407-
* @param null $parentPath
409+
* @param string $parentPath
408410
* @return $this
409411
* @throws QueryBuildingException
410412
*/
411-
public function aggregation($name, array $aggregationDefinition, $parentPath = null)
413+
public function aggregation($name, array $aggregationDefinition, $parentPath = '')
412414
{
413415
$this->request->aggregation($name, $aggregationDefinition, $parentPath);
414416

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ Right now there are two methods implemented. One generic `aggregation` function
208208
aggregation definition and a pre-configured `fieldBasedAggregation`. Both methods can be added to your TS search query.
209209
You can nest aggregations by providing a parent name.
210210

211-
* `aggregation($name, array $aggregationDefinition, $parentPath = NULL)` -- generic method to add a $aggregationDefinition under a path $parentPath with the name $name
212-
* `fieldBasedAggregation($name, $field, $type = "terms", $parentPath = NULL)` -- adds a simple filed based Aggregation of type $type with name $name under path $parentPath. Used for simple aggregations like sum, avg, min, max or terms
211+
* `aggregation($name, array $aggregationDefinition, $parentPath = NULL)` -- generic method to add a $aggregationDefinition under a path $parentPath with the name $name.
212+
* `fieldBasedAggregation($name, $field, $type = 'terms', $parentPath = '', $size = 10)` -- adds a simple filed based Aggregation of type $type with name $name under path $parentPath. Used for simple aggregations like sum, avg, min, max or terms. By default 10 buckets are returned.
213213

214214

215215
### Examples

Tests/Unit/Eel/ElasticSearchQueryBuilderTest.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -279,29 +279,35 @@ public function rangeConstraintsWork($constraint, $operator, $value)
279279
public function simpleAggregationExamples()
280280
{
281281
return [
282-
['min', 'foo', 'bar'],
283-
['terms', 'foo', 'bar'],
284-
['sum', 'foo', 'bar'],
285-
['stats', 'foo', 'bar'],
286-
['value_count', 'foo', 'bar']
282+
['min', 'foo', 'bar', 10],
283+
['terms', 'foo', 'bar', 10],
284+
['sum', 'foo', 'bar', 10],
285+
['stats', 'foo', 'bar', 10],
286+
['value_count', 'foo', 'bar', 20]
287287
];
288288
}
289289

290290
/**
291291
* @test
292292
* @dataProvider simpleAggregationExamples
293+
*
294+
* @param string $type
295+
* @param string $name
296+
* @param string $field
297+
* @param integer size
293298
*/
294-
public function anSimpleAggregationCanBeAddedToTheRequest($type, $name, $field)
299+
public function anSimpleAggregationCanBeAddedToTheRequest($type, $name, $field, $size)
295300
{
296301
$expected = [
297302
$name => [
298303
$type => [
299-
'field' => $field
304+
'field' => $field,
305+
'size' => $size
300306
]
301307
]
302308
];
303309

304-
$this->queryBuilder->fieldBasedAggregation($name, $field, $type);
310+
$this->queryBuilder->fieldBasedAggregation($name, $field, $type, '', $size);
305311
$actual = $this->queryBuilder->getRequest()->toArray();
306312

307313
$this->assertInArray($expected, $actual);
@@ -312,19 +318,28 @@ public function anSimpleAggregationCanBeAddedToTheRequest($type, $name, $field)
312318
*/
313319
public function anAggregationCanBeSubbedUnderAPath()
314320
{
315-
$this->queryBuilder->fieldBasedAggregation("foo", "bar");
316-
$this->queryBuilder->fieldBasedAggregation("bar", "bar", "terms", "foo");
317-
$this->queryBuilder->fieldBasedAggregation("baz", "bar", "terms", "foo.bar");
321+
$this->queryBuilder->fieldBasedAggregation('foo', 'bar');
322+
$this->queryBuilder->fieldBasedAggregation('bar', 'bar', 'terms', 'foo');
323+
$this->queryBuilder->fieldBasedAggregation('baz', 'bar', 'terms', 'foo.bar');
318324

319325
$expected = [
320-
"foo" => [
321-
"terms" => ["field" => "bar"],
322-
"aggregations" => [
323-
"bar" => [
324-
"terms" => ["field" => "bar"],
325-
"aggregations" => [
326-
"baz" => [
327-
"terms" => ["field" => "bar"]
326+
'foo' => [
327+
'terms' => [
328+
'field' => 'bar',
329+
'size' => 10
330+
],
331+
'aggregations' => [
332+
'bar' => [
333+
'terms' => [
334+
'field' => 'bar',
335+
'size' => 10
336+
],
337+
'aggregations' => [
338+
'baz' => [
339+
'terms' => [
340+
'field' => 'bar',
341+
'size' => 10
342+
],
328343
]
329344
]
330345
]

0 commit comments

Comments
 (0)