nelalx
nelalx

Reputation: 69

Update elasticsearch doc via node js api

I am trying to update a doc with a particular id via Node.js api in elasticsearch. I am new to Node.js, so I can't figure out why the update fails. Here is the relevant snipet from my webapp.js file:

app.post('/api/resources/:id', function(req, res) {
 var resource = req.body;
var param = { index: 'test', type: 'tes', _id : req.params.id, doc:resource
};
  console.log("Modifying resource:", req.params.id, resource);
  client.update(param, function (error, response) {
//   client.get({ index: 'test', type: 'tes', id: req.params.id }, function(err, resource) {
      res.send(response);
    });
//  });
});

I am testing this via curl from command line using this command :

curl -v \
  --data '{"name":"xxx"}' \
  http://localhost:3000/api/resources/AV2EbdzXWlL5FjuPDx0r

Here is the response I receive from the command line:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /api/resources/AV2EbdzXWlL5FjuPDx0r HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.51.0
> Accept: */*
> Content-Length: 81
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 81 out of 81 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Date: Fri, 28 Jul 2017 16:40:53 GMT
< Connection: keep-alive
< Content-Length: 0
< 
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact

The document is not getting updated. Can someone help me figure out what I am doing wrong here.

Upvotes: 2

Views: 5499

Answers (1)

Val
Val

Reputation: 217584

Your param hash is not correct, _id should be id and doc should be body. Another thing is that for a partial update, you need to enclose the partial fields in a doc structure:

var resource = {doc: req.body};
var param = { index: 'test', type: 'tes', id: req.params.id, body: resource }
                                           ^                   ^
                                           |                   |
                                          change these two params

See the official documentation here

Upvotes: 3

Related Questions