Skip to content

Commit d11ce6a

Browse files
authored
Merge pull request #439 from meilisearch/get-documents-by-filter
Get documents by filter
2 parents 3eb2d12 + e9f8c48 commit d11ce6a

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,24 @@ def document(document_id, fields: nil)
6363
alias get_document document
6464
alias get_one_document document
6565

66+
# Public: Retrieve documents from a index.
67+
#
68+
# options - The hash options used to refine the selection (default: {}):
69+
# :limit - Number of documents to return (optional).
70+
# :offset - Number of documents to skip (optional).
71+
# :fields - Array of document attributes to show (optional).
72+
# :filter - Filter queries by an attribute's value.
73+
# Available ONLY with Meilisearch v1.2 and newer (optional).
74+
#
75+
# Returns the documents results object.
6676
def documents(options = {})
67-
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
77+
Utils.version_error_handler(__method__) do
78+
if options.key?(:filter)
79+
http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter])
80+
else
81+
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
82+
end
83+
end
6884
end
6985
alias get_documents documents
7086

lib/meilisearch/utils.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ def self.parse(body)
2323
end
2424
end
2525

26+
def self.filter(original_options, allowed_params = [])
27+
original_options.transform_keys(&:to_sym).slice(*allowed_params)
28+
end
29+
2630
def self.parse_query(original_options, allowed_params = [])
27-
only_allowed_params = original_options.transform_keys(&:to_sym).slice(*allowed_params)
31+
only_allowed_params = filter(original_options, allowed_params)
2832

2933
Utils.transform_attributes(only_allowed_params).then do |body|
3034
body.transform_values do |v|

spec/meilisearch/index/documents_spec.rb

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,28 @@
3737
end
3838

3939
it 'adds JSON documents (as a array of documents)' do
40-
documents = <<JSON
41-
[
42-
{ "objectRef": 123, "title": "Pride and Prejudice", "comment": "A great book" },
43-
{ "objectRef": 456, "title": "Le Petit Prince", "comment": "A french book" },
44-
{ "objectRef": 1, "title": "Alice In Wonderland", "comment": "A weird book" },
45-
{ "objectRef": 1344, "title": "The Hobbit", "comment": "An awesome book" },
46-
{ "objectRef": 4, "title": "Harry Potter and the Half-Blood Prince", "comment": "The best book" }
47-
]
48-
JSON
40+
documents = <<~JSON
41+
[
42+
{ "objectRef": 123, "title": "Pride and Prejudice", "comment": "A great book" },
43+
{ "objectRef": 456, "title": "Le Petit Prince", "comment": "A french book" },
44+
{ "objectRef": 1, "title": "Alice In Wonderland", "comment": "A weird book" },
45+
{ "objectRef": 1344, "title": "The Hobbit", "comment": "An awesome book" },
46+
{ "objectRef": 4, "title": "Harry Potter and the Half-Blood Prince", "comment": "The best book" }
47+
]
48+
JSON
4949
response = index.add_documents_json(documents, 'objectRef')
5050

5151
index.wait_for_task(response['taskUid'])
5252
expect(index.documents['results'].count).to eq(5)
5353
end
5454

5555
it 'adds NDJSON documents (as a array of documents)' do
56-
documents = <<NDJSON
57-
{ "objectRef": 123, "title": "Pride and Prejudice", "comment": "A great book" }
58-
{ "objectRef": 456, "title": "Le Petit Prince", "comment": "A french book" }
59-
{ "objectRef": 1, "title": "Alice In Wonderland", "comment": "A weird book" }
60-
{ "objectRef": 4, "title": "Harry Potter and the Half-Blood Prince", "comment": "The best book" }
61-
NDJSON
56+
documents = <<~NDJSON
57+
{ "objectRef": 123, "title": "Pride and Prejudice", "comment": "A great book" }
58+
{ "objectRef": 456, "title": "Le Petit Prince", "comment": "A french book" }
59+
{ "objectRef": 1, "title": "Alice In Wonderland", "comment": "A weird book" }
60+
{ "objectRef": 4, "title": "Harry Potter and the Half-Blood Prince", "comment": "The best book" }
61+
NDJSON
6262
response = index.add_documents_ndjson(documents, 'objectRef')
6363

6464
index.wait_for_task(response['taskUid'])
@@ -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)