Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort implementation for scroll endpoint #56

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading