Skip to content

Commit 6b6463a

Browse files
author
Benoit POLASZEK
committed
feat: sort documents
1 parent 5e89737 commit 6b6463a

File tree

4 files changed

+37
-37
lines changed

4 files changed

+37
-37
lines changed

src/Contracts/DocumentsQuery.php

Lines changed: 16 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,34 @@ public function hasFilter(): bool
117122
return null !== $this->filter;
118123
}
119124

125+
public function setSort(array $sort): self
126+
{
127+
$this->sort = $sort;
128+
129+
return $this;
130+
}
131+
120132
/**
121133
* @return array{
122134
* offset?: non-negative-int,
123135
* limit?: non-negative-int,
124136
* fields?: non-empty-list<string>|non-empty-string,
125137
* filter?: list<non-empty-string|list<non-empty-string>>,
126138
* retrieveVectors?: 'true'|'false',
127-
* ids?: string
139+
* ids?: string,
140+
* sort?: non-empty-list<string>,
128141
* }
129142
*/
130143
public function toArray(): array
131144
{
132145
return array_filter([
133146
'offset' => $this->offset,
134147
'limit' => $this->limit,
135-
'fields' => $this->getFields(),
148+
'fields' => $this->fields,
136149
'filter' => $this->filter,
137150
'retrieveVectors' => (null !== $this->retrieveVectors ? ($this->retrieveVectors ? 'true' : 'false') : null),
138151
'ids' => ($this->ids ?? []) !== [] ? implode(',', $this->ids) : null,
152+
'sort' => $this->sort,
139153
], static function ($item) { return null !== $item; });
140154
}
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-
}
162155
}

src/Endpoints/Delegates/HandlesDocuments.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public function getDocuments(?DocumentsQuery $options = null): DocumentsResults
2929
if ($options->hasFilter()) {
3030
$response = $this->http->post(self::PATH.'/'.$this->uid.'/documents/fetch', $query);
3131
} else {
32+
if (isset($query['sort'])) {
33+
$query['sort'] = implode(',', $query['sort']);
34+
}
35+
if (isset($query['fields'])) {
36+
$query['fields'] = implode(',', $query['fields']);
37+
}
3238
$response = $this->http->get(self::PATH.'/'.$this->uid.'/documents', $query);
3339
}
3440

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: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -666,30 +666,31 @@ public function testGetDocumentsWithFilter(): void
666666
self::assertCount(3, $response);
667667
}
668668

669-
public function testGetDocumentsWithFilterCorrectFieldFormat(): void
669+
public function testGetDocumentsWithSort(): void
670670
{
671-
$fields = ['the', 'clash'];
671+
$index = $this->createEmptyIndex($this->safeIndexName('movies'));
672+
$index->updateSortableAttributes(['id', 'genre']);
673+
$index->updateFilterableAttributes(['id', 'genre']);
674+
$promise = $index->addDocuments(self::DOCUMENTS);
675+
$index->waitForTask($promise['taskUid']);
672676

673-
$queryFields = (new DocumentsQuery())
674-
->setFields($fields)
675-
->setFilter(['id > 100'])
676-
->toArray()['fields'];
677+
$response = $index->getDocuments((new DocumentsQuery())->setSort(['genre:desc', 'id:asc']));
678+
self::assertSame(2, $response[0]['id']);
677679

678-
self::assertSame($fields, $queryFields);
680+
$response = $index->getDocuments((new DocumentsQuery())->setSort(['genre:desc', 'id:desc']));
681+
self::assertSame(1344, $response[0]['id']);
679682
}
680683

681-
public function testGetDocumentsWithoutFilterCorrectFieldsFormat(): void
684+
public function testGetDocumentsWithFilterCorrectFieldFormat(): void
682685
{
683-
$fields = ['anti', 'flag'];
686+
$fields = ['the', 'clash'];
684687

685688
$queryFields = (new DocumentsQuery())
686689
->setFields($fields)
690+
->setFilter(['id > 100'])
687691
->toArray()['fields'];
688692

689-
self::assertSame(
690-
implode(',', $fields),
691-
$queryFields
692-
);
693+
self::assertSame($fields, $queryFields);
693694
}
694695

695696
public function testGetDocumentsWithVector(): void

0 commit comments

Comments
 (0)