AWS opensearch bulk insert with Nodejs http module

Viewed 58

I have a certain scenario, like I need to do a bulk insert into AWS opensearch. From the AWS opensearch documentation, found that the request body is in the for for ndjson(newline delimited json). In my project I am only able to use the native http module of nodejs. How can I make a bulk insert request into the opensearch using nodejs http module.

I am attaching a code snippet with I used, but it is throwing error code 400 with no error message.

 const body = `{ "index": { "_index": "test_index", "_id": "tt1979320" } },
{ "title": "Rush", "year": 2013 },
{ "create": { "_index": "test_index", "_id": "tt1392214" } },
{ "title": "Prisoners", "year": 2013 }`;
  return new Promise((resolve, reject) => {
        const options = {
          host : 'http://test.us-east-1.es.amazonaws.com',
          path : `/test_index/_bulk`,
          method:'POST',
          port:443,
          region:'us-east-1',
          headers : {
              'Content-Type' : 'application/x-ndjson',
              'Content-Length': Buffer.byteLength(JSON.stringify(body)),
              'Authorization' : AUTH,
              'region' : 'us-east-1',
              
          }
      };
      const req = https.request(options, (res) => {
          if (res.statusCode < 200 || res.statusCode >= 300){
              return reject(new Error('statusCode=' + res.statusCode));
          }
          var resbody = [];
          res.on('data', (chunk) => {
              resbody.push(chunk);
          });
          res.on('end', () => {
              resolve(resbody);
          });
      });
      req.on('error', (e) =>{
          console.log("We got an error from post request",e);
          reject(e.message);
      });
      req.write(JSON.stringify(body));
      req.end();
    });
}

Please help solving this issue. Thank you

0 Answers
Related