diff --git a/src/Contracts/MultiSearchFederation.php b/src/Contracts/MultiSearchFederation.php index 3f9db1ec..1f063ab5 100644 --- a/src/Contracts/MultiSearchFederation.php +++ b/src/Contracts/MultiSearchFederation.php @@ -16,6 +16,16 @@ class MultiSearchFederation */ private ?int $offset = null; + /** + * @var array>|null + */ + private ?array $facetsByIndex = null; + + /** + * @var array|null + */ + private ?array $mergeFacets = null; + /** * @param non-negative-int $limit * @@ -40,10 +50,36 @@ public function setOffset(int $offset): self return $this; } + /** + * @param array> $facetsByIndex + * + * @return $this + */ + public function setFacetsByIndex(array $facetsByIndex): self + { + $this->facetsByIndex = $facetsByIndex; + + return $this; + } + + /** + * @param array $mergeFacets + * + * @return $this + */ + public function setMergeFacets(array $mergeFacets): self + { + $this->mergeFacets = $mergeFacets; + + return $this; + } + /** * @return array{ * limit?: non-negative-int, - * offset?: non-negative-int + * offset?: non-negative-int, + * facetsByIndex?: array>, + * mergeFacets?: array, * } */ public function toArray(): array @@ -51,6 +87,8 @@ public function toArray(): array return array_filter([ 'limit' => $this->limit, 'offset' => $this->offset, + 'facetsByIndex' => $this->facetsByIndex, + 'mergeFacets' => $this->mergeFacets, ], static function ($item) { return null !== $item; }); } } diff --git a/tests/Contracts/MultiSearchFederationTest.php b/tests/Contracts/MultiSearchFederationTest.php index 191b3692..ec002b21 100644 --- a/tests/Contracts/MultiSearchFederationTest.php +++ b/tests/Contracts/MultiSearchFederationTest.php @@ -29,4 +29,18 @@ public function testSetOffset(): void self::assertSame(['offset' => 5], $data->toArray()); } + + public function testSetFacetsByIndex(): void + { + $data = (new MultiSearchFederation())->setFacetsByIndex(['books' => ['author', 'genre']]); + + self::assertSame(['facetsByIndex' => ['books' => ['author', 'genre']]], $data->toArray()); + } + + public function testSetMergeFacets(): void + { + $data = (new MultiSearchFederation())->setMergeFacets(['maxValuesPerFacet' => 10]); + + self::assertSame(['mergeFacets' => ['maxValuesPerFacet' => 10]], $data->toArray()); + } } diff --git a/tests/Endpoints/MultiSearchTest.php b/tests/Endpoints/MultiSearchTest.php index 0352cae0..c6b7c909 100644 --- a/tests/Endpoints/MultiSearchTest.php +++ b/tests/Endpoints/MultiSearchTest.php @@ -89,7 +89,7 @@ public function testFederation(): void // By setting the weight to 0.9 this query should appear second ->setFederationOptions((new FederationOptions())->setWeight(0.9)), ], - (new MultiSearchFederation())->setLimit(2) + (new MultiSearchFederation())->setLimit(2)->setFacetsByIndex(['books' => ['genre']])->setMergeFacets(['maxValuesPerFacet' => 10]) ); self::assertArrayHasKey('hits', $response); @@ -97,6 +97,7 @@ public function testFederation(): void self::assertArrayHasKey('limit', $response); self::assertArrayHasKey('offset', $response); self::assertArrayHasKey('estimatedTotalHits', $response); + self::assertArrayHasKey('facetDistribution', $response); self::assertCount(2, $response['hits']); self::assertSame(2, $response['limit']); self::assertSame(0, $response['offset']);