Skip to content

Commit a9cd8d9

Browse files
committed
Add ability to query documents using a filter
1 parent 9dc2db5 commit a9cd8d9

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-05-20 01:53:00 UTC using RuboCop version 1.50.2.
3+
# on 2023-05-20 02:24:12 UTC using RuboCop version 1.50.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -14,7 +14,7 @@ Gemspec/RequireMFA:
1414
Exclude:
1515
- 'meilisearch.gemspec'
1616

17-
# Offense count: 46
17+
# Offense count: 47
1818
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
1919
# AllowedMethods: refine
2020
Metrics/BlockLength:
@@ -23,7 +23,7 @@ Metrics/BlockLength:
2323
# Offense count: 2
2424
# Configuration parameters: CountComments, CountAsOne.
2525
Metrics/ClassLength:
26-
Max: 321
26+
Max: 327
2727

2828
# Offense count: 1
2929
# Configuration parameters: Max, CountKeywordArgs.

lib/meilisearch/index.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ def document(document_id, fields: nil)
6464
alias get_one_document document
6565

6666
def documents(options = {})
67-
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
67+
Utils.version_error_handler(__method__) do
68+
if options.key?(:filter)
69+
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter])
70+
else
71+
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
72+
end
73+
end
6874
end
6975
alias get_documents documents
7076

spec/meilisearch/index/documents_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@
196196
end
197197

198198
describe 'accessing documents' do
199-
before { index.add_documents!(documents) }
199+
before do
200+
index.add_documents(documents)
201+
202+
task = index.update_filterable_attributes(['title', 'objectId'])
203+
client.wait_for_task(task['taskUid'])
204+
end
200205

201206
it 'gets one document from its primary-key' do
202207
task = index.document(123)
@@ -228,6 +233,25 @@
228233
expect(docs).to be_a(Array)
229234
expect(docs.first.keys).to eq(['title'])
230235
end
236+
237+
it 'retrieves documents by filters' do
238+
docs = index.documents(filter: 'objectId > 400')['results']
239+
240+
expect(docs).to be_a(Array)
241+
expect(docs.first).to eq({
242+
'objectId' => 456,
243+
'title' => 'Le Petit Prince',
244+
'comment' => 'A french book'
245+
})
246+
end
247+
248+
it 'retrieves documents by filters & other parameters' do
249+
docs = index.documents(fields: ['title'], filter: 'objectId > 100')['results']
250+
251+
expect(docs).to be_a(Array)
252+
expect(docs.size).to eq(3)
253+
expect(docs.first.keys).to eq(['title'])
254+
end
231255
end
232256

233257
describe 'updating documents' do

0 commit comments

Comments
 (0)