1

I'm trying to create mapping before uploading json data into elasticsearch. I don't know how to implement mapping before uploading json data in sails.js

This is my bulkupload snippet

     var body = [];
      //row is json data
        rows.forEach(function(row, id) {
             body.push({ index:  { _index: 'testindex', _type: 'testtype', _id: (id+1) } });
             body.push(row);
        })  
    client.bulk({
                    body: body
                }, function (err, resp) {
                        if (err) 
                        {
                            console.log(err);
                            return;
                       }
                      else 
                      { 
                            console.log("All Is Well");
                      }
      });

I want to create mapping before data upload.can any one know how to create mapping in sails.

my Json object

 [ { Name: 'paranthn', Age: '43', Address: 'trichy' },
      { Name: 'Arthick', Age: '23', Address: 'trichy' },
      { Name: 'vel', Age: '24', Address: 'trichy' } ]

2 Answers 2

2

Before making your client.bulk() call you first need to make another client.indices.putMapping() call like this in order to save the correct mapping for the data you're about to send via the bulk call:

client.indices.putMapping({
   "index": "testindex",
   "type": "testtype",
   "body": {
      "testtype": {
          "properties": {
              "your_int_field": {
                  "type": "integer"
              },
              "your_string_field": {
                  "type": "string"
              },
              "your_double_field": {
                  "type": "double"
              },
              // your other fields
          }
      }
   }
}, function (err, response) {
   // from this point on, if you don't get any error, you may call bulk.
});

Remember that all these calls are asynchronous, so be careful to only call bulk once putMapping has returned successfully.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi val it works fine for me. but i json object returs everythig as a string but in my properties i need to set property as a integer how can i acheive that.
The number of fields in the JSON may be dynamic. Then how can I do mapping for this ? i have updated sample json data in my question please refer.
1

Sounds like you need PutMapping.

1 Comment

But i need to do mapping in sails.js before that bulk upload functionality.Is this possible to do?

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.