1

I'm trying to store all Json objects through elasticsearch.

client.create({
                index: 'index',
                type: 'type',
                id:"1"
                body:result[0]
                },function (error,response)
                   {
                     if (error) 
                     {
                        console.log('elasticsearch cluster is down!');
                      } 
                      else 
                      {
                        console.log('All is well');
                      }
            });

In this result[0] I'm getting my first value of a Json object but I need to store all Json objects dynamically.

The output which i'm getting is:

  -> POST http://localhost:9200/index/type/1?op_type=create
  {
    "Name": "Martin",
    "Age": "43",
    "Address": "trichy"
  }
  <- 201
  {
    "_index": "index",
    "_type": "type",
    "_id": "1",
    "_version": 4,
    "created": true
  }

But I need an output like this:

 -> POST http://localhost:9200/index/type/1?op_type=create
      {
        "Name": "Martin",
        "Age": "43",
        "Address": "trichy"
      },
      {
        "Name": "vel",
        "Age": "23",
        "Address": "chennai"
      },
      {
        "Name": "ajay",
        "Age": "23",
        "Address": "chennai"
      }
      <- 201
      {
        "_index": "index",
        "_type": "type",
        "_id": "1",
        "_version": 4,
        "created": true
      }

1 Answer 1

2

What you need is to use the bulk endpoint in order to send many documents at the same time.

The body contains two rows per document, the first row contains the index, type and id of the document and the document itself is in the next row. Rinse and repeat for each document.

client.bulk({
  body: [
    // action description
    { index:  { _index: 'index', _type: 'type', _id: 1 } },
     // the document to index
    { Name: 'Martin', Age: 43, Address: 'trichy' },
    { index:  { _index: 'index', _type: 'type', _id: 2 } },
    { Name: 'vel', Age: 23, Address: 'chennai' },
    { index:  { _index: 'index', _type: 'type', _id: 3 } },
    { Name: 'ajay', Age: 23, Address: 'chennai' }
  ]
}, function (err, resp) {
  // ...
});

I suspect your result array is the JSON you get from your other question from yesterday. If so, then you can build the bulk body dynamically, like this:

var body = [];
result.forEach(function(row, id) {
    body.push({ index:  { _index: 'index', _type: 'type', _id: (id+1) } });
    body.push(row);
});

Then you can use the body in your bulk call like this:

client.bulk({
  body: body
}, function (err, resp) {
  // ...
});
Sign up to request clarification or add additional context in comments.

3 Comments

Great! Always glad to help!
Hi Val, how can i create mapping before bulk upload.Is this possible to do? I don't know how to do please guide me to do this.
I've answered here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.