Skip to content

Commit

Permalink
Write tests for NestedFilteredAggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van Asselt committed Apr 25, 2024
1 parent d7b63f8 commit 5c24fff
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 18 deletions.
80 changes: 80 additions & 0 deletions src/Domain/Aggregations/NestedFilteredAggregation.php
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,
],
],
],
],
],
];
}
}
70 changes: 52 additions & 18 deletions tests/Unit/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use JeroenG\Explorer\Application\AggregationResult;
use JeroenG\Explorer\Application\SearchCommand;
use JeroenG\Explorer\Domain\Aggregations\MaxAggregation;
use JeroenG\Explorer\Domain\Aggregations\NestedFilteredAggregation;
use JeroenG\Explorer\Domain\Aggregations\TermsAggregation;
use JeroenG\Explorer\Domain\Query\Query;
use JeroenG\Explorer\Domain\Syntax\Compound\BoolQuery;
Expand Down Expand Up @@ -83,15 +84,15 @@ public function test_it_accepts_must_should_filter_and_where_queries(): void
'query' => [
'bool' => [
'must' => [
['match' => ['title' => [ 'query' => 'Lorem Ipsum', 'fuzziness' => 'auto']]],
['match' => ['title' => ['query' => 'Lorem Ipsum', 'fuzziness' => 'auto']]],
['multi_match' => ['query' => 'fuzzy search', 'fuzziness' => 'auto']],
],
'should' => [
['match' => ['text' => [ 'query' => 'consectetur adipiscing elit', 'fuzziness' => 'auto']]],
['match' => ['text' => ['query' => 'consectetur adipiscing elit', 'fuzziness' => 'auto']]],
],
'filter' => [
['term' => ['published' => [ 'value' => true, 'boost' => 1.0]]],
['term' => ['subtitle' => [ 'value' => 'Dolor sit amet', 'boost' => 1.0]]],
['term' => ['published' => ['value' => true, 'boost' => 1.0]]],
['term' => ['subtitle' => ['value' => 'Dolor sit amet', 'boost' => 1.0]]],
['terms' => ['tags' => ['t1', 't2'], 'boost' => 1.0]],
],
],
Expand Down Expand Up @@ -241,7 +242,7 @@ public function test_it_builds_with_default_fields(): void
'query' => [
'bool' => [
'must' => [
['multi_match' => ['query' => 'fuzzy search', 'fields' => self::SEARCHABLE_FIELDS, 'fuzziness' => 'auto' ]],
['multi_match' => ['query' => 'fuzzy search', 'fields' => self::SEARCHABLE_FIELDS, 'fuzziness' => 'auto']],
],
'should' => [],
'filter' => [],
Expand Down Expand Up @@ -281,7 +282,7 @@ public function test_it_adds_fields_to_query(): void
'filter' => [],
],
],
'fields' => ['*.length', 'specific.field']
'fields' => ['*.length', 'specific.field'],
],
])
->andReturn([
Expand Down Expand Up @@ -331,18 +332,18 @@ public function test_it_adds_aggregates(): void
'aggregations' => [
'specificAggregation' => [
'buckets' => [
['key' => 'myKey', 'doc_count' => 42]
]
['key' => 'myKey', 'doc_count' => 42],
],
],
'anotherAggregation' => [
'buckets' => [
['key' => 'anotherKey', 'doc_count' => 6]
]
['key' => 'anotherKey', 'doc_count' => 6],
],
],
'metricAggregation' => [
'value' => 10,
]
]
],
],
]);

$query = Query::with(new BoolQuery());
Expand Down Expand Up @@ -402,7 +403,24 @@ public function test_it_adds_nested_aggregations(): void
],
],
],
'anotherAggregation' => ['terms' => ['field' => 'anotherField', 'size' => 10]]
'nestedFilteredAggregation' => [
'nested' => [
'path' => 'nestedFilteredAggregation',
],
'aggs' => [
'filter_aggs' => [
'aggs' => [
'someFieldFiltered' => [
'terms' => [
'field' => 'nestedFilteredAggregation.someField',
'size' => 10,
],
],
],
],
],
],
'anotherAggregation' => ['terms' => ['field' => 'anotherField', 'size' => 10]],
],
],
])
Expand All @@ -418,14 +436,29 @@ public function test_it_adds_nested_aggregations(): void
'doc_count_error_upper_bound' => 0,
'sum_other_doc_count' => 0,
'buckets' => [
['key' => 'someKey', 'doc_count' => 6,]
['key' => 'someKey', 'doc_count' => 6,],
],
],
],

'nestedFilteredAggregation' => [
'doc_count' => 42,
'filter_aggs' => [
'doc_count' => 42,
'someFieldFiltered' => [
'doc_count_error_upper_bound' => 0,
'sum_other_doc_count' => 0,
'buckets' => [
['key' => 'someKey', 'doc_count' => 6,],
],
],
],
],

'specificAggregation' => [
'buckets' => [
['key' => 'myKey', 'doc_count' => 42]
]
['key' => 'myKey', 'doc_count' => 42],
],
],
],
]);
Expand All @@ -434,14 +467,15 @@ public function test_it_adds_nested_aggregations(): void
$query->addAggregation('anotherAggregation', new TermsAggregation('anotherField'));
$nestedAggregation = new NestedAggregation('nestedAggregation');
$nestedAggregation->add('someField', new TermsAggregation('nestedAggregation.someField'));
$query->addAggregation('nestedAggregation',$nestedAggregation);
$query->addAggregation('nestedAggregation', $nestedAggregation);
$query->addAggregation('anotherAggregation', new NestedFilteredAggregation('filter_aggs', 'nestedFilteredAggregation.someField', []));
$builder = new SearchCommand(self::TEST_INDEX, $query);
$builder->setIndex(self::TEST_INDEX);

$subject = new Finder($client, $builder);
$results = $subject->find();

self::assertCount(2, $results->aggregations());
self::assertCount(3, $results->aggregations());

$nestedAggregation = $results->aggregations()[0];

Expand Down

0 comments on commit 5c24fff

Please sign in to comment.