2

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.

1 Answer 1

3

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

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

3 Comments

app.post('/api/resources/:id', function(req, res) { var resource = req.body; var param = { index: 'test', type: 'tes', _id : req.params.id, body: {doc:resource, doc_as_upsert: true } }; 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); }); // }); });
app.post('/api/resources/:id', function(req, res) { // var resource = req.body; var resource = {doc: req.body}; var param = { index: 'test', type: 'tes', _id : req.params.id, body: {doc:resource, doc_as_upsert: true } }; 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); }); // }); });
You still have _id instead of id

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.