Skip to content

Commit 8a5046b

Browse files
Merge #599
599: Support batching and updating with JSON, NDJSON, and CSV r=ellnix a=ellnix # Pull Request ## Related issue Fixes #243 The issue is very old, I added what I saw was missing in it, hopefully now we can close it. Reviewing can be done commit-by-commit, might be easier to follow. Co-authored-by: ellnix <[email protected]>
2 parents 6a81390 + a89a377 commit 8a5046b

File tree

4 files changed

+292
-73
lines changed

4 files changed

+292
-73
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-01-15 12:21:42 UTC using RuboCop version 1.69.2.
3+
# on 2025-01-22 14:03:07 UTC using RuboCop version 1.69.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
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 67
9+
# Offense count: 70
1010
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
1111
# AllowedMethods: refine
1212
Metrics/BlockLength:
@@ -15,9 +15,9 @@ Metrics/BlockLength:
1515
# Offense count: 4
1616
# Configuration parameters: CountComments, CountAsOne.
1717
Metrics/ClassLength:
18-
Max: 492
18+
Max: 537
1919

20-
# Offense count: 1
20+
# Offense count: 3
2121
# Configuration parameters: Max, CountKeywordArgs.
2222
Metrics/ParameterLists:
2323
MaxOptionalParameters: 4

lib/meilisearch/http_request.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def initialize(url, api_key = nil, options = {})
2222
@headers = build_default_options_headers
2323
end
2424

25-
def http_get(relative_path = '', query_params = {})
25+
def http_get(relative_path = '', query_params = {}, options = {})
2626
send_request(
2727
proc { |path, config| self.class.get(path, config) },
2828
relative_path,
2929
config: {
3030
query_params: query_params,
31-
headers: remove_headers(@headers.dup, 'Content-Type'),
32-
options: @options
31+
headers: remove_headers(@headers.dup.merge(options[:headers] || {}), 'Content-Type'),
32+
options: @options.merge(options)
3333
}
3434
)
3535
end
@@ -47,40 +47,40 @@ def http_post(relative_path = '', body = nil, query_params = nil, options = {})
4747
)
4848
end
4949

50-
def http_put(relative_path = '', body = nil, query_params = nil)
50+
def http_put(relative_path = '', body = nil, query_params = nil, options = {})
5151
send_request(
5252
proc { |path, config| self.class.put(path, config) },
5353
relative_path,
5454
config: {
5555
query_params: query_params,
5656
body: body,
57-
headers: @headers,
58-
options: @options
57+
headers: @headers.dup.merge(options[:headers] || {}),
58+
options: @options.merge(options)
5959
}
6060
)
6161
end
6262

63-
def http_patch(relative_path = '', body = nil, query_params = nil)
63+
def http_patch(relative_path = '', body = nil, query_params = nil, options = {})
6464
send_request(
6565
proc { |path, config| self.class.patch(path, config) },
6666
relative_path,
6767
config: {
6868
query_params: query_params,
6969
body: body,
70-
headers: @headers,
71-
options: @options
70+
headers: @headers.dup.merge(options[:headers] || {}),
71+
options: @options.merge(options)
7272
}
7373
)
7474
end
7575

76-
def http_delete(relative_path = '', query_params = nil)
76+
def http_delete(relative_path = '', query_params = nil, options = {})
7777
send_request(
7878
proc { |path, config| self.class.delete(path, config) },
7979
relative_path,
8080
config: {
8181
query_params: query_params,
82-
headers: remove_headers(@headers.dup, 'Content-Type'),
83-
options: @options
82+
headers: remove_headers(@headers.dup.merge(options[:headers] || {}), 'Content-Type'),
83+
options: @options.merge(options)
8484
}
8585
)
8686
end

lib/meilisearch/index.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,48 @@ def update_documents(documents, primary_key = nil)
145145
end
146146
alias add_or_update_documents update_documents
147147

148+
def update_documents_json(documents, primary_key = nil)
149+
options = { convert_body?: false }
150+
response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
151+
152+
Models::Task.new(response, task_endpoint)
153+
end
154+
alias add_or_update_documents_json update_documents_json
155+
156+
def update_documents_ndjson(documents, primary_key = nil)
157+
options = { headers: { 'Content-Type' => 'application/x-ndjson' }, convert_body?: false }
158+
response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
159+
160+
Models::Task.new(response, task_endpoint)
161+
end
162+
alias add_or_update_documents_ndjson update_documents_ndjson
163+
164+
def update_documents_csv(documents, primary_key = nil, delimiter = nil)
165+
options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }
166+
167+
response = http_put "/indexes/#{@uid}/documents", documents, {
168+
primaryKey: primary_key,
169+
csvDelimiter: delimiter
170+
}.compact, options
171+
172+
Models::Task.new(response, task_endpoint)
173+
end
174+
alias add_or_update_documents_csv add_documents_csv
175+
176+
def update_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil)
177+
documents.lines.each_slice(batch_size).map do |batch|
178+
update_documents_ndjson(batch.join, primary_key)
179+
end
180+
end
181+
182+
def update_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil)
183+
lines = documents.lines
184+
heading = lines.first
185+
lines.drop(1).each_slice(batch_size).map do |batch|
186+
update_documents_csv(heading + batch.join, primary_key, delimiter)
187+
end
188+
end
189+
148190
def update_documents!(documents, primary_key = nil)
149191
Utils.soft_deprecate(
150192
'Index#update_documents!',
@@ -161,6 +203,20 @@ def add_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
161203
end
162204
end
163205

206+
def add_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil)
207+
documents.lines.each_slice(batch_size).map do |batch|
208+
add_documents_ndjson(batch.join, primary_key)
209+
end
210+
end
211+
212+
def add_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil)
213+
lines = documents.lines
214+
heading = lines.first
215+
lines.drop(1).each_slice(batch_size).map do |batch|
216+
add_documents_csv(heading + batch.join, primary_key, delimiter)
217+
end
218+
end
219+
164220
def add_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
165221
Utils.soft_deprecate(
166222
'Index#add_documents_in_batches!',

0 commit comments

Comments
 (0)