Skip to content

Commit

Permalink
Kill mutant in Results.php
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 32 deletions.
2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ExplorerServiceProvider.php",
"Infrastructure/Console",
"Infrastructure/Scout/ElasticEngine",
"Infrastructure/Elastic/ElasticAdapter",
"Infrastructure/Elastic/ElasticClientBuilder",
"Infrastructure/Elastic/ElasticIndexAdapter",
"Infrastructure/Elastic/ElasticDocumentAdapter"
]
Expand Down
28 changes: 23 additions & 5 deletions src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace JeroenG\Explorer\Application;

use Countable;
use function array_key_exists;
use function array_merge;

class Results implements Countable
{
Expand All @@ -31,11 +33,7 @@ public function aggregations(): array

foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) {
if (array_key_exists('doc_count', $rawAggregation)) {
foreach ($rawAggregation as $nestedAggregationName => $rawNestedAggregation) {
if (isset($rawNestedAggregation['buckets'])) {
$aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']);
}
}
$aggregations = array_merge($aggregations, $this->parseNestedAggregations($rawAggregation));
continue;
}

Expand All @@ -49,4 +47,24 @@ public function count(): int
{
return $this->rawResults['hits']['total']['value'];
}

/** @return AggregationResult[] */
private function parseNestedAggregations(array $rawAggregation): array
{
$aggregations = [];
foreach ($rawAggregation as $nestedAggregationName => $rawNestedAggregation) {
if (isset($rawNestedAggregation['buckets'])) {
$aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']);
}

if (isset($rawNestedAggregation['doc_count'])) {
$nested = $this->parseNestedAggregations($rawNestedAggregation);
foreach ($nested as $item) {
$aggregations[] = $item;
}
}
}

return $aggregations;
}
}
83 changes: 83 additions & 0 deletions src/Domain/Aggregations/NestedFilteredAggregation.php
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,
],
],
],
];
}
}
2 changes: 0 additions & 2 deletions src/Domain/IndexManagement/DirectIndexConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace JeroenG\Explorer\Domain\IndexManagement;

use Webmozart\Assert\Assert;

final class DirectIndexConfiguration implements IndexConfigurationInterface
{
private function __construct(
Expand Down
4 changes: 0 additions & 4 deletions src/Infrastructure/Elastic/FakeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elasticsearch\Client;
use GuzzleHttp\Ring\Future\FutureArrayInterface;
use JeroenG\Explorer\Application\Results;
use JeroenG\Explorer\Application\SearchCommandInterface;
use Webmozart\Assert\Assert;

class FakeResponse
Expand Down
1 change: 0 additions & 1 deletion src/Infrastructure/Scout/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace JeroenG\Explorer\Infrastructure\Scout;

use JeroenG\Explorer\Application\Paginator;
use JeroenG\Explorer\Domain\Aggregations\AggregationSyntaxInterface;
use JeroenG\Explorer\Domain\Query\QueryProperties\QueryProperty;
use JeroenG\Explorer\Domain\Syntax\Compound\BoolQuery;
Expand Down
1 change: 0 additions & 1 deletion src/Infrastructure/Scout/ScoutSearchCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use JeroenG\Explorer\Domain\Syntax\Compound\QueryType;
use JeroenG\Explorer\Domain\Syntax\MultiMatch;
use JeroenG\Explorer\Domain\Syntax\Sort;
use JeroenG\Explorer\Domain\Syntax\SyntaxInterface;
use JeroenG\Explorer\Domain\Syntax\Term;
use JeroenG\Explorer\Domain\Syntax\Terms;
use Laravel\Scout\Builder;
Expand Down
Loading

0 comments on commit 8825d6f

Please sign in to comment.