-
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.
Write tests for NestedFilteredAggregation
- Loading branch information
Bart van Asselt
committed
Apr 25, 2024
1 parent
d7b63f8
commit 5c24fff
Showing
2 changed files
with
132 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Aggregations; | ||
|
||
final class NestedFilteredAggregation implements AggregationSyntaxInterface | ||
{ | ||
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 $name, string $field, array $filters, int $size = 10) | ||
{ | ||
$this->name = $name; | ||
$this->field = $field; | ||
$this->size = $size; | ||
$this->filters = $filters; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function buildElasticFilters(): array | ||
{ | ||
$elasticFilters = []; | ||
foreach ($this->filters as $field => $filterValues) { | ||
$elasticFilters[] = [ | ||
'terms' => [ | ||
'variants.' . $field => $filterValues, | ||
], | ||
]; | ||
} | ||
|
||
return [ | ||
'bool' => [ | ||
'should' => [ | ||
'bool' => [ | ||
'must' => $elasticFilters, | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function build(): array | ||
{ | ||
return [ | ||
'nested' => [ | ||
'path' => 'variants', | ||
], | ||
'aggs' => [ | ||
'filter_variants' => [ | ||
'filter' => $this->buildElasticFilters(), | ||
'aggs' => [ | ||
$this->name => [ | ||
'terms' => [ | ||
'field' => $this->field, | ||
'size' => $this->size, | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
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