Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,17 @@ def document(document_id, fields: nil)
# :fields - Array of document attributes to show (optional).
# :filter - Filter queries by an attribute's value.
# Available ONLY with Meilisearch v1.2 and newer (optional).
# :sort - A list of attributes written as an array or as a comma-separated string (optional)
# :ids - Array of ids to be retrieved (optional)
#
# @return [Hash{String => Object}] The documents results object.
# @see https://www.meilisearch.com/docs/reference/api/documents#get-documents-with-post Meilisearch API Reference
def documents(options = {})
Utils.version_error_handler(__method__) do
if options.key?(:filter)
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter, :ids])
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter, :sort, :ids])
else
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields, :ids])
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields, :sort, :ids])
end
end
end
Expand Down
25 changes: 24 additions & 1 deletion spec/meilisearch/index/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{ objectId: 456, title: 'Le Petit Prince', comment: 'A french book' },
{ objectId: 1, title: 'Alice In Wonderland', comment: 'A weird book' },
{ objectId: 1344, title: 'The Hobbit', comment: 'An awesome book' },
{ objectId: 13, title: 'Zen in the Art of Archery' },
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince', comment: 'The best book' },
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' },
{ objectId: 2, title: 'Le Rouge et le Noir' }
Expand Down Expand Up @@ -383,7 +384,7 @@
describe '#documents' do
before do
index.add_documents(documents).await
index.update_filterable_attributes(['title', 'objectId']).await
index.update_filterable_attributes(['title', 'objectId', 'comment']).await
end

it 'browses documents' do
Expand Down Expand Up @@ -423,6 +424,28 @@
)
end

describe 'sorts documents' do
before do
index.update_sortable_attributes(['title']).await
end

it 'get' do
docs = index.documents(sort: ['title:asc'])
expect(docs['results'].first).to include('objectId' => 1, 'title' => 'Alice In Wonderland')
expect(docs['results'].last).to include('objectId' => 13, 'title' => 'Zen in the Art of Archery')

docs = index.documents(sort: ['title:desc'])
expect(docs['results'].first).to include('objectId' => 13, 'title' => 'Zen in the Art of Archery')
expect(docs['results'].last).to include('objectId' => 1, 'title' => 'Alice In Wonderland')
end

it 'post' do
docs = index.documents(filter: 'comment NOT EXISTS', sort: ['title:asc'])
expect(docs['results'].first).to include('objectId' => 2, 'title' => 'Le Rouge et le Noir')
expect(docs['results'].last).to include('objectId' => 13, 'title' => 'Zen in the Art of Archery')
end
end

it 'retrieves documents by ids' do
expected_ids = [1, 2, 4]
response = index.documents(ids: expected_ids)
Expand Down