0

I have this call:

  const lst = [];  // an array with a few object in it 

  c.bulk({
    index: 'foo',
    body: lst.map(v => JSON.stringify(v)).join('\n')
  })
   .catch(e => {
     log.error(e);
   });

I am trying to insert multiple items into ES. The bulk API is here: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

but for some reason with the above call I get this error:

ResponseError: illegal_argument_exception at IncomingMessage. (/Users/alex/codes/interos/@interos/elastic-search/node_modules/@elastic/elasticsearch/lib/Transport.js:287:25) at IncomingMessage.emit (events.js:208:15) at endReadableNT (_stream_readable.js:1154:12) at processTicksAndRejections (internal/process/task_queues.js:77:11)

I tried using this:

  c.bulk({
    method: 'POST',
    index: 'foo',
    body: lst.map(v => JSON.stringify(v)).join('\n') + '\n'
  })
   .catch(e => {
     log.error(e);
   });

used method:POST and ensured there was a newline at the end of the body, still the same error :(

1 Answer 1

1

When using the JS bulk API you don't need to stringify anything and most importantly you're missing the command lines before each object you want to index.

Your body should be constructed like this instead:

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

c.bulk({
  index: 'foo',
  body: body
})
.catch(e => {
  log.error(e);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.