Skip to content

Commit 8d9cd7b

Browse files
committed
[BUGFIX] Add aggregation and fieldBasedAggregation function
This change implements a new generuc aggregation function as well as a new fieldBasedAggregation function that replaces addSimpleAggregation
1 parent abaae1d commit 8d9cd7b

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,29 +404,42 @@ public function queryFilterMultiple($data, $clauseType = 'must') {
404404
}
405405

406406
/**
407-
* Adds a simple aggregation with only filed set as configuration
407+
* This method adds a field based aggregation configuration. This can be used for simple
408+
* aggregations like terms
408409
*
409410
* @param $name
410411
* @param $field
411412
* @param string $type
412413
* @param null $parentPath
413414
* @return $this
414415
*/
415-
public function addSimpleAggregation($name, $field, $type = 'terms', $parentPath = NULL) {
416-
if(!array_key_exists("aggregations", $this->request)) {
417-
$this->request['aggregations'] = array();
418-
}
419-
420-
$aggregation = array (
421-
$type => array (
416+
public function fieldBasedAggregation($name, $field, $type = "terms", $parentPath = NULL) {
417+
$aggregationDefinition = array(
418+
$type => array(
422419
'field' => $field
423420
)
424421
);
425422

423+
$this->aggregation($name, $aggregationDefinition, $parentPath);
424+
return $this;
425+
}
426+
427+
/**
428+
* @param string $name
429+
* @param array $aggregationDefinition
430+
* @param null $parentPath
431+
* @return $this
432+
* @throws QueryBuildingException
433+
*/
434+
public function aggregation($name, array $aggregationDefinition, $parentPath = NULL) {
435+
if(!array_key_exists("aggregations", $this->request)) {
436+
$this->request['aggregations'] = array();
437+
}
438+
426439
if($parentPath !== NULL) {
427-
$this->addSubAggregation($parentPath, $name, $aggregation);
440+
$this->addSubAggregation($parentPath, $name, $aggregationDefinition);
428441
} else {
429-
$this->request['aggregations'][$name] = $aggregation;
442+
$this->request['aggregations'][$name] = $aggregationDefinition;
430443
}
431444

432445
return $this;

Tests/Unit/Eel/ElasticSearchQueryBuilderTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function anSimpleAggregationCanBeAddedToTheRequest($type, $name, $field)
228228
)
229229
);
230230

231-
$this->queryBuilder->addSimpleAggregation($name, $field, $type);
231+
$this->queryBuilder->fieldBasedAggregation($name, $field, $type);
232232
$actual = $this->queryBuilder->getRequest();
233233

234234
$this->assertInArray($expected, $actual);
@@ -238,9 +238,9 @@ public function anSimpleAggregationCanBeAddedToTheRequest($type, $name, $field)
238238
* @test
239239
*/
240240
public function anAggregationCanBeSubbedUnderAPath() {
241-
$this->queryBuilder->addSimpleAggregation("foo", "bar");
242-
$this->queryBuilder->addSimpleAggregation("bar", "bar", "terms", "foo");
243-
$this->queryBuilder->addSimpleAggregation("baz", "bar", "terms", "foo.bar");
241+
$this->queryBuilder->fieldBasedAggregation("foo", "bar");
242+
$this->queryBuilder->fieldBasedAggregation("bar", "bar", "terms", "foo");
243+
$this->queryBuilder->fieldBasedAggregation("baz", "bar", "terms", "foo.bar");
244244

245245
$expected = array(
246246
"foo" => array(
@@ -267,17 +267,35 @@ public function anAggregationCanBeSubbedUnderAPath() {
267267
* @expectedException \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException
268268
*/
269269
public function ifTheParentPathDoesNotExistAnExceptionisThrown() {
270-
$this->queryBuilder->addSimpleAggregation("foo", "bar");
271-
$this->queryBuilder->addSimpleAggregation("bar", "bar", "terms", "doesNotExist");
270+
$this->queryBuilder->fieldBasedAggregation("foo", "bar");
271+
$this->queryBuilder->fieldBasedAggregation("bar", "bar", "terms", "doesNotExist");
272272
}
273273

274274
/**
275275
* @test
276276
* @expectedException \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException
277277
*/
278278
public function ifSubbedParentPathDoesNotExistAnExceptionisThrown() {
279-
$this->queryBuilder->addSimpleAggregation("foo", "bar");
280-
$this->queryBuilder->addSimpleAggregation("bar", "bar", "terms", "foo.doesNotExist");
279+
$this->queryBuilder->fieldBasedAggregation("foo", "bar");
280+
$this->queryBuilder->fieldBasedAggregation("bar", "bar", "terms", "foo.doesNotExist");
281+
}
282+
283+
/**
284+
* @test
285+
*/
286+
public function aCustomAggregationDefinitionCanBeApplied() {
287+
$expected = array(
288+
"foo" => array(
289+
"some" => array("field" => "bar"),
290+
"custom" => array("field" => "bar"),
291+
"arrays" => array("field" => "bar")
292+
)
293+
);
294+
295+
$this->queryBuilder->aggregation("foo", $expected['foo']);
296+
$actual = $this->queryBuilder->getRequest();
297+
298+
$this->assertInArray($expected, $actual);
281299
}
282300

283301
/**

0 commit comments

Comments
 (0)