From 7d9ea75953427bd21e29af6dec09cd760a67fdd5 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Thu, 20 Jun 2024 08:26:36 +0200 Subject: [PATCH 1/6] Add retrieveVector method to getSimilarDocuments --- docker-compose.yml | 2 +- src/Contracts/SimilarDocumentsQuery.php | 12 ++++++++++++ tests/Endpoints/SimilarDocumentsTest.php | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index ab90f7b0..13688304 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,7 +16,7 @@ services: - ./:/home/package meilisearch: - image: getmeili/meilisearch:latest + image: getmeili/meilisearch:v1.9.0-rc.3 ports: - "7700" environment: diff --git a/src/Contracts/SimilarDocumentsQuery.php b/src/Contracts/SimilarDocumentsQuery.php index 597d651a..ffca3dbe 100644 --- a/src/Contracts/SimilarDocumentsQuery.php +++ b/src/Contracts/SimilarDocumentsQuery.php @@ -16,6 +16,7 @@ class SimilarDocumentsQuery private ?array $attributesToRetrieve = null; private ?bool $showRankingScore = null; private ?bool $showRankingScoreDetails = null; + private ?bool $retrieveVectors = null; private ?array $filter = null; /** @@ -96,6 +97,16 @@ public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): Simi return $this; } + /** + * @param bool|null $retrieveVectors boolean value to show _vector details + */ + public function setRetrieveVectors(?bool $retrieveVectors): SimilarDocumentsQuery + { + $this->retrieveVectors = $retrieveVectors; + + return $this; + } + /** * @return array{id: int|string, offset: non-negative-int, limit: positive-int, filter: array|string>, embedder: non-empty-string, attributesToRetrieve: list, showRankingScore: bool, showRankingScoreDetails: bool} SimilarDocumentsQuery converted to an array with non null fields */ @@ -110,6 +121,7 @@ public function toArray(): array 'attributesToRetrieve' => $this->attributesToRetrieve, 'showRankingScore' => $this->showRankingScore, 'showRankingScoreDetails' => $this->showRankingScoreDetails, + 'retrieveVectors' => $this->retrieveVectors, ], function ($item) { return null !== $item; }); diff --git a/tests/Endpoints/SimilarDocumentsTest.php b/tests/Endpoints/SimilarDocumentsTest.php index abddf359..7d54048d 100644 --- a/tests/Endpoints/SimilarDocumentsTest.php +++ b/tests/Endpoints/SimilarDocumentsTest.php @@ -32,6 +32,14 @@ public function testBasicSearchWithSimilarDocuments(): void $documentId = $response->getHit(0)['id']; $response = $this->index->searchSimilarDocuments(new SimilarDocumentsQuery($documentId)); + 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); + $response = $this->index->searchSimilarDocuments($similarQuery->setRetrieveVectors(true)); + self::assertGreaterThanOrEqual(4, $response->getHitsCount()); self::assertArrayHasKey('_vectors', $response->getHit(0)); self::assertArrayHasKey('id', $response->getHit(0)); From ba788060d85e13b7cb818cfae77f112e5e1b475a Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Thu, 20 Jun 2024 09:38:56 +0200 Subject: [PATCH 2/6] Update vector search test checking `_vector` field --- tests/Endpoints/SearchTest.php | 16 ++++++++++++---- tests/Endpoints/SimilarDocumentsTest.php | 1 - 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/Endpoints/SearchTest.php b/tests/Endpoints/SearchTest.php index f695743d..60f1eaab 100644 --- a/tests/Endpoints/SearchTest.php +++ b/tests/Endpoints/SearchTest.php @@ -694,14 +694,22 @@ public function testVectorSearch(): void $http->patch('/experimental-features', ['vectorStore' => true]); $index = $this->createEmptyIndex($this->safeIndexName()); - $promise = $index->updateEmbedders(['default' => ['source' => 'userProvided', 'dimensions' => 1]]); + $promise = $index->updateEmbedders(['manual' => ['source' => 'userProvided', 'dimensions' => 3]]); $this->assertIsValidPromise($promise); $index->waitForTask($promise['taskUid']); + $promise = $index->updateDocuments(self::VECTOR_MOVIES); + $this->assertIsValidPromise($promise); + $index->waitForTask($promise['taskUid']); + + $response = $index->search('', ['vector' => [-0.5, 0.3, 0.85], 'hybrid' => ['semanticRatio' => 1.0]]); + + self::assertSame(5, $response->getSemanticHitCount()); + self::assertArrayNotHasKey('_vectors', $response->getHit(0)); - $response = $index->search('', ['vector' => [1], 'hybrid' => ['semanticRatio' => 1.0]]); + $response = $index->search('', ['vector' => [-0.5, 0.3, 0.85], 'hybrid' => ['semanticRatio' => 1.0], 'retrieveVectors' => true]); - self::assertSame(0, $response->getSemanticHitCount()); - self::assertEmpty($response->getHits()); + self::assertSame(5, $response->getSemanticHitCount()); + self::assertArrayHasKey('_vectors', $response->getHit(0)); } public function testShowRankingScoreDetails(): void diff --git a/tests/Endpoints/SimilarDocumentsTest.php b/tests/Endpoints/SimilarDocumentsTest.php index 7d54048d..20ce0048 100644 --- a/tests/Endpoints/SimilarDocumentsTest.php +++ b/tests/Endpoints/SimilarDocumentsTest.php @@ -39,7 +39,6 @@ public function testBasicSearchWithSimilarDocuments(): void $similarQuery = new SimilarDocumentsQuery($documentId); $response = $this->index->searchSimilarDocuments($similarQuery->setRetrieveVectors(true)); - self::assertGreaterThanOrEqual(4, $response->getHitsCount()); self::assertArrayHasKey('_vectors', $response->getHit(0)); self::assertArrayHasKey('id', $response->getHit(0)); From d50e88ca4fac5bf90475c56df4463cef1ba4866d Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Mon, 24 Jun 2024 07:22:31 +0200 Subject: [PATCH 3/6] Make document query request the vector --- src/Contracts/DocumentsQuery.php | 14 +++++++++++++- src/Contracts/SimilarDocumentsQuery.php | 2 +- tests/Endpoints/DocumentsTest.php | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Contracts/DocumentsQuery.php b/src/Contracts/DocumentsQuery.php index 2f92cb33..feff7dd7 100644 --- a/src/Contracts/DocumentsQuery.php +++ b/src/Contracts/DocumentsQuery.php @@ -10,6 +10,7 @@ class DocumentsQuery private int $limit; private array $fields; private array $filter; + private ?bool $retrieveVectors = null; public function setOffset(int $offset): DocumentsQuery { @@ -46,6 +47,16 @@ public function setFilter(array $filter): DocumentsQuery return $this; } + /** + * @param bool|null $retrieveVectors boolean value to show _vector details + */ + public function setRetrieveVectors(?bool $retrieveVectors): DocumentsQuery + { + $this->retrieveVectors = $retrieveVectors; + + return $this; + } + /** * Checks if the $filter attribute has been set. * @@ -84,6 +95,7 @@ public function toArray(): array 'limit' => $this->limit ?? null, 'filter' => $this->filter ?? null, 'fields' => $this->fields(), - ], function ($item) { return null != $item || is_numeric($item); }); + 'retrieveVectors' => ($this->retrieveVectors !== null ? ($this->retrieveVectors === true ? 'true' : 'false') : null), + ], function ($item) { return null !== $item; }); } } diff --git a/src/Contracts/SimilarDocumentsQuery.php b/src/Contracts/SimilarDocumentsQuery.php index ffca3dbe..9dbe3a2f 100644 --- a/src/Contracts/SimilarDocumentsQuery.php +++ b/src/Contracts/SimilarDocumentsQuery.php @@ -108,7 +108,7 @@ public function setRetrieveVectors(?bool $retrieveVectors): SimilarDocumentsQuer } /** - * @return array{id: int|string, offset: non-negative-int, limit: positive-int, filter: array|string>, embedder: non-empty-string, attributesToRetrieve: list, showRankingScore: bool, showRankingScoreDetails: bool} SimilarDocumentsQuery converted to an array with non null fields + * @return array{id: int|string, offset: non-negative-int, limit: positive-int, filter: array|string>, embedder: non-empty-string, attributesToRetrieve: list, showRankingScore: bool, showRankingScoreDetails: bool, retrieveVectors: bool} SimilarDocumentsQuery converted to an array with non null fields */ public function toArray(): array { diff --git a/tests/Endpoints/DocumentsTest.php b/tests/Endpoints/DocumentsTest.php index b253bdb0..ab0d1a09 100644 --- a/tests/Endpoints/DocumentsTest.php +++ b/tests/Endpoints/DocumentsTest.php @@ -12,6 +12,7 @@ use Meilisearch\Exceptions\InvalidResponseBodyException; use Meilisearch\Exceptions\JsonEncodingException; use Psr\Http\Message\ResponseInterface; +use Meilisearch\Http\Client; use Tests\TestCase; final class DocumentsTest extends TestCase @@ -624,6 +625,28 @@ public function testGetDocumentsWithoutFilterCorrectFieldsFormat(): void ); } + public function testGetDocumentsWithVector(): void + { + $http = new Client($this->host, getenv('MEILISEARCH_API_KEY')); + $http->patch('/experimental-features', ['vectorStore' => true]); + $index = $this->createEmptyIndex($this->safeIndexName('movies')); + + $promise = $index->updateEmbedders(['manual' => ['source' => 'userProvided', 'dimensions' => 3]]); + $this->assertIsValidPromise($promise); + $index->waitForTask($promise['taskUid']); + $promise = $index->updateDocuments(self::VECTOR_MOVIES); + $this->assertIsValidPromise($promise); + $index->waitForTask($promise['taskUid']); + + $response = $index->getDocuments(new DocumentsQuery()); + self::assertArrayNotHasKey('_vectors', $response->getResults()[0]); + $query = (new DocumentsQuery())->setRetrieveVectors(true); + $response = $index->getDocuments($query); + self::assertArrayHasKey('_vectors', $response->getResults()[0]); + + self::assertCount(5, $response); + } + public function testGetDocumentsMessageHintException(): void { $responseMock = $this->createMock(ResponseInterface::class); From 1aa580568414077037c775eab50ac17477bf539c Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Mon, 24 Jun 2024 07:39:47 +0200 Subject: [PATCH 4/6] Rollback compose version --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 13688304..ab90f7b0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,7 +16,7 @@ services: - ./:/home/package meilisearch: - image: getmeili/meilisearch:v1.9.0-rc.3 + image: getmeili/meilisearch:latest ports: - "7700" environment: From d494adbb3948c520b3815c03b4226018135718ba Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Mon, 24 Jun 2024 07:42:53 +0200 Subject: [PATCH 5/6] linter --- src/Contracts/DocumentsQuery.php | 2 +- tests/Endpoints/DocumentsTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Contracts/DocumentsQuery.php b/src/Contracts/DocumentsQuery.php index feff7dd7..e39475af 100644 --- a/src/Contracts/DocumentsQuery.php +++ b/src/Contracts/DocumentsQuery.php @@ -95,7 +95,7 @@ public function toArray(): array 'limit' => $this->limit ?? null, 'filter' => $this->filter ?? null, 'fields' => $this->fields(), - 'retrieveVectors' => ($this->retrieveVectors !== null ? ($this->retrieveVectors === true ? 'true' : 'false') : null), + 'retrieveVectors' => (null !== $this->retrieveVectors ? (true === $this->retrieveVectors ? 'true' : 'false') : null), ], function ($item) { return null !== $item; }); } } diff --git a/tests/Endpoints/DocumentsTest.php b/tests/Endpoints/DocumentsTest.php index ab0d1a09..0b1e3463 100644 --- a/tests/Endpoints/DocumentsTest.php +++ b/tests/Endpoints/DocumentsTest.php @@ -11,8 +11,8 @@ use Meilisearch\Exceptions\InvalidArgumentException; use Meilisearch\Exceptions\InvalidResponseBodyException; use Meilisearch\Exceptions\JsonEncodingException; -use Psr\Http\Message\ResponseInterface; use Meilisearch\Http\Client; +use Psr\Http\Message\ResponseInterface; use Tests\TestCase; final class DocumentsTest extends TestCase From e6f5468fc362ae080168f0f99e7e1d018d2cd400 Mon Sep 17 00:00:00 2001 From: Many the fish Date: Tue, 25 Jun 2024 13:52:55 +0200 Subject: [PATCH 6/6] Update src/Contracts/DocumentsQuery.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomas Norkūnas --- src/Contracts/DocumentsQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/DocumentsQuery.php b/src/Contracts/DocumentsQuery.php index e39475af..223986ba 100644 --- a/src/Contracts/DocumentsQuery.php +++ b/src/Contracts/DocumentsQuery.php @@ -95,7 +95,7 @@ public function toArray(): array 'limit' => $this->limit ?? null, 'filter' => $this->filter ?? null, 'fields' => $this->fields(), - 'retrieveVectors' => (null !== $this->retrieveVectors ? (true === $this->retrieveVectors ? 'true' : 'false') : null), + 'retrieveVectors' => (null !== $this->retrieveVectors ? ($this->retrieveVectors ? 'true' : 'false') : null), ], function ($item) { return null !== $item; }); } }