简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM