Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested aggregations #241

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 21 additions & 5 deletions src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,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 +45,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;
bartjuh4 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading