-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if adding to ignore fixes CI Add code coverage Write tests for NestedFilteredAggregation Add support for nested aggregations
- Loading branch information
Bart van Asselt
committed
Apr 25, 2024
1 parent
3a0283c
commit 8825d6f
Showing
9 changed files
with
283 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.