Skip to content
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
53 changes: 53 additions & 0 deletions src/Aggregating/Metrics/TopHitsAggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Ensi\LaravelElasticQuery\Aggregating\Metrics;

use Ensi\LaravelElasticQuery\Contracts\Aggregation;
use Ensi\LaravelElasticQuery\Search\Sorting\SortCollection;
use stdClass;
use Webmozart\Assert\Assert;

class TopHitsAggregation implements Aggregation
{
public function __construct(
private string $name,
private ?int $size = null,
private array $source = [],
private ?SortCollection $sort = null,
) {
Assert::stringNotEmpty(trim($name));
Assert::nullOrGreaterThan($this->size, 0);
}

public function name(): string
{
return $this->name;
}

public function parseResults(array $response): array
{
return [$this->name => $response[$this->name]['hits']['hits'] ?? []];
}

public function toDSL(): array
{
$body = [];

if ($this->size !== null) {
$body['size'] = $this->size;
}

if ($this->sort) {
$body['sort'] = $this->sort->toDSL();
}
if (!empty($this->source)) {
$body['_source'] = $this->source;
}

return [
$this->name => [
'top_hits' => empty($body) ? new stdClass() : $body,
],
];
}
}
30 changes: 30 additions & 0 deletions tests/IntegrationTests/AggregationQueryIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Ensi\LaravelElasticQuery\Aggregating\Bucket;
use Ensi\LaravelElasticQuery\Aggregating\Metrics\MinMaxScoreAggregation;
use Ensi\LaravelElasticQuery\Aggregating\Metrics\TopHitsAggregation;
use Ensi\LaravelElasticQuery\Aggregating\MinMax;
use Ensi\LaravelElasticQuery\Contracts\AggregationsBuilder;
use Ensi\LaravelElasticQuery\Search\Sorting\Sort;
Expand Down Expand Up @@ -53,6 +54,35 @@
);
});

test('aggregation query top hits', function () {
/** @var IntegrationTestCase $this */

$results = ProductsIndex::aggregate()
->terms(
name: 'group_by',
field: 'active',
composite: new TopHitsAggregation(
'top_products',
size: 10
)
)
->get();

$results = $results->get('group_by');

/** @var Bucket $result */
foreach ($results as $result) {
$groupByKey = $result->key;
/** @var array $products */
$products = $result->getCompositeValue('top_products');

array_walk($products, fn ($hit) => assertEquals($groupByKey, data_get($hit, '_source.active')));
$productIds = array_map(fn ($hit) => data_get($hit, '_source.product_id'), $products);

assertEqualsCanonicalizing($groupByKey ? [1, 150, 328, 405, 471] : [319], $productIds);
}
});

test('aggregation query cardinality', function () {
/** @var IntegrationTestCase $this */

Expand Down
Loading