Skip to content

Commit 2cb63d3

Browse files
committed
backport #779
1 parent 55499d8 commit 2cb63d3

File tree

5 files changed

+52
-34
lines changed

5 files changed

+52
-34
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"php": "^7.4 || ^8.0",
3535
"ext-json": "*",
3636
"php-http/discovery": "^1.7",
37-
"psr/http-client": "^1.0"
37+
"psr/http-client": "^1.0",
38+
"symfony/polyfill-php81": "^1.33"
3839
},
3940
"autoload": {
4041
"psr-4": {

src/Contracts/DocumentsQuery.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class DocumentsQuery
3333
*/
3434
private ?array $ids = null;
3535

36+
/**
37+
* @var list<non-empty-string>|null
38+
*/
39+
private ?array $sort = null;
40+
3641
/**
3742
* @param non-negative-int $offset
3843
*
@@ -117,46 +122,37 @@ public function hasFilter(): bool
117122
return null !== $this->filter;
118123
}
119124

125+
/**
126+
* @param list<non-empty-string> $sort
127+
*/
128+
public function setSort(array $sort): self
129+
{
130+
$this->sort = $sort;
131+
132+
return $this;
133+
}
134+
120135
/**
121136
* @return array{
122137
* offset?: non-negative-int,
123138
* limit?: non-negative-int,
124139
* fields?: non-empty-list<string>|non-empty-string,
125140
* filter?: list<non-empty-string|list<non-empty-string>>,
126141
* retrieveVectors?: bool,
127-
* ids?: string
142+
* ids?: string,
143+
* sort?: non-empty-list<string>,
128144
* }
129145
*/
130146
public function toArray(): array
131147
{
132148
return array_filter([
133149
'offset' => $this->offset,
134150
'limit' => $this->limit,
135-
'fields' => $this->getFields(),
151+
'fields' => $this->fields,
136152
'filter' => $this->filter,
137153
'retrieveVectors' => $this->retrieveVectors,
138154
'ids' => ($this->ids ?? []) !== [] ? implode(',', $this->ids) : null,
155+
'sort' => $this->sort,
139156
], static function ($item) { return null !== $item; });
140157
}
141-
142-
/**
143-
* Prepares fields for request
144-
* Fix for 1.2 document/fetch.
145-
*
146-
* @see https://github.com/meilisearch/meilisearch-php/issues/522
147-
*
148-
* @return array|string|null
149-
*/
150-
private function getFields()
151-
{
152-
if (null === $this->fields) {
153-
return null;
154-
}
155-
156-
if (null !== $this->filter) {
157-
return $this->fields;
158-
}
159-
160-
return implode(',', $this->fields);
161-
}
162158
}

src/Http/Client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ private function buildQueryString(array $queryParams = []): string
221221
if (\is_bool($value)) {
222222
$queryParams[$key] = $value ? 'true' : 'false';
223223
}
224+
if (\is_array($value) && array_is_list($value)) {
225+
$queryParams[$key] = implode(',', $value);
226+
}
224227
}
225228

226229
return \count($queryParams) > 0 ? '?'.http_build_query($queryParams) : '';

tests/Contracts/DocumentsQueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testSetFields(): void
2020
{
2121
$data = (new DocumentsQuery())->setLimit(10)->setFields(['abc', 'xyz']);
2222

23-
self::assertSame(['limit' => 10, 'fields' => 'abc,xyz'], $data->toArray());
23+
self::assertSame(['limit' => 10, 'fields' => ['abc', 'xyz']], $data->toArray());
2424
}
2525

2626
public function testSetLimit(): void

tests/Endpoints/DocumentsTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -678,18 +678,36 @@ public function testGetDocumentsWithFilterCorrectFieldFormat(): void
678678
self::assertSame($fields, $queryFields);
679679
}
680680

681-
public function testGetDocumentsWithoutFilterCorrectFieldsFormat(): void
681+
public function testGetDocumentsWithSort(): void
682682
{
683-
$fields = ['anti', 'flag'];
683+
$index = $this->createEmptyIndex($this->safeIndexName('movies'));
684+
$index->updateSortableAttributes(['id', 'genre']);
685+
$index->updateFilterableAttributes(['id', 'genre']);
686+
$promise = $index->addDocuments(self::DOCUMENTS);
687+
$index->waitForTask($promise['taskUid']);
684688

685-
$queryFields = (new DocumentsQuery())
686-
->setFields($fields)
687-
->toArray()['fields'];
689+
$response = $index->getDocuments((new DocumentsQuery())->setSort(['genre:desc', 'id:asc']));
690+
self::assertSame(2, $response[0]['id']);
691+
692+
$response = $index->getDocuments((new DocumentsQuery())->setSort(['genre:desc', 'id:desc']));
693+
self::assertSame(1344, $response[0]['id']);
694+
}
688695

689-
self::assertSame(
690-
implode(',', $fields),
691-
$queryFields
692-
);
696+
public function testGetDocumentsWithFiltersFieldsAndSort(): void
697+
{
698+
$index = $this->createEmptyIndex($this->safeIndexName('movies'));
699+
$index->updateSortableAttributes(['id', 'genre']);
700+
$index->updateFilterableAttributes(['id', 'genre']);
701+
$promise = $index->addDocuments(self::DOCUMENTS);
702+
$index->waitForTask($promise['taskUid']);
703+
704+
$query = (new DocumentsQuery())
705+
->setSort(['genre:desc', 'id:asc'])
706+
->setFields(['id', 'title'])
707+
->setFilter(['id > 2']);
708+
$response = $index->getDocuments($query);
709+
self::assertSame(123, $response[0]['id']);
710+
self::assertSame(['id', 'title'], array_keys($response[0]));
693711
}
694712

695713
public function testGetDocumentsWithVector(): void

0 commit comments

Comments
 (0)