-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from bartjuh4/nested_aggregations
Add support for nested aggregations
- Loading branch information
Showing
9 changed files
with
281 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Aggregations; | ||
|
||
final class NestedFilteredAggregation implements AggregationSyntaxInterface | ||
{ | ||
private string $path; | ||
|
||
private string $name; | ||
|
||
private string $field; | ||
|
||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
private array $filters; | ||
|
||
private int $size; | ||
|
||
/** | ||
* @param array<string, mixed> $filters | ||
*/ | ||
public function __construct(string $path, string $name, string $field, array $filters, int $size = 10) | ||
{ | ||
$this->path = $path; | ||
$this->name = $name; | ||
$this->field = $field; | ||
$this->size = $size; | ||
$this->filters = $filters; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function build(): array | ||
{ | ||
return [ | ||
'nested' => [ | ||
'path' => $this->path, | ||
], | ||
'aggs' => [ | ||
'filter_aggs' => [ | ||
'filter' => $this->buildElasticFilters(), | ||
'aggs' => [ | ||
$this->name => [ | ||
'terms' => [ | ||
'field' => $this->path . '.' . $this->field, | ||
'size' => $this->size, | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
private function buildElasticFilters(): array | ||
{ | ||
$elasticFilters = []; | ||
foreach ($this->filters as $field => $filterValues) { | ||
$elasticFilters[] = [ | ||
'terms' => [ | ||
$this->path . '.' . $field => $filterValues, | ||
], | ||
]; | ||
} | ||
|
||
return [ | ||
'bool' => [ | ||
'should' => [ | ||
'bool' => [ | ||
'must' => $elasticFilters, | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.