From 03311e0421eb1dbc88dc09c74b8fb17addf3d7f7 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Tue, 8 Oct 2024 16:52:33 +0200 Subject: [PATCH 1/2] Add facet distribution settings to federated search --- src/Contracts/MultiSearchFederation.php | 35 ++++++++++++++++++- tests/Contracts/MultiSearchFederationTest.php | 14 ++++++++ tests/Endpoints/MultiSearchTest.php | 3 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Contracts/MultiSearchFederation.php b/src/Contracts/MultiSearchFederation.php index 3f9db1ec..79a6ff8f 100644 --- a/src/Contracts/MultiSearchFederation.php +++ b/src/Contracts/MultiSearchFederation.php @@ -16,6 +16,13 @@ class MultiSearchFederation */ private ?int $offset = null; + /** + * @var array>|null + */ + private ?array $facetsByIndex = null; + + private ?array $mergeFacets = null; + /** * @param non-negative-int $limit * @@ -40,10 +47,34 @@ public function setOffset(int $offset): self return $this; } + /** + * @param array> $facetsByIndex + * + * @return $this + */ + public function setFacetsByIndex(array $facetsByIndex): self + { + $this->facetsByIndex = $facetsByIndex; + + return $this; + } + + /** + * @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 +82,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..9d82ac9c 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([$this->booksIndex->getUid() => ['genre'], $this->songsIndex->getUid() => ['duration-float']])->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']); From 71ba8f7c1efaacc9301db6d4ac82728e5876482f Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Wed, 9 Oct 2024 10:00:53 +0200 Subject: [PATCH 2/2] Precise mergeFacets type --- src/Contracts/MultiSearchFederation.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Contracts/MultiSearchFederation.php b/src/Contracts/MultiSearchFederation.php index 79a6ff8f..d998aa37 100644 --- a/src/Contracts/MultiSearchFederation.php +++ b/src/Contracts/MultiSearchFederation.php @@ -21,6 +21,9 @@ class MultiSearchFederation */ private ?array $facetsByIndex = null; + /** + * @var array{maxValuesPerFacet: positive-int}|null + */ private ?array $mergeFacets = null; /** @@ -60,6 +63,8 @@ public function setFacetsByIndex(array $facetsByIndex): self } /** + * @param array{maxValuesPerFacet: positive-int} $mergeFacets + * * @return $this */ public function setMergeFacets(array $mergeFacets): self @@ -74,7 +79,7 @@ public function setMergeFacets(array $mergeFacets): self * limit?: non-negative-int, * offset?: non-negative-int, * facetsByIndex?: array>, - * mergeFacets?: array, + * mergeFacets?: array{maxValuesPerFacet: positive-int}, * } */ public function toArray(): array