Skip to content

Commit a74c0bd

Browse files
committed
Add documents API sort
1 parent 78f93e6 commit a74c0bd

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/meilisearch/index.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ def document(document_id, fields: nil)
7373
# :fields - Array of document attributes to show (optional).
7474
# :filter - Filter queries by an attribute's value.
7575
# Available ONLY with Meilisearch v1.2 and newer (optional).
76+
# :sort - A list of attributes written as an array or as a comma-separated string (optional)
7677
#
7778
# Returns the documents results object.
7879
def documents(options = {})
7980
Utils.version_error_handler(__method__) do
8081
if options.key?(:filter)
81-
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter])
82+
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter, :sort])
8283
else
83-
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
84+
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields, :sort])
8485
end
8586
end
8687
end

spec/meilisearch/index/documents_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{ objectId: 456, title: 'Le Petit Prince', comment: 'A french book' },
1111
{ objectId: 1, title: 'Alice In Wonderland', comment: 'A weird book' },
1212
{ objectId: 1344, title: 'The Hobbit', comment: 'An awesome book' },
13+
{ objectId: 13, title: 'Zen in the Art of Archery' },
1314
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', comment: 'The best book' },
1415
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' },
1516
{ objectId: 2, title: 'Le Rouge et le Noir' }
@@ -383,7 +384,7 @@
383384
describe '#documents' do
384385
before do
385386
index.add_documents(documents).await
386-
index.update_filterable_attributes(['title', 'objectId']).await
387+
index.update_filterable_attributes(['title', 'objectId', 'comment']).await
387388
end
388389

389390
it 'browses documents' do
@@ -422,6 +423,28 @@
422423
{ 'title' => a_kind_of(String) }
423424
)
424425
end
426+
427+
describe 'sorts documents' do
428+
before do
429+
index.update_sortable_attributes(['title']).await
430+
end
431+
432+
it 'get' do
433+
docs = index.documents(sort: ['title:asc'])
434+
expect(docs['results'].first).to include('objectId' => 1, 'title' => 'Alice In Wonderland')
435+
expect(docs['results'].last).to include('objectId' => 13, 'title' => 'Zen in the Art of Archery')
436+
437+
docs = index.documents(sort: ['title:desc'])
438+
expect(docs['results'].first).to include('objectId' => 13, 'title' => 'Zen in the Art of Archery')
439+
expect(docs['results'].last).to include('objectId' => 1, 'title' => 'Alice In Wonderland')
440+
end
441+
442+
it 'post' do
443+
docs = index.documents(filter: 'comment NOT EXISTS', sort: ['title:asc'])
444+
expect(docs['results'].first).to include('objectId' => 2, 'title' => 'Le Rouge et le Noir')
445+
expect(docs['results'].last).to include('objectId' => 13, 'title' => 'Zen in the Art of Archery')
446+
end
447+
end
425448
end
426449

427450
describe '#update_documents' do

0 commit comments

Comments
 (0)