Skip to content

Commit ccd923d

Browse files
committed
Support batched updates with NDJSON and CSV
1 parent 7b641c5 commit ccd923d

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/meilisearch/index.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ def update_documents_csv(documents, primary_key = nil, delimiter = nil)
173173
end
174174
alias add_or_update_documents_csv add_documents_csv
175175

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+
176190
def update_documents!(documents, primary_key = nil)
177191
Utils.soft_deprecate(
178192
'Index#update_documents!',

spec/meilisearch/index/documents_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,71 @@
259259
}
260260
)
261261
end
262+
263+
it '#update_documents_ndjson_in_batches' do
264+
index.add_documents_ndjson(ndjson_docs, 'objectRef').await
265+
266+
edits = <<~EDITS
267+
{ "objectRef": 123, "comment": "AN OLD BOOK" }
268+
{ "objectRef": 456, "title": "The Little Prince" }
269+
{ "objectRef": 55, "comment": "A SCI FI BOOK" }
270+
EDITS
271+
272+
tasks = index.update_documents_ndjson_in_batches(edits, 2)
273+
expect(tasks).to contain_exactly(a_kind_of(Meilisearch::Models::Task),
274+
a_kind_of(Meilisearch::Models::Task))
275+
tasks.each(&:await)
276+
expect(index.documents['results']).to include(
277+
{
278+
'objectRef' => 123,
279+
'title' => 'Pride and Prejudice',
280+
'comment' => 'AN OLD BOOK'
281+
},
282+
{
283+
'objectRef' => 456,
284+
'title' => 'The Little Prince',
285+
'comment' => 'A french book'
286+
},
287+
{
288+
'objectRef' => 55,
289+
'title' => 'The Three Body Problem',
290+
'comment' => 'A SCI FI BOOK'
291+
}
292+
)
293+
end
294+
295+
it '#update_documents_csv_in_batches' do
296+
index.add_documents_csv(csv_docs, 'objectRef').await
297+
edits = <<~CSV
298+
"objectRef:number"|"comment:string"
299+
"123"|"AN OLD BOOK"
300+
"456"|"AN EXQUISITE FRENCH BOOK"
301+
"55"|"A SCI FI BOOK"
302+
CSV
303+
304+
tasks = index.update_documents_csv_in_batches(edits, 2, 'objectRef', '|')
305+
expect(tasks).to contain_exactly(a_kind_of(Meilisearch::Models::Task),
306+
a_kind_of(Meilisearch::Models::Task))
307+
tasks.each(&:await)
308+
309+
expect(index.documents['results']).to include(
310+
{
311+
'objectRef' => 123,
312+
'title' => 'Pride and Prejudice',
313+
'comment' => 'AN OLD BOOK'
314+
},
315+
{
316+
'objectRef' => 456,
317+
'title' => 'Le Petit Prince',
318+
'comment' => 'AN EXQUISITE FRENCH BOOK'
319+
},
320+
{
321+
'objectRef' => 55,
322+
'title' => 'The Three Body Problem',
323+
'comment' => 'A SCI FI BOOK'
324+
}
325+
)
326+
end
262327
end
263328

264329
describe '#add_documents!' do

0 commit comments

Comments
 (0)