Skip to content

Commit 03311e0

Browse files
committed
Add facet distribution settings to federated search
1 parent 6e038a3 commit 03311e0

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/Contracts/MultiSearchFederation.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class MultiSearchFederation
1616
*/
1717
private ?int $offset = null;
1818

19+
/**
20+
* @var array<non-empty-string, list<non-empty-string>>|null
21+
*/
22+
private ?array $facetsByIndex = null;
23+
24+
private ?array $mergeFacets = null;
25+
1926
/**
2027
* @param non-negative-int $limit
2128
*
@@ -40,17 +47,43 @@ public function setOffset(int $offset): self
4047
return $this;
4148
}
4249

50+
/**
51+
* @param array<non-empty-string, list<non-empty-string>> $facetsByIndex
52+
*
53+
* @return $this
54+
*/
55+
public function setFacetsByIndex(array $facetsByIndex): self
56+
{
57+
$this->facetsByIndex = $facetsByIndex;
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* @return $this
64+
*/
65+
public function setMergeFacets(array $mergeFacets): self
66+
{
67+
$this->mergeFacets = $mergeFacets;
68+
69+
return $this;
70+
}
71+
4372
/**
4473
* @return array{
4574
* limit?: non-negative-int,
46-
* offset?: non-negative-int
75+
* offset?: non-negative-int,
76+
* facetsByIndex?: array<non-empty-string, list<non-empty-string>>,
77+
* mergeFacets?: array,
4778
* }
4879
*/
4980
public function toArray(): array
5081
{
5182
return array_filter([
5283
'limit' => $this->limit,
5384
'offset' => $this->offset,
85+
'facetsByIndex' => $this->facetsByIndex,
86+
'mergeFacets' => $this->mergeFacets,
5487
], static function ($item) { return null !== $item; });
5588
}
5689
}

tests/Contracts/MultiSearchFederationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,18 @@ public function testSetOffset(): void
2929

3030
self::assertSame(['offset' => 5], $data->toArray());
3131
}
32+
33+
public function testSetFacetsByIndex(): void
34+
{
35+
$data = (new MultiSearchFederation())->setFacetsByIndex(['books' => ['author', 'genre']]);
36+
37+
self::assertSame(['facetsByIndex' => ['books' => ['author', 'genre']]], $data->toArray());
38+
}
39+
40+
public function testSetMergeFacets(): void
41+
{
42+
$data = (new MultiSearchFederation())->setMergeFacets(['maxValuesPerFacet' => 10]);
43+
44+
self::assertSame(['mergeFacets' => ['maxValuesPerFacet' => 10]], $data->toArray());
45+
}
3246
}

tests/Endpoints/MultiSearchTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ public function testFederation(): void
8989
// By setting the weight to 0.9 this query should appear second
9090
->setFederationOptions((new FederationOptions())->setWeight(0.9)),
9191
],
92-
(new MultiSearchFederation())->setLimit(2)
92+
(new MultiSearchFederation())->setLimit(2)->setFacetsByIndex([$this->booksIndex->getUid() => ['genre'], $this->songsIndex->getUid() => ['duration-float']])->setMergeFacets(['maxValuesPerFacet' => 10])
9393
);
9494

9595
self::assertArrayHasKey('hits', $response);
9696
self::assertArrayHasKey('processingTimeMs', $response);
9797
self::assertArrayHasKey('limit', $response);
9898
self::assertArrayHasKey('offset', $response);
9999
self::assertArrayHasKey('estimatedTotalHits', $response);
100+
self::assertArrayHasKey('facetDistribution', $response);
100101
self::assertCount(2, $response['hits']);
101102
self::assertSame(2, $response['limit']);
102103
self::assertSame(0, $response['offset']);

0 commit comments

Comments
 (0)