diff --git a/lib/Helpers.d.ts b/lib/Helpers.d.ts index 88b315edb..c6d64d48e 100644 --- a/lib/Helpers.d.ts +++ b/lib/Helpers.d.ts @@ -102,6 +102,18 @@ interface UpdateActionOperation { }; } +interface UpdateActionDocOperation { + doc: { + [key: string]: any; + }; +} + +interface UpdateActionScriptOperation { + script: { + [key: string]: any; + }; +} + interface DeleteAction { delete: { _index: string; @@ -111,7 +123,10 @@ interface DeleteAction { type CreateAction = CreateActionOperation | [CreateActionOperation, unknown]; type IndexAction = IndexActionOperation | [IndexActionOperation, unknown]; -type UpdateAction = [UpdateActionOperation, Record]; +type UpdateAction = [ + UpdateActionOperation, + UpdateActionDocOperation | UpdateActionScriptOperation +]; type Action = IndexAction | CreateAction | UpdateAction | DeleteAction; type Omit = Pick>; diff --git a/lib/Helpers.js b/lib/Helpers.js index a648aea9a..fa4ab3ec9 100644 --- a/lib/Helpers.js +++ b/lib/Helpers.js @@ -539,10 +539,7 @@ class Helpers { bulkBody.push(actionBody, payloadBody); } else if (operation === 'update') { actionBody = serializer.serialize(action); - payloadBody = - typeof chunk === 'string' - ? `{"doc":${chunk}}` - : serializer.serialize({ doc: chunk, ...payload }); + payloadBody = serializer.serialize(payload || chunk); chunkBytes += Buffer.byteLength(actionBody) + Buffer.byteLength(payloadBody); bulkBody.push(actionBody, payloadBody); } else if (operation === 'delete') { diff --git a/test/types/helpers.test-d.ts b/test/types/helpers.test-d.ts index f07630569..fa8fca1d0 100644 --- a/test/types/helpers.test-d.ts +++ b/test/types/helpers.test-d.ts @@ -36,6 +36,7 @@ import { ScrollSearchResponse, OnDropDocument, MsearchHelper, + UpdateActionDocOperation, } from '../../lib/Helpers'; import { ApiResponse, ApiError, Context } from '../../lib/Transport'; @@ -107,7 +108,7 @@ expectError( const options: BulkHelperOptions> = { datasource: [], onDocument(doc: Record) { - return [{ update: { _index: 'test' } }, doc]; + return [{ update: { _index: 'test' } }, doc as UpdateActionDocOperation]; }, }; expectAssignable>>(options); diff --git a/test/unit/helpers/bulk.test.js b/test/unit/helpers/bulk.test.js index f0d375e44..89f173009 100644 --- a/test/unit/helpers/bulk.test.js +++ b/test/unit/helpers/bulk.test.js @@ -971,14 +971,16 @@ test('bulk update', (t) => { flushBytes: 1, concurrency: 1, onDocument() { + const currentId = id++; return [ { update: { _index: 'test', - _id: id++, + _id: currentId, }, }, { + doc: dataset[currentId], doc_as_upsert: true, }, ]; @@ -1022,13 +1024,15 @@ test('bulk update', (t) => { flushBytes: 1, concurrency: 1, onDocument() { + const currentId = id++; return [ { update: { _index: 'test', - _id: id++, + _id: currentId, }, }, + { doc: dataset[currentId] }, ]; }, onDrop() { @@ -1070,14 +1074,16 @@ test('bulk update', (t) => { flushBytes: 1, concurrency: 1, onDocument() { + const currentId = id++; return [ { update: { _index: 'test', - _id: id++, + _id: currentId, }, }, { + doc: dataset[currentId], doc_as_upsert: true, }, ];