Skip to content

Commit 54d7c51

Browse files
authored
Merge pull request #688 from meilisearch/multi-search-facet-distribution
Multi search facet distribution
2 parents 442a1fa + 71ba8f7 commit 54d7c51

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/Contracts/MultiSearchFederation.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ 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+
/**
25+
* @var array{maxValuesPerFacet: positive-int}|null
26+
*/
27+
private ?array $mergeFacets = null;
28+
1929
/**
2030
* @param non-negative-int $limit
2131
*
@@ -40,17 +50,45 @@ public function setOffset(int $offset): self
4050
return $this;
4151
}
4252

53+
/**
54+
* @param array<non-empty-string, list<non-empty-string>> $facetsByIndex
55+
*
56+
* @return $this
57+
*/
58+
public function setFacetsByIndex(array $facetsByIndex): self
59+
{
60+
$this->facetsByIndex = $facetsByIndex;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @param array{maxValuesPerFacet: positive-int} $mergeFacets
67+
*
68+
* @return $this
69+
*/
70+
public function setMergeFacets(array $mergeFacets): self
71+
{
72+
$this->mergeFacets = $mergeFacets;
73+
74+
return $this;
75+
}
76+
4377
/**
4478
* @return array{
4579
* limit?: non-negative-int,
46-
* offset?: non-negative-int
80+
* offset?: non-negative-int,
81+
* facetsByIndex?: array<non-empty-string, list<non-empty-string>>,
82+
* mergeFacets?: array{maxValuesPerFacet: positive-int},
4783
* }
4884
*/
4985
public function toArray(): array
5086
{
5187
return array_filter([
5288
'limit' => $this->limit,
5389
'offset' => $this->offset,
90+
'facetsByIndex' => $this->facetsByIndex,
91+
'mergeFacets' => $this->mergeFacets,
5492
], static function ($item) { return null !== $item; });
5593
}
5694
}

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)