Skip to content

Commit 8c9287a

Browse files
bors[bot]vishnugt
andauthored
Merge #1039
1039: Add methods to automatically add/update documents in batches r=bidoubiwa a=vishnugt Fixes #1036 Co-authored-by: vishnugt <[email protected]> Co-authored-by: Vishnu Gt <[email protected]>
2 parents bd86cbd + 199106d commit 8c9287a

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,18 @@ Or using the index object:
397397

398398
`index.addDocuments(documents: Document<T>[]): Promise<EnqueuedUpdate>`
399399

400+
- [Add or replace multiple documents in batches](https://docs.meilisearch.com/reference/api/documents.html#add-or-replace-documents):
401+
402+
`index.addDocumentsInBatch(documents: Document<T>[], batchSize = 1000): Promise<EnqueuedUpdate[]>`
403+
400404
- [Add or update multiple documents](https://docs.meilisearch.com/reference/api/documents.html#add-or-update-documents):
401405

402406
`index.updateDocuments(documents: Document<T>[]): Promise<EnqueuedUpdate>`
403407

408+
- [Add or update multiple documents in batches](https://docs.meilisearch.com/reference/api/documents.html#add-or-update-documents):
409+
410+
`index.updateDocumentsInBatch(documents: Document<T>[], batchSize = 1000): Promise<EnqueuedUpdate[]>`
411+
404412
- [Get Documents](https://docs.meilisearch.com/reference/api/documents.html#get-documents):
405413

406414
`index.getDocuments(params: getDocumentsParams): Promise<Document<T>[]>`

src/lib/indexes.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,25 @@ class Index<T = Record<string, any>> {
316316
return await this.httpRequest.post(url, documents, options)
317317
}
318318

319+
/**
320+
* Add or replace multiples documents to an index in batches
321+
* @memberof Index
322+
* @method addDocumentsInBatch
323+
*/
324+
async addDocumentsInBatch(
325+
documents: Array<Document<T>>,
326+
batchSize = 1000,
327+
options?: AddDocumentParams
328+
): Promise<EnqueuedUpdate[]> {
329+
const updates = []
330+
for (let i = 0; i < documents.length; i += batchSize) {
331+
updates.push(
332+
await this.addDocuments(documents.slice(i, i + batchSize), options)
333+
)
334+
}
335+
return updates
336+
}
337+
319338
/**
320339
* Add or update multiples documents to an index
321340
* @memberof Index
@@ -329,6 +348,25 @@ class Index<T = Record<string, any>> {
329348
return await this.httpRequest.put(url, documents, options)
330349
}
331350

351+
/**
352+
* Add or update multiples documents to an index in batches
353+
* @memberof Index
354+
* @method updateDocuments
355+
*/
356+
async updateDocumentsInBatch(
357+
documents: Array<Document<T>>,
358+
batchSize = 1000,
359+
options?: AddDocumentParams
360+
): Promise<EnqueuedUpdate[]> {
361+
const updates = []
362+
for (let i = 0; i < documents.length; i += batchSize) {
363+
updates.push(
364+
await this.updateDocuments(documents.slice(i, i + batchSize), options)
365+
)
366+
}
367+
return updates
368+
}
369+
332370
/**
333371
* Delete one document
334372
* @memberof Index

tests/documents_tests.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ describe.each([
7171
await client.index(indexPk.uid).waitForPendingUpdate(updateId)
7272
})
7373

74+
test(`${permission} key: Add documents to uid with primary key in batch`, async () => {
75+
const response: EnqueuedUpdate[] = await client
76+
.index(indexPk.uid)
77+
.addDocumentsInBatch(dataset, 4)
78+
expect(response).toBeInstanceOf(Array)
79+
expect(response).toHaveLength(2)
80+
expect(response[0]).toHaveProperty('updateId', expect.any(Number))
81+
for (const enqueuedUpdate of response) {
82+
const addResponse = await client
83+
.index(indexPk.uid)
84+
.waitForPendingUpdate(enqueuedUpdate.updateId)
85+
expect(addResponse.status).toBe('processed')
86+
expect(addResponse.type.name).toBe('DocumentsAddition')
87+
}
88+
})
89+
7490
test(`${permission} key: Get documents with string attributesToRetrieve`, async () => {
7591
await client
7692
.index(indexNoPk.uid)
@@ -208,6 +224,22 @@ describe.each([
208224
})
209225
})
210226

227+
test(`${permission} key: Update document from index that has a primary key in batch`, async () => {
228+
const response: EnqueuedUpdate[] = await client
229+
.index(indexPk.uid)
230+
.updateDocumentsInBatch(dataset, 2)
231+
expect(response).toBeInstanceOf(Array)
232+
expect(response).toHaveLength(4)
233+
expect(response[0]).toHaveProperty('updateId', expect.any(Number))
234+
for (const enqueuedUpdate of response) {
235+
const addResponse = await client
236+
.index(indexPk.uid)
237+
.waitForPendingUpdate(enqueuedUpdate.updateId)
238+
expect(addResponse.status).toBe('processed')
239+
expect(addResponse.type.name).toBe('DocumentsPartial')
240+
}
241+
})
242+
211243
test(`${permission} key: Add document with update documents function from index that has NO primary key`, async () => {
212244
const { updateId: addDocUpdate } = await client
213245
.index(indexNoPk.uid)

0 commit comments

Comments
 (0)