From 8b4d8a3e486826a2889b4d303f34ee3b6df661ab Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Thu, 20 Jul 2023 15:16:52 -0500 Subject: [PATCH 1/2] Update docs for bulk helper Adds a section detailing how to modify documents before sending to the bulk API, using the improvement introduced in https://github.com/elastic/elasticsearch-js/pull/1732 --- docs/helpers.asciidoc | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/helpers.asciidoc b/docs/helpers.asciidoc index b78f79399..0c7de0983 100644 --- a/docs/helpers.asciidoc +++ b/docs/helpers.asciidoc @@ -326,6 +326,33 @@ const result = await client.helpers.bulk({ console.log(result) ---- +[discrete] +==== Modifying a document before operation + +~Added~ ~in~ ~`v8.8.2`~ + +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. + +[source,js] +---- +const { Client } = require('@elastic/elasticsearch') + +const client = new Client({ + cloud: { id: '' }, + auth: { apiKey: 'base64EncodedKey' } +}) +const result = await client.helpers.bulk({ + datasource: [...] + onDocument (doc) { + return [ + { index: { _index: 'my-index' } }, + { ...doc, favorite_color: 'mauve' }, + ] + } +}) + +console.log(result) +---- [discrete] [[multi-search-helper]] @@ -574,4 +601,4 @@ const scrollSearch = client.helpers.scrollDocuments({ for await (const doc of scrollSearch) { console.log(doc) } ----- \ No newline at end of file +---- From 9ef5265ef15579db0127b1fce25e75e91ae4f724 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Thu, 20 Jul 2023 15:36:56 -0500 Subject: [PATCH 2/2] Add a couple missing commas to code snippets --- docs/helpers.asciidoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/helpers.asciidoc b/docs/helpers.asciidoc index 0c7de0983..4815ebc4a 100644 --- a/docs/helpers.asciidoc +++ b/docs/helpers.asciidoc @@ -281,7 +281,7 @@ helper uses those options in conjunction with the Bulk API call. [source,js] ---- const result = await client.helpers.bulk({ - datasource: [...] + datasource: [...], onDocument (doc) { return { index: { _index: 'my-index' } @@ -342,7 +342,7 @@ const client = new Client({ auth: { apiKey: 'base64EncodedKey' } }) const result = await client.helpers.bulk({ - datasource: [...] + datasource: [...], onDocument (doc) { return [ { index: { _index: 'my-index' } },