Skip to content

Commit d35c515

Browse files
authored
Merge pull request #10 from smoench/field-collapsing
add field collapsing
2 parents b9187e5 + 60204d8 commit d35c515

File tree

11 files changed

+389
-71
lines changed

11 files changed

+389
-71
lines changed

src/FieldAwareTrait.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/FieldCollapse/FieldCollapse.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenSearchDSL\FieldCollapse;
6+
7+
use OpenSearchDSL\BuilderInterface;
8+
use OpenSearchDSL\ParametersTrait;
9+
use stdClass;
10+
11+
class FieldCollapse implements BuilderInterface
12+
{
13+
use ParametersTrait;
14+
15+
/**
16+
* @var list<InnerHits>
17+
*/
18+
private array $innerHits = [];
19+
20+
public function __construct(
21+
private readonly string $field
22+
) {
23+
}
24+
25+
public function addInnerHits(InnerHits $innerHits): self
26+
{
27+
$this->innerHits[] = $innerHits;
28+
29+
return $this;
30+
}
31+
32+
public function getType(): string
33+
{
34+
return 'collapse';
35+
}
36+
37+
public function toArray(): array|stdClass
38+
{
39+
$array = [
40+
'field' => $this->field,
41+
];
42+
43+
if ($this->innerHits !== []) {
44+
$array['inner_hits'] = array_map(
45+
static fn (InnerHits $innerHits) => $innerHits->toArray(),
46+
$this->innerHits
47+
);
48+
}
49+
50+
return $this->processArray($array);
51+
}
52+
}

src/FieldCollapse/InnerHits.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenSearchDSL\FieldCollapse;
6+
7+
use OpenSearchDSL\BuilderInterface;
8+
use OpenSearchDSL\ParametersTrait;
9+
10+
class InnerHits implements BuilderInterface
11+
{
12+
use ParametersTrait;
13+
14+
private ?FieldCollapse $collapse = null;
15+
16+
public function __construct(
17+
private readonly string $name,
18+
) {
19+
}
20+
21+
public function setFieldCollapse(FieldCollapse $collapse): self
22+
{
23+
$this->collapse = $collapse;
24+
25+
return $this;
26+
}
27+
28+
public function getType(): string
29+
{
30+
return 'inner_hits';
31+
}
32+
33+
public function toArray(): array
34+
{
35+
$array = [
36+
'name' => $this->name,
37+
];
38+
39+
if ($this->collapse instanceof FieldCollapse) {
40+
$array['collapse'] = $this->collapse->toArray();
41+
}
42+
43+
return $this->processArray($array);
44+
}
45+
}

src/Search.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
use InvalidArgumentException;
1717
use OpenSearchDSL\Aggregation\AbstractAggregation;
18+
use OpenSearchDSL\FieldCollapse\FieldCollapse;
1819
use OpenSearchDSL\Highlight\Highlight;
1920
use OpenSearchDSL\InnerHit\NestedInnerHit;
2021
use OpenSearchDSL\Query\Compound\BoolQuery;
2122
use OpenSearchDSL\SearchEndpoint\AbstractSearchEndpoint;
2223
use OpenSearchDSL\SearchEndpoint\AggregationsEndpoint;
24+
use OpenSearchDSL\SearchEndpoint\CollapseEndpoint;
2325
use OpenSearchDSL\SearchEndpoint\HighlightEndpoint;
2426
use OpenSearchDSL\SearchEndpoint\InnerHitsEndpoint;
2527
use OpenSearchDSL\SearchEndpoint\PostFilterEndpoint;
@@ -188,9 +190,7 @@ private function getEndpoint(string $type): SearchEndpointInterface
188190
*/
189191
public function getQueries(): ?BoolQuery
190192
{
191-
$endpoint = $this->getEndpoint(QueryEndpoint::NAME);
192-
193-
return $endpoint->getBool();
193+
return $this->getEndpoint(QueryEndpoint::NAME)->getBool();
194194
}
195195

196196
/**
@@ -237,9 +237,7 @@ public function addPostFilter(
237237
*/
238238
public function getPostFilters(): ?BoolQuery
239239
{
240-
$endpoint = $this->getEndpoint(PostFilterEndpoint::NAME);
241-
242-
return $endpoint->getBool();
240+
return $this->getEndpoint(PostFilterEndpoint::NAME)->getBool();
243241
}
244242

245243
/**
@@ -325,7 +323,7 @@ public function addHighlight(Highlight $highlight): self
325323
/**
326324
* Returns highlight builder.
327325
*/
328-
public function getHighlights(): ?BuilderInterface
326+
public function getHighlights(): ?Highlight
329327
{
330328
/** @var HighlightEndpoint $highlightEndpoint */
331329
$highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME);
@@ -353,6 +351,24 @@ public function getSuggests(): array
353351
return $this->getEndpoint(SuggestEndpoint::NAME)->getAll();
354352
}
355353

