From bfc80e69bb3582476db3706c11b8e49a71507f2c Mon Sep 17 00:00:00 2001 From: Julien JOURDE Date: Wed, 25 Sep 2024 08:47:45 +0200 Subject: [PATCH] Fix `with_payload` and `with_vector` params. --- src/Models/Request/ScrollRequest.php | 4 +- src/Models/Request/SearchRequest.php | 6 +- .../Collections/SearchPayloadTest.php | 118 ++++++++++++++++++ .../Collections/SearchVectorTest.php | 118 ++++++++++++++++++ 4 files changed, 241 insertions(+), 5 deletions(-) create mode 100644 tests/Integration/Endpoints/Collections/SearchPayloadTest.php create mode 100644 tests/Integration/Endpoints/Collections/SearchVectorTest.php diff --git a/src/Models/Request/ScrollRequest.php b/src/Models/Request/ScrollRequest.php index e55dd5d..b07bb62 100644 --- a/src/Models/Request/ScrollRequest.php +++ b/src/Models/Request/ScrollRequest.php @@ -77,10 +77,10 @@ public function toArray(): array if ($this->offset) { $body['offset'] = $this->offset; } - if ($this->withVector) { + if ($this->withVector !== null) { $body['with_vector'] = $this->withVector; } - if ($this->withPayload) { + if ($this->withPayload !== null) { $body['with_payload'] = $this->withPayload; } if ($this->orderBy) { diff --git a/src/Models/Request/SearchRequest.php b/src/Models/Request/SearchRequest.php index 4841332..ac0b479 100644 --- a/src/Models/Request/SearchRequest.php +++ b/src/Models/Request/SearchRequest.php @@ -112,14 +112,14 @@ public function toArray(): array if ($this->offset) { $body['offset'] = $this->offset; } - if ($this->withVector) { + if ($this->withVector !== null) { $body['with_vector'] = $this->withVector; } - if ($this->withPayload) { + if ($this->withPayload !== null) { $body['with_payload'] = $this->withPayload; } return $body; } -} \ No newline at end of file +} diff --git a/tests/Integration/Endpoints/Collections/SearchPayloadTest.php b/tests/Integration/Endpoints/Collections/SearchPayloadTest.php new file mode 100644 index 0000000..40b772d --- /dev/null +++ b/tests/Integration/Endpoints/Collections/SearchPayloadTest.php @@ -0,0 +1,118 @@ + + */ + +namespace Qdrant\Tests\Integration\Endpoints\Collections; + +use Qdrant\Endpoints\Collections; +use Qdrant\Exception\InvalidArgumentException; +use Qdrant\Models\PointsStruct; +use Qdrant\Models\Request\CreateIndex; +use Qdrant\Models\Request\ScrollRequest; +use Qdrant\Models\VectorStruct; +use Qdrant\Tests\Integration\AbstractIntegration; + +class SearchPayloadTest extends AbstractIntegration +{ + /** + * @throws InvalidArgumentException + */ + public function setUp(): void + { + parent::setUp(); + + $this->createCollections('sample-collection'); + + $response = $this->getCollections('sample-collection')->index()->create( + (new CreateIndex('payload', [ + 'type' => 'integer', + 'range' => true, + 'lookup' => false, + 'is_principal' => true + ])), + [ + 'wait' => 'true' + ] + ); + + $this->assertEquals('ok', $response['status']); + $this->assertEquals('completed', $response['result']['status']); + + $response = $this->getCollections('sample-collection')->points() + ->upsert( + PointsStruct::createFromArray(self::basicPointDataProvider()[0][0]), + [ + 'wait' => 'true' + ] + ); + + $this->assertEquals('ok', $response['status']); + $this->assertEquals('completed', $response['result']['status']); + } + + public static function basicPointDataProvider(): array + { + return [ + [ + [ + [ + 'id' => 1, + 'vector' => new VectorStruct([1, 3, 400], 'image'), + 'payload' => [ + 'sort' => 1, + 'color' => 'red' + ] + ], + [ + 'id' => 2, + 'vector' => new VectorStruct([1, 3, 300], 'image'), + 'payload' => [ + 'sort' => 2, + 'color' => 'red' + ] + ], + [ + 'id' => 3, + 'vector' => new VectorStruct([1, 3, 300], 'image'), + 'payload' => [ + 'sort' => 3, + 'color' => 'green' + ] + ], + ] + ] + ]; + } + + public function testScrollWithPayload(): void + { + $scroll = (new ScrollRequest())->setWithPayload(true); + $response = $this->getCollections('sample-collection')->points()->scroll($scroll); + + $this->assertEquals('ok', $response['status']); + $this->assertCount(2, $response['result']); + $this->assertCount(3, $response['result']['points']); + $this->assertArrayHasKey('payload', $response['result']['points'][0]); + } + + public function testScrollWithoutPayload(): void + { + $scroll = (new ScrollRequest())->setWithPayload(false); + $response = $this->getCollections('sample-collection')->points()->scroll($scroll); + + $this->assertEquals('ok', $response['status']); + $this->assertCount(2, $response['result']); + $this->assertCount(3, $response['result']['points']); + $this->assertArrayNotHasKey('payload', $response['result']['points'][0]); + } + + protected function tearDown(): void + { + parent::tearDown(); + $collections = new Collections($this->client); + + $collections->setCollectionName('sample-collection')->delete(); + } +} diff --git a/tests/Integration/Endpoints/Collections/SearchVectorTest.php b/tests/Integration/Endpoints/Collections/SearchVectorTest.php new file mode 100644 index 0000000..a95213f --- /dev/null +++ b/tests/Integration/Endpoints/Collections/SearchVectorTest.php @@ -0,0 +1,118 @@ + + */ + +namespace Qdrant\Tests\Integration\Endpoints\Collections; + +use Qdrant\Endpoints\Collections; +use Qdrant\Exception\InvalidArgumentException; +use Qdrant\Models\PointsStruct; +use Qdrant\Models\Request\CreateIndex; +use Qdrant\Models\Request\ScrollRequest; +use Qdrant\Models\VectorStruct; +use Qdrant\Tests\Integration\AbstractIntegration; + +class SearchVectorTest extends AbstractIntegration +{ + /** + * @throws InvalidArgumentException + */ + public function setUp(): void + { + parent::setUp(); + + $this->createCollections('sample-collection'); + + $response = $this->getCollections('sample-collection')->index()->create( + (new CreateIndex('payload', [ + 'type' => 'integer', + 'range' => true, + 'lookup' => false, + 'is_principal' => true + ])), + [ + 'wait' => 'true' + ] + ); + + $this->assertEquals('ok', $response['status']); + $this->assertEquals('completed', $response['result']['status']); + + $response = $this->getCollections('sample-collection')->points() + ->upsert( + PointsStruct::createFromArray(self::basicPointDataProvider()[0][0]), + [ + 'wait' => 'true' + ] + ); + + $this->assertEquals('ok', $response['status']); + $this->assertEquals('completed', $response['result']['status']); + } + + public static function basicPointDataProvider(): array + { + return [ + [ + [ + [ + 'id' => 1, + 'vector' => new VectorStruct([1, 3, 400], 'image'), + 'payload' => [ + 'sort' => 1, + 'color' => 'red' + ] + ], + [ + 'id' => 2, + 'vector' => new VectorStruct([1, 3, 300], 'image'), + 'payload' => [ + 'sort' => 2, + 'color' => 'red' + ] + ], + [ + 'id' => 3, + 'vector' => new VectorStruct([1, 3, 300], 'image'), + 'payload' => [ + 'sort' => 3, + 'color' => 'green' + ] + ], + ] + ] + ]; + } + + public function testScrollWithVector(): void + { + $scroll = (new ScrollRequest())->setWithVector(true); + $response = $this->getCollections('sample-collection')->points()->scroll($scroll); + + $this->assertEquals('ok', $response['status']); + $this->assertCount(2, $response['result']); + $this->assertCount(3, $response['result']['points']); + $this->assertArrayHasKey('vector', $response['result']['points'][0]); + } + + public function testScrollWithoutVector(): void + { + $scroll = (new ScrollRequest())->setWithPayload(false); + $response = $this->getCollections('sample-collection')->points()->scroll($scroll); + + $this->assertEquals('ok', $response['status']); + $this->assertCount(2, $response['result']); + $this->assertCount(3, $response['result']['points']); + $this->assertArrayNotHasKey('vector', $response['result']['points'][0]); + } + + protected function tearDown(): void + { + parent::tearDown(); + $collections = new Collections($this->client); + + $collections->setCollectionName('sample-collection')->delete(); + } +}