Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda