Added async generator support in bulk helper (#1138)

* Added async generator support in bulk helper

* Updated test

* Updated docs

* Improved type definitions

* Updated onDrop callback type definition
This commit is contained in:
Tomas Della Vedova
2020-04-03 09:46:26 +02:00
committed by GitHub
parent bdd38597d8
commit df17fb99d0
5 changed files with 115 additions and 17 deletions

View File

@ -43,7 +43,7 @@ To create a new instance of the Bulk helper, you should access it as shown in th
[cols=2*]
|===
|`datasource`
a|An array or a readable stream with the data you need to index/create/update/delete.
a|An array, async generator or a readable stream with the data you need to index/create/update/delete.
It can be an array of strings or objects, but also a stream of json strings or JavaScript objects. +
If it is a stream, we recommend to use the https://www.npmjs.com/package/split2[`split2`] package, that will split the stream on new lines delimiters. +
This parameter is mandatory.
@ -182,6 +182,36 @@ const result = await client.helpers.bulk({
})
----
==== Usage with an async generator
[source,js]
----
const { Client } = require('@elastic/elasticsearch')
async function * generator () {
const dataset = [
{ user: 'jon', age: 23 },
{ user: 'arya', age: 18 },
{ user: 'tyrion', age: 39 }
]
for (const doc of dataset) {
yield doc
}
}
const client = new Client({ node: 'http://localhost:9200' })
const result = await client.helpers.bulk({
datasource: generator(),
onDocument (doc) {
return {
index: { _index: 'my-index' }
}
}
})
console.log(result)
----
=== Search Helper
A simple wrapper around the search API. Instead of returning the entire `result` object it will return only the search documents result.