Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Does the Elasticsearch bulk API JavaScript sample code work correctly?

I want to use Elasticsearch bulk index with JavaScript, and follow the official document. Basically I just used it as is, but it throws an exception saying that the bulk request must be terminated by a newline code, which seems to be JSON lines. My question is, if so, how can I convert the JSON to JSON lines and then pass it to bulk API? or am I missing some options when calling client.bulk?

ResponseError: illegal_argument_exception: [illegal_argument_exception] Reason: The bulk request must be terminated by a newline [\n]
at SniffingTransport.request

I use Elasticsearch v7.15 and Node.js v16.15 on macOS.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I second this. I'm using 8.2 and it doesn't work. I followed the example exactly and it still throws the newline error.

You used to be able to pass in the operation followed by the document in an array and it would work just fine but it seems to have been "updated" for the worse.

I'll keep trying and see if I come up with something.

EDIT: OK. I found something that works for me and I hope it works for you as well.

I made a child client to reuse the connection. It's pretty straight forward but here's the link: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/child.html

I modified the accept and content-type headers for that client and the code for setting up the client looks like so:

const es = require('@elastic/elasticsearch');
const es_hosts = [
  'https://elastic_host1', 
  'https://elastic_host2'
];
const es_client = new es.Client({ node: es_hosts });
const es_bulk = es_client.child({
  headers: {
    'accept': 'application/json',
    'content-type': 'application/json'
  }
})

I'm assuming you're passing in an array which has an operation and a document. We'll convert that array into what equates to be a large string, remembering to tack on the newline.

let bulk_data = [
  { index: { _index: 'my-index' } },
  { id: 1, name: 'dave', job: 'janitor' } }
]

let bulk_body = {
  body: bulk_data.map(JSON.stringify).join('\n') + '\n'
}

const results = await es_bulk.bulk(bulk_body);

Let me know if this helps.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!