Skip to content

Commit 64cecf3

Browse files
committed
sort implementation for scroll endpoint
1 parent 7c5371d commit 64cecf3

4 files changed

Lines changed: 122 additions & 5 deletions

File tree

src/Endpoints/HttpFactoryTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected function createRequest(string $method, string $uri, array $body = []):
5252

5353
protected function queryBuild(array $params): string
5454
{
55-
return '?' . http_build_query($params);
55+
$paramStr = http_build_query($params);
56+
return $paramStr ? '?' . $paramStr : '';
5657
}
5758
}

src/Models/Request/CreateIndex.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88

99
class CreateIndex implements RequestModel
1010
{
11-
public function __construct(protected string $fieldName, protected string $fieldSchema, protected array $schemaParams = [])
11+
public function __construct(protected string $fieldName, protected ?array $fieldSchema = null)
1212
{
1313
}
1414

1515
public function toArray(): array
1616
{
17-
// TODO: schema params is missing
18-
return [
17+
return array_filter([
1918
'field_name' => $this->fieldName,
2019
'field_schema' => $this->fieldSchema
21-
];
20+
]);
2221
}
2322
}

src/Models/Request/ScrollRequest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ScrollRequest implements RequestModel
2020

2121
protected bool|array|null $withPayload = null;
2222

23+
protected string|array|null $orderBy = null;
24+
2325
public function setFilter(Filter $filter): static
2426
{
2527
$this->filter = $filter;
@@ -41,6 +43,13 @@ public function setOffset(int|string $offset): static
4143
return $this;
4244
}
4345

46+
public function setOrderBy(array|string $orderBy): static
47+
{
48+
$this->orderBy = $orderBy;
49+
50+
return $this;
51+
}
52+
4453
public function setWithPayload($withPayload): static
4554
{
4655
$this->withPayload = $withPayload;
@@ -74,6 +83,9 @@ public function toArray(): array
7483
if ($this->withPayload) {
7584
$body['with_payload'] = $this->withPayload;
7685
}
86+
if ($this->orderBy) {
87+
$body['order_by'] = $this->orderBy;
88+
}
7789

7890
return $body;
7991
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* @since Mar 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Integration\Endpoints\Collections;
8+
9+
use Qdrant\Endpoints\Collections;
10+
use Qdrant\Exception\InvalidArgumentException;
11+
use Qdrant\Models\Filter\Condition\MatchString;
12+
use Qdrant\Models\Filter\Filter;
13+
use Qdrant\Models\PointsStruct;
14+
use Qdrant\Models\Request\CreateIndex;
15+
use Qdrant\Models\Request\ScrollRequest;
16+
use Qdrant\Models\Request\SearchRequest;
17+
use Qdrant\Models\VectorStruct;
18+
use Qdrant\Tests\Integration\AbstractIntegration;
19+
20+
class SearchSortTest extends AbstractIntegration
21+
{
22+
/**
23+
* @throws InvalidArgumentException
24+
*/
25+
public function setUp(): void
26+
{
27+
parent::setUp();
28+
29+
$this->createCollections('sample-collection');
30+
31+
$response = $this->getCollections('sample-collection')->index()->create(
32+
(new CreateIndex('sort', [
33+
'type' => 'integer',
34+
'range' => true,
35+
'lookup' => false,
36+
'is_principal' => true
37+
]))
38+
);
39+
40+
$this->assertEquals('ok', $response['status']);
41+
$this->assertEquals('acknowledged', $response['result']['status']);
42+
43+
$response = $this->getCollections('sample-collection')->points()
44+
->upsert(PointsStruct::createFromArray(self::basicPointDataProvider()[0][0]));
45+
46+
$this->assertEquals('ok', $response['status']);
47+
$this->assertEquals('acknowledged', $response['result']['status']);
48+
}
49+
50+
public static function basicPointDataProvider(): array
51+
{
52+
return [
53+
[
54+
[
55+
[
56+
'id' => 1,
57+
'vector' => new VectorStruct([1, 3, 400], 'image'),
58+
'payload' => [
59+
'sort' => 1,
60+
'color' => 'red'
61+
]
62+
],
63+
[
64+
'id' => 2,
65+
'vector' => new VectorStruct([1, 3, 300], 'image'),
66+
'payload' => [
67+
'sort' => 2,
68+
'image' => 'red'
69+
]
70+
],
71+
[
72+
'id' => 3,
73+
'vector' => new VectorStruct([1, 3, 300], 'image'),
74+
'payload' => [
75+
'sort' => 3,
76+
'image' => 'green'
77+
]
78+
],
79+
]
80+
]
81+
];
82+
}
83+
84+
public function testSearchPoint(): void
85+
{
86+
$filter = (new Filter())->addMust(
87+
new MatchString('color', 'red')
88+
);
89+
90+
$scroll = (new ScrollRequest())->setFilter($filter)->setOrderBy('sort');
91+
$response = $this->getCollections('sample-collection')->points()->scroll($scroll);
92+
93+
$this->assertEquals('ok', $response['status']);
94+
$this->assertCount(2, $response['result']);
95+
}
96+
97+
98+
protected function tearDown(): void
99+
{
100+
parent::tearDown();
101+
$collections = new Collections($this->client);
102+
103+
$collections->setCollectionName('sample-collection')->delete();
104+
}
105+
}

0 commit comments

Comments
 (0)