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:

    1. total: number,
    2. failed: number,
    3. retry: number,
    4. successful: number,
    5. time: number,
    6. bytes: number,
    7. aborted: boolean
    8. }

    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:

    1. client.helpers.bulk({
    2. onDocument (doc) {
    3. return {
    4. }
    5. }
    6. })

    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.

    1. client.helpers.bulk({
    2. datasource: arrayOfDocuments,
    3. onDocument (doc) {
    4. return {
    5. create: { _index: 'example-index', _id: doc.id }
    6. }
    7. }
    8. })

    The following bulk operation creates documents in the example-index with document overwrite:

    1. client.helpers.bulk({
    2. datasource: arrayOfDocuments,
    3. onDocument (doc) {
    4. return [
    5. {
    6. create: { _index: 'example-index', _id: doc.id }
    7. },
    8. { ...doc, createdAt: new Date().toISOString() }
    9. }

    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:

    1. client.helpers.bulk({
    2. datasource: arrayOfDocuments,
    3. onDocument (doc) {
    4. return [
    5. {
    6. update: { _index: 'example-index', _id: doc.id }
    7. },
    8. {
    9. doc: { ...doc, createdAt: new Date().toISOString() },
    10. doc_as_upsert: true
    11. }
    12. ]
    13. }
    14. })

    Delete

    The following bulk operation deletes documents from the example-index:

    1. client.helpers.bulk({
    2. datasource: arrayOfDocuments,
    3. onDocument (doc) {
    4. return {
    5. delete: { _index: 'example-index', _id: doc.id }
    6. }