354+
/**
355+
* Allows to highlight search results on one or more fields.
356+
*/
357+
public function addCollapse(FieldCollapse $collapse): self
358+
{
359+
$this->getEndpoint(CollapseEndpoint::NAME)->add($collapse, 'collapse');
360+
361+
return $this;
362+
}
363+
364+
public function getCollapse(): ?FieldCollapse
365+
{
366+
/** @var CollapseEndpoint $endpoint */
367+
$endpoint = $this->getEndpoint(CollapseEndpoint::NAME);
368+
369+
return $endpoint->getCollapse();
370+
}
371+
356372
public function getFrom(): ?int
357373
{
358374
return $this->from;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenSearchDSL\SearchEndpoint;
6+
7+
use InvalidArgumentException;
8+
use OpenSearchDSL\BuilderInterface;
9+
use OpenSearchDSL\FieldCollapse\FieldCollapse;
10+
use OverflowException;
11+
12+
class CollapseEndpoint extends AbstractSearchEndpoint
13+
{
14+
final public const NAME = 'collapse';
15+
16+
private ?FieldCollapse $collapse = null;
17+
18+
private ?string $key = null;
19+
20+
public function normalize(): ?array
21+
{
22+
if ($this->collapse instanceof FieldCollapse) {
23+
return $this->collapse->toArray();
24+
}
25+
26+
return null;
27+
}
28+
29+
public function add(BuilderInterface $builder, ?string $key = null): string
30+
{
31+
if (! $builder instanceof FieldCollapse) {
32+
throw new InvalidArgumentException('Only FieldCollapse can be added');
33+
}
34+
if ($this->collapse instanceof FieldCollapse) {
35+
throw new OverflowException('Only one highlight can be set');
36+
}
37+
38+
if (! $key) {
39+
$key = bin2hex(random_bytes(30));
40+
}
41+
42+
$this->key = $key;
43+
$this->collapse = $builder;
44+
45+
return $this->key;
46+
}
47+
48+
public function getAll(?string $boolType = null): array
49+
{
50+
return [
51+
$this->key => $this->collapse,
52+
];
53+
}
54+
55+
public function getCollapse(): ?FieldCollapse
56+
{
57+
return $this->collapse;
58+
}
59+
}

src/SearchEndpoint/HighlightEndpoint.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,25 @@
1313

1414
namespace OpenSearchDSL\SearchEndpoint;
1515

16+
use InvalidArgumentException;
1617
use OpenSearchDSL\BuilderInterface;
18+
use OpenSearchDSL\Highlight\Highlight;
1719
use OverflowException;
1820

1921
/**
2022
* Search highlight dsl endpoint.
2123
*/
2224
class HighlightEndpoint extends AbstractSearchEndpoint
2325
{
24-
/**
25-
* Endpoint name
26-
*/
2726
final public const NAME = 'highlight';
2827

29-
private ?BuilderInterface $highlight = null;
28+
private ?Highlight $highlight = null;
3029

31-
/**
32-
* @var string|null Key for highlight storing.
33-
*/
3430
private ?string $key = null;
3531

3632
public function normalize(): ?array
3733
{
38-
if ($this->highlight instanceof \OpenSearchDSL\BuilderInterface) {
34+
if ($this->highlight instanceof Highlight) {
3935
return $this->highlight->toArray();
4036
}
4137

@@ -44,7 +40,10 @@ public function normalize(): ?array
4440

4541
public function add(BuilderInterface $builder, ?string $key = null): string
4642
{
47-
if ($this->highlight instanceof \OpenSearchDSL\BuilderInterface) {
43+
if (! $builder instanceof Highlight) {
44+
throw new InvalidArgumentException('Only highlight can be added');
45+
}
46+
if ($this->highlight instanceof Highlight) {
4847
throw new OverflowException('Only one highlight can be set');
4948
}
5049

@@ -65,7 +64,7 @@ public function getAll(?string $boolType = null): array
6564
];
6665
}
6766

68-
public function getHighlight(): ?BuilderInterface
67+
public function getHighlight(): ?Highlight
6968
{
7069
return $this->highlight;
7170
}

src/SearchEndpoint/SearchEndpointFactory.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020
*/
2121
class SearchEndpointFactory
2222
{
23-
/**
24-
* @var array Holds namespaces for endpoints.
25-
*/
2623
private static array $endpoints = [
27-
'query' => QueryEndpoint::class,
28-
'post_filter' => PostFilterEndpoint::class,
29-
'sort' => SortEndpoint::class,
30-
'highlight' => HighlightEndpoint::class,
31-
'aggregations' => AggregationsEndpoint::class,
32-
'suggest' => SuggestEndpoint::class,
33-
'inner_hits' => InnerHitsEndpoint::class,
24+
QueryEndpoint::NAME => QueryEndpoint::class,
25+
PostFilterEndpoint::NAME => PostFilterEndpoint::class,
26+
SortEndpoint::NAME => SortEndpoint::class,
27+
HighlightEndpoint::NAME => HighlightEndpoint::class,
28+
AggregationsEndpoint::NAME => AggregationsEndpoint::class,
29+
SuggestEndpoint::NAME => SuggestEndpoint::class,
30+
InnerHitsEndpoint::NAME => InnerHitsEndpoint::class,
31+
CollapseEndpoint::NAME => CollapseEndpoint::class,
3432
];
3533

3634
/**

0 commit comments

Comments
 (0)