Skip to content

Commit 9f4fd89

Browse files
[Backport 8.8] Add docs for bulk helper improvement (#1957)
Co-authored-by: Josh Mock <joshua.mock@elastic.co>
1 parent 50b728f commit 9f4fd89

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

docs/helpers.asciidoc

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ helper uses those options in conjunction with the Bulk API call.
281281
[source,js]
282282
----
283283
const result = await client.helpers.bulk({
284-
datasource: [...]
284+
datasource: [...],
285285
onDocument (doc) {
286286
return {
287287
index: { _index: 'my-index' }
@@ -326,6 +326,33 @@ const result = await client.helpers.bulk({
326326
console.log(result)
327327
----
328328

329+
[discrete]
330+
==== Modifying a document before operation
331+
332+
~Added~ ~in~ ~`v8.8.2`~
333+
334+
If you need to modify documents in your datasource before it is sent to Elasticsearch, you can return an array in the `onDocument` function rather than an operation object. The first item in the array must be the operation object, and the second item must be the document or partial document object as you'd like it to be sent to Elasticsearch.
335+
336+
[source,js]
337+
----
338+
const { Client } = require('@elastic/elasticsearch')
339+
340+
const client = new Client({
341+
cloud: { id: '<cloud-id>' },
342+
auth: { apiKey: 'base64EncodedKey' }
343+
})
344+
const result = await client.helpers.bulk({
345+
datasource: [...],
346+
onDocument (doc) {
347+
return [
348+
{ index: { _index: 'my-index' } },
349+
{ ...doc, favorite_color: 'mauve' },
350+
]
351+
}
352+
})
353+
354+
console.log(result)
355+
----
329356

330357
[discrete]
331358
[[multi-search-helper]]
@@ -574,4 +601,4 @@ const scrollSearch = client.helpers.scrollDocuments({
574601
for await (const doc of scrollSearch) {
575602
console.log(doc)
576603
}
577-
----
604+
----

0 commit comments

Comments
 (0)