Skip to content

Commit

Permalink
Merge pull request #56 from hkulekci/scroll-implementation
Browse files Browse the repository at this point in the history
sort implementation for scroll endpoint
  • Loading branch information
hkulekci authored Aug 21, 2024
2 parents 7c5371d + 960300a commit 411d60b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Endpoints/HttpFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function createRequest(string $method, string $uri, array $body = []):

protected function queryBuild(array $params): string
{
return '?' . http_build_query($params);
$paramStr = http_build_query($params);
return $paramStr ? '?' . $paramStr : '';
}
}
19 changes: 15 additions & 4 deletions src/Models/Request/CreateIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@

class CreateIndex implements RequestModel
{
public function __construct(protected string $fieldName, protected string $fieldSchema, protected array $schemaParams = [])
protected string $fieldName;
protected ?array $fieldSchema;

public function __construct(string $fieldName, array|string $fieldSchema = null)
{
if (is_string($fieldSchema)) {
$this->fieldSchema = [
'type' => $fieldSchema
];
} else {
$this->fieldSchema = $fieldSchema;
}

$this->fieldName = $fieldName;
}

public function toArray(): array
{
// TODO: schema params is missing
return [
return array_filter([
'field_name' => $this->fieldName,
'field_schema' => $this->fieldSchema
];
]);
}
}
12 changes: 12 additions & 0 deletions src/Models/Request/ScrollRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ScrollRequest implements RequestModel

protected bool|array|null $withPayload = null;

protected string|array|null $orderBy = null;

public function setFilter(Filter $filter): static
{
$this->filter = $filter;
Expand All @@ -41,6 +43,13 @@ public function setOffset(int|string $offset): static
return $this;
}

public function setOrderBy(array|string $orderBy): static
{
$this->orderBy = $orderBy;

return $this;
}

public function setWithPayload($withPayload): static
{
$this->withPayload = $withPayload;
Expand Down Expand Up @@ -74,6 +83,9 @@ public function toArray(): array
if ($this->withPayload) {
$body['with_payload'] = $this->withPayload;
}
if ($this->orderBy) {
$body['order_by'] = $this->orderBy;
}

return $body;
}
Expand Down
130 changes: 130 additions & 0 deletions tests/Integration/Endpoints/Collections/SearchSortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* @since Mar 2023
* @author Haydar KULEKCI <[email protected]>
*/

namespace Integration\Endpoints\Collections;

use Qdrant\Endpoints\Collections;
use Qdrant\Exception\InvalidArgumentException;
use Qdrant\Models\Filter\Condition\MatchString;
use Qdrant\Models\Filter\Filter;
use Qdrant\Models\PointsStruct;
use Qdrant\Models\Request\CreateIndex;
use Qdrant\Models\Request\ScrollRequest;
use Qdrant\Models\Request\SearchRequest;
use Qdrant\Models\VectorStruct;
use Qdrant\Tests\Integration\AbstractIntegration;

class SearchSortTest extends AbstractIntegration
{
/**
* @throws InvalidArgumentException
*/
public function setUp(): void
{
parent::setUp();

$this->createCollections('sample-collection');

$response = $this->getCollections('sample-collection')->index()->create(
(new CreateIndex('sort', [
'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 testScrollAscPoint(): void
{
$filter = (new Filter())->addMust(
new MatchString('color', 'red')
);

$scroll = (new ScrollRequest())->setFilter($filter)->setOrderBy('sort');
$response = $this->getCollections('sample-collection')->points()->scroll($scroll);

$this->assertEquals('ok', $response['status']);
$this->assertCount(2, $response['result']);
$this->assertEquals(1, $response['result']['points'][0]['id']);
}

public function testScrollDescPoint(): void
{
$filter = (new Filter())->addMust(
new MatchString('color', 'red')
);

$scroll = (new ScrollRequest())->setFilter($filter)->setOrderBy([
'key' => 'sort',
'direction' => 'desc'
]);
$response = $this->getCollections('sample-collection')->points()->scroll($scroll);

$this->assertEquals('ok', $response['status']);
$this->assertCount(2, $response['result']);
$this->assertEquals(2, $response['result']['points'][0]['id']);
}

protected function tearDown(): void
{
parent::tearDown();
$collections = new Collections($this->client);

$collections->setCollectionName('sample-collection')->delete();
}
}

0 comments on commit 411d60b

Please sign in to comment.