From 433962b4dc5f408fc5750ce83c8df50ca6e323a3 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Tue, 8 Oct 2024 15:53:37 +0200 Subject: [PATCH] Make embedder field mandatory in SimilarDocumentsQuery --- .code-samples.meilisearch.yaml | 2 +- src/Contracts/SimilarDocumentsQuery.php | 32 ++++-------- tests/Contracts/SimilarDocumentsQueryTest.php | 50 +++++++++---------- tests/Endpoints/SimilarDocumentsTest.php | 4 +- 4 files changed, 39 insertions(+), 49 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 35697bcf..535134d8 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -742,7 +742,7 @@ distinct_attribute_guide_distinct_parameter_1: |- search_parameter_guide_matching_strategy_3: |- $client->index('movies')->search('white shirt', ['matchingStrategy' => 'frequency']); get_similar_post_1: |- - $similarQuery = new SimilarDocumentsQuery('TARGET_DOCUMENT_ID'); + $similarQuery = new SimilarDocumentsQuery('TARGET_DOCUMENT_ID', 'default'); $client->index('INDEX_NAME')->searchSimilarDocuments($similarQuery); multi_search_federated_1: |- $client->multiSearch([ diff --git a/src/Contracts/SimilarDocumentsQuery.php b/src/Contracts/SimilarDocumentsQuery.php index ab6dc95b..04c65ae3 100644 --- a/src/Contracts/SimilarDocumentsQuery.php +++ b/src/Contracts/SimilarDocumentsQuery.php @@ -11,6 +11,11 @@ class SimilarDocumentsQuery */ private $id; + /** + * @var non-empty-string + */ + private string $embedder; + /** * @var non-negative-int|null */ @@ -21,11 +26,6 @@ class SimilarDocumentsQuery */ private ?int $limit = null; - /** - * @var non-empty-string|null - */ - private ?string $embedder = null; - /** * @var list|null */ @@ -48,11 +48,13 @@ class SimilarDocumentsQuery private $rankingScoreThreshold; /** - * @param int|string $id + * @param int|string $id + * @param non-empty-string $embedder */ - public function __construct($id) + public function __construct($id, string $embedder) { $this->id = $id; + $this->embedder = $embedder; } /** @@ -91,18 +93,6 @@ public function setFilter(array $filter): self return $this; } - /** - * @param non-empty-string $embedder - * - * @return $this - */ - public function setEmbedder(string $embedder): self - { - $this->embedder = $embedder; - - return $this; - } - /** * @param list $attributesToRetrieve an array of attribute names to retrieve * @@ -166,10 +156,10 @@ public function setRankingScoreThreshold($rankingScoreThreshold): self /** * @return array{ * id: int|string, + * embedder: non-empty-string, * offset?: non-negative-int, * limit?: positive-int, * filter?: array|string>, - * embedder?: non-empty-string, * attributesToRetrieve?: list, * showRankingScore?: bool, * showRankingScoreDetails?: bool, @@ -181,10 +171,10 @@ public function toArray(): array { return array_filter([ 'id' => $this->id, + 'embedder' => $this->embedder, 'offset' => $this->offset, 'limit' => $this->limit, 'filter' => $this->filter, - 'embedder' => $this->embedder, 'attributesToRetrieve' => $this->attributesToRetrieve, 'showRankingScore' => $this->showRankingScore, 'showRankingScoreDetails' => $this->showRankingScoreDetails, diff --git a/tests/Contracts/SimilarDocumentsQueryTest.php b/tests/Contracts/SimilarDocumentsQueryTest.php index 79785d92..2de46338 100644 --- a/tests/Contracts/SimilarDocumentsQueryTest.php +++ b/tests/Contracts/SimilarDocumentsQueryTest.php @@ -10,54 +10,54 @@ final class SimilarDocumentsQueryTest extends TestCase { /** - * @param int|string $id - * - * @testWith [123] - * ["test"] + * @param int|string $id + * @param non-empty-string $embedder + * @testWith [123, "default"] + * ["test", "manual"] */ - public function testConstruct($id): void + public function testConstruct($id, string $embedder): void { - $data = new SimilarDocumentsQuery($id); + $data = new SimilarDocumentsQuery($id, $embedder); - self::assertSame(['id' => $id], $data->toArray()); + self::assertSame(['id' => $id, 'embedder' => $embedder], $data->toArray()); } public function testSetOffset(): void { - $data = (new SimilarDocumentsQuery('test'))->setOffset(66); + $data = (new SimilarDocumentsQuery('test', 'default'))->setOffset(66); - self::assertSame(['id' => 'test', 'offset' => 66], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'offset' => 66], $data->toArray()); } public function testSetLimit(): void { - $data = (new SimilarDocumentsQuery('test'))->setLimit(50); + $data = (new SimilarDocumentsQuery('test', 'default'))->setLimit(50); - self::assertSame(['id' => 'test', 'limit' => 50], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'limit' => 50], $data->toArray()); } public function testSetFilter(): void { - $data = (new SimilarDocumentsQuery('test'))->setFilter([ + $data = (new SimilarDocumentsQuery('test', 'default'))->setFilter([ ['genres = horror', 'genres = mystery'], "director = 'Jordan Peele'", ]); - self::assertSame(['id' => 'test', 'filter' => [['genres = horror', 'genres = mystery'], "director = 'Jordan Peele'"]], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'filter' => [['genres = horror', 'genres = mystery'], "director = 'Jordan Peele'"]], $data->toArray()); } public function testSetEmbedder(): void { - $data = (new SimilarDocumentsQuery('test'))->setEmbedder('default'); + $data = (new SimilarDocumentsQuery('test', 'default'))->setEmbedder('manual'); - self::assertSame(['id' => 'test', 'embedder' => 'default'], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'manual'], $data->toArray()); } public function testSetAttributesToRetrieve(): void { - $data = (new SimilarDocumentsQuery('test'))->setAttributesToRetrieve(['name', 'price']); + $data = (new SimilarDocumentsQuery('test', 'default'))->setAttributesToRetrieve(['name', 'price']); - self::assertSame(['id' => 'test', 'attributesToRetrieve' => ['name', 'price']], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'attributesToRetrieve' => ['name', 'price']], $data->toArray()); } /** @@ -66,9 +66,9 @@ public function testSetAttributesToRetrieve(): void */ public function testSetShowRankingScore(bool $showRankingScore): void { - $data = (new SimilarDocumentsQuery('test'))->setShowRankingScore($showRankingScore); + $data = (new SimilarDocumentsQuery('test', 'default'))->setShowRankingScore($showRankingScore); - self::assertSame(['id' => 'test', 'showRankingScore' => $showRankingScore], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'showRankingScore' => $showRankingScore], $data->toArray()); } /** @@ -77,9 +77,9 @@ public function testSetShowRankingScore(bool $showRankingScore): void */ public function testSetShowRankingScoreDetails(bool $showRankingScoreDetails): void { - $data = (new SimilarDocumentsQuery('test'))->setShowRankingScoreDetails($showRankingScoreDetails); + $data = (new SimilarDocumentsQuery('test', 'default'))->setShowRankingScoreDetails($showRankingScoreDetails); - self::assertSame(['id' => 'test', 'showRankingScoreDetails' => $showRankingScoreDetails], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'showRankingScoreDetails' => $showRankingScoreDetails], $data->toArray()); } /** @@ -88,9 +88,9 @@ public function testSetShowRankingScoreDetails(bool $showRankingScoreDetails): v */ public function testSetRetrieveVectors(bool $retrieveVectors): void { - $data = (new SimilarDocumentsQuery('test'))->setRetrieveVectors($retrieveVectors); + $data = (new SimilarDocumentsQuery('test', 'default'))->setRetrieveVectors($retrieveVectors); - self::assertSame(['id' => 'test', 'retrieveVectors' => $retrieveVectors], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'retrieveVectors' => $retrieveVectors], $data->toArray()); } /** @@ -101,8 +101,8 @@ public function testSetRetrieveVectors(bool $retrieveVectors): void */ public function testSetRankingScoreThreshold($rankingScoreThreshold): void { - $data = (new SimilarDocumentsQuery('test'))->setRankingScoreThreshold($rankingScoreThreshold); + $data = (new SimilarDocumentsQuery('test', 'default'))->setRankingScoreThreshold($rankingScoreThreshold); - self::assertSame(['id' => 'test', 'rankingScoreThreshold' => $rankingScoreThreshold], $data->toArray()); + self::assertSame(['id' => 'test', 'embedder' => 'default', 'rankingScoreThreshold' => $rankingScoreThreshold], $data->toArray()); } } diff --git a/tests/Endpoints/SimilarDocumentsTest.php b/tests/Endpoints/SimilarDocumentsTest.php index 20ce0048..c5d6e40c 100644 --- a/tests/Endpoints/SimilarDocumentsTest.php +++ b/tests/Endpoints/SimilarDocumentsTest.php @@ -30,14 +30,14 @@ public function testBasicSearchWithSimilarDocuments(): void self::assertSame(1, $response->getHitsCount()); $documentId = $response->getHit(0)['id']; - $response = $this->index->searchSimilarDocuments(new SimilarDocumentsQuery($documentId)); + $response = $this->index->searchSimilarDocuments(new SimilarDocumentsQuery($documentId, 'manual')); self::assertGreaterThanOrEqual(4, $response->getHitsCount()); self::assertArrayNotHasKey('_vectors', $response->getHit(0)); self::assertArrayHasKey('id', $response->getHit(0)); self::assertSame($documentId, $response->getId()); - $similarQuery = new SimilarDocumentsQuery($documentId); + $similarQuery = new SimilarDocumentsQuery($documentId, 'manual'); $response = $this->index->searchSimilarDocuments($similarQuery->setRetrieveVectors(true)); self::assertGreaterThanOrEqual(4, $response->getHitsCount()); self::assertArrayHasKey('_vectors', $response->getHit(0));