# Problems with bulk indexing via JavaScript client

**URL:** https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535
**Category:** Elasticsearch
**Created:** [July 27, 2019, 8:32pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535 "2019-07-27T20:32:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![waynesmallman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waynesmallman/32/18596_2.png) [@waynesmallman](https://discuss.elastic.co/u/waynesmallman)
#### Post date: [July 27, 2019, 8:32pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/1 "2019-07-27T20:32:06Z")

</div>

Hi, I'm using a variant of the code for bulk indexing from [the documentation for Elastic 6.x](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/6.x/bulk_examples.html), and I'm having a few problems.

Here's a typical document:

```
{ index: { _index: 'messages', _type: '_doc', _id: 21653 } },
  { user_id: 11,
    creation: '2016-11-07T15:43:03:000',
    modification: '2019-04-28T11:40:16:000',
    title:
     'Dark Workflows: How 5 Signatures Became a Productivity Write-Off',
    note: 'REMOVED',
    links_to_asset: [[Object] ],
    number_of_words: 804,
    phrases: [],
    favourite: [],
    message_id: 13,
    from: 'Wayne Smallman <REMOVED>',
    seen: 'unread',
    to_read: [],
    engagement: [],
    in_folder_id: 587 }

```

... and then it gets so far in and I get the following error:

> { took: 20,  
> errors: true,  
> items:  
> [{ index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] },  
> { index: [Object] } ] }

Let's face it, as errors go, that's not in the least bit helpful, nor do I understand where the data has gotten to for each of the corresponding indexes, assuming the data should have been listed there.

You'll have noticed `links_to_asset: [[Object] ]`, and I've tried converting each of the _raw_ objects with `JSON.stringify()` versions, but that makes no difference.

Looking at the data, and after having added _lots_ of tracer comments, it's choking on the first attempt at a bulk index, although — as explained — the data _appears_ to be correct.

Some guidance would be much appreciated.

---

<div class="post-metadata">

### Author: ![waynesmallman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waynesmallman/32/18596_2.png) [@waynesmallman](https://discuss.elastic.co/u/waynesmallman)
#### Post date: [July 28, 2019, 4:56pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/2 "2019-07-28T16:56:31Z")

</div>

I'm not sure if this constitutes a degree of success or not, but after a few changes I'm not getting the unhelpful error.

```
async function runBulkInsert () {
  const { body: bulkResponse } = await client.bulk({
    refresh: true,
    body: [
      indexForBulk,
      assetAttributes
    ]
  })
  if (bulkResponse.errors) {
    console.log("Elastic:createIndicesReader:runBulkInsert:bulkResponse.errors", bulkResponse)
    process.exit(1)
  }
}
runBulkInsert().catch(error => {
  console.log("Elastic:createIndicesReader:runBulkInsert:catch", error.meta.body.error)
})

```

Here, `indexForBulk` and `assetAttributes` correspond to the index and document.

Regardless of the _lack of_ errors, the bulk import still isn't working (I've checked with Postman and Kibana).

I've spent the entire weekend on this, and the data I'm sending to `bulk()` is consistent with the documentation, so I'm at a stage where I've exceeded the entire range of permutations of code I know.

---

<div class="post-metadata">

### Author: ![waynesmallman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waynesmallman/32/18596_2.png) [@waynesmallman](https://discuss.elastic.co/u/waynesmallman)
#### Post date: [July 28, 2019, 7:14pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/3 "2019-07-28T19:14:28Z")

</div>

I tried something different:

```
axios.post(`localhost:9200/_bulk`, parameters)
.then(function (response) {
  // handle success
  console.log("Elastic:axios:success", response);
})
.catch(function (error) {
  // handle error
  console.log("Elastic:axios:error", error);
})

```

... but now I'm getting another error:

> { Error: Request failed with status code 400  
> at createError (/REMOVED/node\_modules/axios/lib/core/createError.js:16:15)  
> at settle (/REMOVED/node\_modules/axios/lib/core/settle.js:17:12)  
> at IncomingMessage.handleStreamEnd (/REMOVED/node\_modules/axios/lib/adapters/http.js:237:11)  
> at IncomingMessage.emit (events.js:187:15)  
> at endReadableNT (\_stream\_readable.js:1092:12)  
> at process.\_tickCallback (internal/process/next\_tick.js:63:19)  
> config:  
> { url: '[http://localhost:9200/\_bulk](http://localhost:9200/_bulk)',  
> method: 'post',  
> data: ...

---

<div class="post-metadata">

### Author: ![delvedor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/delvedor/32/67639_2.png) [@delvedor](https://discuss.elastic.co/u/delvedor)
#### Post date: [July 29, 2019, 9:09am UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/4 "2019-07-29T09:09:41Z")

</div>

Hello!  
It looks like that the date format you are using is not correct:  
Can you try to change it from `2016-11-07T15:43:03:000` to `2016-11-07T15:43:03.000` _(the last column should be a dot)_.  
If the issue persists I recommend you to run a single index operation, so you can easily see the error that elasticsearch is sending back to you.  
If you can't fix the issue after the steps above, can you open a new issue in the [JavaScript client](https://github.com/elastic/elasticsearch-js) and use [this repository](https://github.com/delvedor/es-reproduce-issue) to reproduce your issue?  
It would be very useful if you also write what you are getting and the expected behavior.  
Thanks!

---

<div class="post-metadata">

### Author: ![waynesmallman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waynesmallman/32/18596_2.png) [@waynesmallman](https://discuss.elastic.co/u/waynesmallman)
#### Post date: [July 29, 2019, 7:32pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/5 "2019-07-29T19:32:45Z")

</div>

Hi! In the end, that was one of several issues, but I wouldn't have known until I made a change to the bulk error checking:

```
if (responseForBulkIndex.errors) {
  console.log("Elastic:createIndicesReader:runBulkInsert:responseForBulkIndex.errors")
  responseForBulkIndex.items.forEach(function(error) {
    console.log("Elastic:createIndicesReader:runBulkInsert:responseForBulkIndex.error", error)
  })
  process.exit(1)
}

```

Here, I'm looping over the `[object]` items to get at what was inside them, which was a stream of errors.

It would be useful to have Elastic surface these errors instead of burying them.

---

<div class="post-metadata">

### Author: ![delvedor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/delvedor/32/67639_2.png) [@delvedor](https://discuss.elastic.co/u/delvedor)
#### Post date: [July 30, 2019, 2:38pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/6 "2019-07-30T14:38:46Z")

</div>

I'm sorry that you encountered some issues for reading the errors of the bulk API.  
We'll work on improving the code examples and also provide a bulk helper in the future!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 27, 2019, 2:38pm UTC](https://discuss.elastic.co/t/problems-with-bulk-indexing-via-javascript-client/192535/7 "2019-08-27T14:38:50Z")

</div>

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