Skip to content

Commit 1630584

Browse files
committed
Add documents API sort
1 parent 0a4833c commit 1630584

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
@@ -108,16 +108,17 @@ def document(document_id, fields: nil)
108108
# :fields - Array of document attributes to show (optional).
109109
# :filter - Filter queries by an attribute's value.
110110
# Available ONLY with Meilisearch v1.2 and newer (optional).
111+
# :sort - A list of attributes written as an array or as a comma-separated string (optional)
111112
# :ids - Array of ids to be retrieved (optional)
112113
#
113114
# @return [Hash{String => Object}] The documents results object.
114115
# @see https://www.meilisearch.com/docs/reference/api/documents#get-documents-with-post Meilisearch API Reference
115116
def documents(options = {})
116117
Utils.version_error_handler(__method__) do
117118
if options.key?(:filter)
118-
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter, :ids])
119+
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter, :sort, :ids])
119120
else
120-
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields, :ids])
121+
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields, :sort, :ids])
121122
end
122123
end
123124
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
@@ -423,6 +424,28 @@
423424
)
424425
end
425426

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
448+
426449
it 'retrieves documents by ids' do
427450
expected_ids = [1, 2, 4]
428451
response = index.documents(ids: expected_ids)

0 commit comments

Comments
 (0)