Skip to content

Commit 411d60b

Browse files
authored
Merge pull request #56 from hkulekci/scroll-implementation
sort implementation for scroll endpoint
2 parents 7c5371d + 960300a commit 411d60b

File tree

4 files changed

+159
-5
lines changed

4 files changed

+159
-5
lines changed

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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@
88

99
class CreateIndex implements RequestModel
1010
{
11-
public function __construct(protected string $fieldName, protected string $fieldSchema, protected array $schemaParams = [])
11+
protected string $fieldName;
12+
protected ?array $fieldSchema;
13+
14+
public function __construct(string $fieldName, array|string $fieldSchema = null)
1215
{
16+
if (is_string($fieldSchema)) {
17+
$this->fieldSchema = [
18+
'type' => $fieldSchema
19+
];
20+
} else {
21+
$this->fieldSchema = $fieldSchema;
22+
}
23+
24+
$this->fieldName = $fieldName;
1325
}
1426

1527
public function toArray(): array
1628
{
17-
// TODO: schema params is missing
18-
return [
29+
return array_filter([
1930
'field_name' => $this->fieldName,
2031
'field_schema' => $this->fieldSchema
21-
];
32+
]);
2233
}
2334
}

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: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
'wait' => 'true'
40+
]
41+
);
42+
43+
$this->assertEquals('ok', $response['status']);
44+
$this->assertEquals('completed', $response['result']['status']);
45+
46+
$response = $this->getCollections('sample-collection')->points()
47+
->upsert(
48+
PointsStruct::createFromArray(self::basicPointDataProvider()[0][0]),
49+
[
50+
'wait' => 'true'
51+
]
52+
);
53+
54+
$this->assertEquals('ok', $response['status']);
55+
$this->assertEquals('completed', $response['result']['status']);
56+
}
57+
58+
public static function basicPointDataProvider(): array
59+
{
60+
return [
61+
[
62+
[
63+
[
64+
'id' => 1,
65+
'vector' => new VectorStruct([1, 3, 400], 'image'),
66+
'payload' => [
67+
'sort' => 1,
68+
'color' => 'red'
69+
]
70+
],
71+
[
72+
'id' => 2,
73+
'vector' => new VectorStruct([1, 3, 300], 'image'),
74+
'payload' => [
75+
'sort' => 2,
76+
'color' => 'red'
77+
]
78+
],
79+
[
80+
'id' => 3,
81+
'vector' => new VectorStruct([1, 3, 300], 'image'),
82+
'payload' => [
83+
'sort' => 3,
84+
'color' => 'green'
85+
]
86+
],
87+
]
88+
]
89+
];
90+
}
91+
92+
public function testScrollAscPoint(): void
93+
{
94+
$filter = (new Filter())->addMust(
95+
new MatchString('color', 'red')
96+
);
97+
98+
$scroll = (new ScrollRequest())->setFilter($filter)->setOrderBy('sort');
99+
$response = $this->getCollections('sample-collection')->points()->scroll($scroll);
100+
101+
$this->assertEquals('ok', $response['status']);
102+
$this->assertCount(2, $response['result']);
103+
$this->assertEquals(1, $response['result']['points'][0]['id']);
104+
}
105+
106+
public function testScrollDescPoint(): void
107+
{
108+
$filter = (new Filter())->addMust(
109+
new MatchString('color', 'red')
110+
);
111+
112+
$scroll = (new ScrollRequest())->setFilter($filter)->setOrderBy([
113+
'key' => 'sort',
114+
'direction' => 'desc'
115+
]);
116+
$response = $this->getCollections('sample-collection')->points()->scroll($scroll);
117+
118+
$this->assertEquals('ok', $response['status']);
119+
$this->assertCount(2, $response['result']);
120+
$this->assertEquals(2, $response['result']['points'][0]['id']);
121+
}
122+
123+
protected function tearDown(): void
124+
{
125+
parent::tearDown();
126+
$collections = new Collections($this->client);
127+
128+
$collections->setCollectionName('sample-collection')->delete();
129+
}
130+
}

0 commit comments

Comments
 (0)