我正在尝试通过elasticsearch中的Node.js api更新具有特定ID的文档。我是Node.js的新手,所以我无法弄清楚更新失败的原因。以下是我webapp.js
文件中的相关snipet:
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);
});
// });
});
我正在使用此命令从命令行通过curl对此进行测试:
curl -v \
--data '{"name":"xxx"}' \
http://localhost:3000/api/resources/AV2EbdzXWlL5FjuPDx0r
以下是我从命令行收到的响应:
* 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
该文档未获得更新。有人可以帮我弄清楚我在做错了什么。
答案 0 :(得分:2)
您的param
哈希不正确,_id
应为id
,doc
应为body
。另一件事是,对于部分更新,您需要将部分字段括在doc
结构中:
var resource = {doc: req.body};
var param = { index: 'test', type: 'tes', id: req.params.id, body: resource }
^ ^
| |
change these two params
请参阅官方文档here