@@ -317,6 +317,31 @@ class Index<T = Record<string, any>> {
317317 return await this . httpRequest . post ( url , documents , options )
318318 }
319319
320+ /**
321+ * Add or replace multiples documents to an index in batches
322+ * @memberof Index
323+ * @method addDocumentsInBatch
324+ */
325+ async addDocumentsInBatch (
326+ documents : Array < Document < T > > ,
327+ batchSize : number = 1000 ,
328+ options ?: AddDocumentParams
329+ ) : Promise < EnqueuedUpdate [ ] > {
330+ const resultArray = [ ]
331+ let batchDocuments = [ ] ;
332+ for ( let i = 0 , n = documents . length ; i < n ; ++ i ) {
333+ if ( batchDocuments . length == batchSize ) {
334+ resultArray . push ( await this . addDocuments ( batchDocuments , options ) ) ;
335+ batchDocuments = [ ]
336+ }
337+ batchDocuments . push ( documents [ i ] ) ;
338+ }
339+ if ( batchDocuments . length != 0 ) {
340+ resultArray . push ( await this . addDocuments ( batchDocuments , options ) ) ;
341+ }
342+ return resultArray
343+ }
344+
320345 /**
321346 * Add or update multiples documents to an index
322347 * @memberof Index
@@ -330,6 +355,31 @@ class Index<T = Record<string, any>> {
330355 return await this . httpRequest . put ( url , documents , options )
331356 }
332357
358+ /**
359+ * Add or update multiples documents to an index in batches
360+ * @memberof Index
361+ * @method updateDocuments
362+ */
363+ async updateDocumentsInBatch (
364+ documents : Array < Document < T > > ,
365+ batchSize : number = 1000 ,
366+ options ?: AddDocumentParams
367+ ) : Promise < EnqueuedUpdate [ ] > {
368+ const resultArray = [ ]
369+ let batchDocuments = [ ] ;
370+ for ( let i = 0 , n = documents . length ; i < n ; ++ i ) {
371+ if ( batchDocuments . length == batchSize ) {
372+ resultArray . push ( await this . updateDocuments ( batchDocuments , options ) ) ;
373+ batchDocuments = [ ]
374+ }
375+ batchDocuments . push ( documents [ i ] ) ;
376+ }
377+ if ( batchDocuments . length != 0 ) {
378+ resultArray . push ( await this . updateDocuments ( batchDocuments , options ) ) ;
379+ }
380+ return resultArray
381+ }
382+
333383 /**
334384 * Delete one document
335385 * @memberof Index
0 commit comments