Bulkresponse undefined

I'm using the node js plugin to do bulk indexing. (followed this link)

The only things I've changed are the data set and the index creation (i created indices before).

The code works fine to index but there is something that is throwing an error at the end of each run.

TypeError: Cannot read property 'errors' of undefined
    at upload (/app/index.js:130:24)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Here is my code

async function upload (body) {
  const client = new elasticsearch.Client({
  host: ELASTICSEARCH_HOST
})
  const { body: bulkResponse } = await client.bulk({ refresh: true, body })

  if (bulkResponse.errors) {
    const erroredDocuments = []
    // The items array has the same order of the dataset we just indexed.
    // The presence of the `error` key indicates that the operation
    // that we did for the document has failed.
    bulkResponse.items.forEach((action, i) => {
      const operation = Object.keys(action)[0]
      if (action[operation].error) {
        erroredDocuments.push({
          // If the status is 429 it means that you can retry the document,
          // otherwise it's very likely a mapping error, and you should
          // fix the document before to try it again.
          status: action[operation].status,
          error: action[operation].error,
          operation: body[i * 2],
          document: body[i * 2 + 1]
        })
      }
    })
    console.log(erroredDocuments)
  }

  const { body: count } = await client.count({ index: category })
  console.log(count)
};

The only thing I have changed is that I call the function for each body I have generated so they get uploaded to different indices.

The only "errors" property I have is from bulkResponse object.

const { body: bulkResponse } = await client.bulk({ refresh: true, body })

  if (bulkResponse.errors) {
    const erroredDocuments = []

Could it be that for some reason is not getting created when elastic makes the response?

I'm opening this topic so I can fix it and the error doesn't show up every time it makes the bulk upload.

If there is a mistake in the code it would help elastic to update that example.

try removing the { body: } from the const's, got it to work for me... not sure why they are constructing the variable that way.

updates:
const bulkResponse = await client.bulk({ refresh: true, body })

const count = await client.count({ index: 'tweets' })

Gonna try that.

From my understanding the

if (bulkResponse.errors) {}
Which is the part that is failing should only trigger if there is an error.. since there is none and the variable is "undefined" it should not run.

My understanding is that undefined will give a false since it is a "falsy"

But again.. maybe I'm wrong...

Tried that code, the same error is showing up.

maybe they have changed the code and bulkResponse.errors is not anymore?

gonna check the data returned

UPDATE: The code literaly returns that the object bulkResponse is undefined.

'use strict'

require('array.prototype.flatmap').shim()
const { Client } = require('elasticsearch')
const client = new Client({
  node: 'http://localhost:9200'
})

async function run () {
  await client.indices.create({
    index: 'tweets',
    body: {
      mappings: {
        properties: {
          id: { type: 'integer' },
          text: { type: 'text' },
          user: { type: 'keyword' },
          time: { type: 'date' }
        }
      }
    }
  }, { ignore: [400] })

  const dataset = [{
    id: 1,
    text: 'If I fall, don\'t bring me back.',
    user: 'jon',
    date: new Date()
  }, {
    id: 2,
    text: 'Witer is coming',
    user: 'ned',
    date: new Date()
  }, {
    id: 3,
    text: 'A Lannister always pays his debts.',
    user: 'tyrion',
    date: new Date()
  }, {
    id: 4,
    text: 'I am the blood of the dragon.',
    user: 'daenerys',
    date: new Date()
  }, {
    id: 5, // change this value to a string to see the bulk response with errors
    text: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
    user: 'arya',
    date: new Date()
  }]

  const body = dataset.flatMap(doc => [{ index: { _index: 'tweets' } }, doc])

  const bulkResponse = await client.bulk({ refresh: true, body })

  if (bulkResponse.errors) {
    const erroredDocuments = []
    // The items array has the same order of the dataset we just indexed.
    // The presence of the `error` key indicates that the operation
    // that we did for the document has failed.
    bulkResponse.items.forEach((action, i) => {
      const operation = Object.keys(action)[0]
      if (action[operation].error) {
        erroredDocuments.push({
          // If the status is 429 it means that you can retry the document,
          // otherwise it's very likely a mapping error, and you should
          // fix the document before to try it again.
          status: action[operation].status,
          error: action[operation].error,
          operation: body[i * 2],
          document: body[i * 2 + 1]
        })
      }
    })
    console.log(erroredDocuments)
  }

  const count = await client.count({ index: 'tweets' })
  console.log(count)
}

run().catch(console.log)

Works for me, what version of ES are you running? this works in 7.6.1

I was able to do it.

I was using const {bulkResponse} and not const bulkResponse

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.