Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda