Skip to content

Commit be81f20

Browse files
authored
Merge pull request #53 from ensi-platform/v7-inner-hits
Collaps Inner Hits and Search Aggregation
2 parents 04d7d1e + c816f5e commit be81f20

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

src/Search/Collapsing/Collapse.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
use Ensi\LaravelElasticQuery\Contracts\DSLAware;
66
use Webmozart\Assert\Assert;
77

8+
/**
9+
* @property string $field
10+
* @property InnerHits[] $innerHits
11+
*/
812
class Collapse implements DSLAware
913
{
10-
public function __construct(private string $field)
11-
{
14+
public function __construct(
15+
private string $field,
16+
private array $innerHits = [],
17+
) {
1218
Assert::stringNotEmpty(trim($field));
1319
}
1420

@@ -19,6 +25,13 @@ public function field(): string
1925

2026
public function toDSL(): array
2127
{
22-
return ['field' => $this->field];
28+
$dsl = ['field' => $this->field];
29+
30+
/** @var InnerHits $innerHit */
31+
foreach ($this->innerHits as $innerHit) {
32+
$dsl['inner_hits'][] = $innerHit->toDSL();
33+
}
34+
35+
return $dsl;
2336
}
2437
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Search\Collapsing;
4+
5+
use Ensi\LaravelElasticQuery\Contracts\DSLAware;
6+
use Ensi\LaravelElasticQuery\Search\Sorting\Sort;
7+
use Webmozart\Assert\Assert;
8+
9+
class InnerHits implements DSLAware
10+
{
11+
public function __construct(
12+
protected string $name,
13+
protected int $size,
14+
protected ?Sort $sort,
15+
) {
16+
Assert::stringNotEmpty(trim($name));
17+
}
18+
19+
public function toDSL(): array
20+
{
21+
$dsl = [
22+
'name' => $this->name,
23+
'size' => $this->size,
24+
];
25+
26+
if ($this->sort) {
27+
$dsl['sort'] = $this->sort->toDSL();
28+
}
29+
30+
return $dsl;
31+
}
32+
}

src/Search/CursorPage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class CursorPage
99
public function __construct(
1010
int $size,
1111
public Collection $hits,
12+
public Collection|null $aggs,
1213
public ?string $current = null,
1314
public ?string $next = null,
1415
public ?string $previous = null

src/Search/Page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Page
99
public function __construct(
1010
public int $size,
1111
public Collection $hits,
12+
public Collection|null $aggs,
1213
public int $offset = 0,
1314
public int $total = 0
1415
) {

src/Search/SearchQuery.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Ensi\LaravelElasticQuery\Search;
44

55
use Closure;
6+
use Ensi\LaravelElasticQuery\Aggregating\AggregationCollection;
67
use Ensi\LaravelElasticQuery\Concerns\DecoratesBoolQuery;
78
use Ensi\LaravelElasticQuery\Concerns\ExtendsSort;
9+
use Ensi\LaravelElasticQuery\Contracts\Aggregation;
810
use Ensi\LaravelElasticQuery\Contracts\CollapsibleQuery;
911
use Ensi\LaravelElasticQuery\Contracts\SearchIndex;
1012
use Ensi\LaravelElasticQuery\Contracts\SortableQuery;
@@ -25,6 +27,7 @@ class SearchQuery implements SortableQuery, CollapsibleQuery
2527
protected BoolQueryBuilder $boolQuery;
2628
protected SortCollection $sorts;
2729
protected ?Collapse $collapse = null;
30+
protected ?AggregationCollection $aggregations = null;
2831
protected ?int $size = null;
2932
protected ?int $from = null;
3033
protected array $fields = [];
@@ -60,6 +63,7 @@ public function paginate(int $size, int $offset = 0): Page
6063
return new Page(
6164
$size,
6265
$hits,
66+
aggs: $this->aggregations?->parseResults($response['aggregations'] ?? []),
6367
offset: $offset,
6468
total: data_get($response, 'hits.total.value', 0)
6569
);
@@ -82,6 +86,7 @@ public function cursorPaginate(int $size, ?string $cursor = null): CursorPage
8286
return new CursorPage(
8387
$size,
8488
$hits,
89+
aggs: $this->aggregations?->parseResults($response['aggregations'] ?? []),
8590
current: $current->encode(),
8691
next: $this->findNextCursor($sorts, $size, $hits),
8792
previous: $this->findPreviousCursor($sorts, $size, $current)
@@ -131,6 +136,10 @@ protected function execute(
131136
$dsl['sort'] = $sorts->toDSL();
132137
}
133138

139+
if (!is_null($this->aggregations)) {
140+
$dsl['aggs'] = $this->aggregations->toDSL();
141+
}
142+
134143
if (!is_null($this->collapse)) {
135144
$dsl['collapse'] = $this->collapse->toDSL();
136145
}
@@ -175,9 +184,17 @@ public function sortByNested(string $field, Closure $callback): static
175184
return $this;
176185
}
177186

178-
public function collapse(string $field): static
187+
public function collapse(string $field, array $innerHits = []): static
188+
{
189+
$this->collapse = new Collapse($field, $innerHits);
190+
191+
return $this;
192+
}
193+
194+
public function addAggregations(Aggregation $aggregation): static
179195
{
180-
$this->collapse = new Collapse($field);
196+
$this->aggregations ??= new AggregationCollection();
197+
$this->aggregations->add($aggregation);
181198

182199
return $this;
183200
}

0 commit comments

Comments
 (0)