How to use bulk operator in elasticsearch

298 Views Asked by At

I am using the elasticsearch client.

var elasticsearch = require('elasticsearch');
   var client = new elasticsearch.Client({
   host:"https://********",
   log: 'trace',
});

I have JSON objects lets say

[{"name":"abc", "age": 23},{"name":"bcd", "age": 25}......]

My Goal to insert it in bulk. I tried but not working.

client.bulk({
    index: "person",
    type: '_doc',
    body: [{JSON}] // input as JSON format

})
1

There are 1 best solutions below

0
Alex Baidan On

Main thing that need to remember about bulk API, that:

The actions are specified in the request body using a newline delimited JSON (NDJSON) structure.

So it is really important to follow request body formatting, with new lines, action and metadata.

Example:

POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }

Documentation