Skip to content

[Backport 8.8] Add docs for bulk helper improvement #1957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions docs/helpers.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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: '<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]]
Expand Down Expand Up @@ -574,4 +601,4 @@ const scrollSearch = client.helpers.scrollDocuments({
for await (const doc of scrollSearch) {
console.log(doc)
}
----
----