Helper methods
The bulk helper simplifies making complex bulk API requests.
The following code creates a bulk helper instance:
Bulk helper operations return an object with the following fields:
total: number,
failed: number,
retry: number,
successful: number,
time: number,
bytes: number,
aborted: boolean
}
Bulk helper configuration options
When creating a new bulk helper instance, you can use the following configuration options.
Examples
Index
The index operation creates a new document if it doesn’t exist and recreates the document if it already exists.
The following bulk operation indexes documents into example-index
:
client.helpers.bulk({
onDocument (doc) {
return {
}
}
})
The following bulk operation indexes documents into example-index
with document overwrite:
Create
The create operation creates a new document only if the document does not already exist.
client.helpers.bulk({
datasource: arrayOfDocuments,
onDocument (doc) {
return {
create: { _index: 'example-index', _id: doc.id }
}
}
})
The following bulk operation creates documents in the example-index
with document overwrite:
client.helpers.bulk({
datasource: arrayOfDocuments,
onDocument (doc) {
return [
{
create: { _index: 'example-index', _id: doc.id }
},
{ ...doc, createdAt: new Date().toISOString() }
}
Update
The update operation updates the document with the fields being sent. The document must already exist in the index.
The following bulk operation updates documents in the arrayOfDocuments
:
The following bulk operation updates documents in the arrayOfDocuments
with document overwrite:
client.helpers.bulk({
datasource: arrayOfDocuments,
onDocument (doc) {
return [
{
update: { _index: 'example-index', _id: doc.id }
},
{
doc: { ...doc, createdAt: new Date().toISOString() },
doc_as_upsert: true
}
]
}
})
Delete
The following bulk operation deletes documents from the example-index
:
client.helpers.bulk({
datasource: arrayOfDocuments,
onDocument (doc) {
return {
delete: { _index: 'example-index', _id: doc.id }
}