@@ -329,6 +329,39 @@ def add_documents(self, documents, primary_key=None):
329329 url = f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } ?{ primary_key } '
330330 return self .http .post (url , documents )
331331
332+ def add_documents_in_batches (self , documents , batch_size = 1000 , primary_key = None ):
333+ """Add documents to the index in batches.
334+
335+ Parameters
336+ ----------
337+ documents: list
338+ List of documents. Each document should be a dictionary.
339+ batch_size (optional): int
340+ The number of documents that should be included in each batch. Default = 1000
341+ primary_key (optional): string
342+ The primary-key used in index. Ignored if already set up.
343+
344+ Returns
345+ -------
346+ update: list[dict]
347+ List of dictionaries containing an update ids to track the action:
348+ https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
349+
350+ Raises
351+ ------
352+ MeiliSearchApiError
353+ An error containing details about why MeiliSearch can't process your request.
354+ MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
355+ """
356+
357+ update_ids = []
358+
359+ for document_batch in self ._batch (documents , batch_size ):
360+ update_id = self .add_documents (document_batch , primary_key )
361+ update_ids .append (update_id )
362+
363+ return update_ids
364+
332365 def update_documents (self , documents , primary_key = None ):
333366 """Update documents in the index.
334367
@@ -357,6 +390,38 @@ def update_documents(self, documents, primary_key=None):
357390 url = f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .document } ?{ primary_key } '
358391 return self .http .put (url , documents )
359392
393+ def update_documents_in_batches (self , documents , batch_size = 1000 , primary_key = None ):
394+ """Update documents to the index in batches.
395+
396+ Parameters
397+ ----------
398+ documents: list
399+ List of documents. Each document should be a dictionary.
400+ batch_size (optional): int
401+ The number of documents that should be included in each batch. Default = 1000
402+ primary_key (optional): string
403+ The primary-key used in index. Ignored if already set up.
404+
405+ Returns
406+ -------
407+ update: list[dict]
408+ List of dictionaries containing an update ids to track the action:
409+ https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status
410+
411+ Raises
412+ ------
413+ MeiliSearchApiError
414+ An error containing details about why MeiliSearch can't process your request.
415+ MeiliSearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
416+ """
417+
418+ update_ids = []
419+
420+ for document_batch in self ._batch (documents , batch_size ):
421+ update_id = self .update_documents (document_batch , primary_key )
422+ update_ids .append (update_id )
423+
424+ return update_ids
360425
361426 def delete_document (self , document_id ):
362427 """Delete one document from the index.
@@ -935,5 +1000,11 @@ def reset_attributes_for_faceting(self):
9351000 self .__settings_url_for (self .config .paths .attributes_for_faceting ),
9361001 )
9371002
1003+ @staticmethod
1004+ def _batch (documents , batch_size ):
1005+ total_len = len (documents )
1006+ for i in range (0 , total_len , batch_size ):
1007+ yield documents [i : i + batch_size ]
1008+
9381009 def __settings_url_for (self , sub_route ):
9391010 return f'{ self .config .paths .index } /{ self .uid } /{ self .config .paths .setting } /{ sub_route } '
0 commit comments