elasticsearch,Node.js,Curl,elasticsearch" /> elasticsearch,Node.js,Curl,elasticsearch" />

Node.js 通过节点js api更新elasticsearch文档

Node.js 通过节点js api更新elasticsearch文档,node.js,curl,elasticsearch,Node.js,Curl,elasticsearch,我正在尝试通过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:resour

我正在尝试通过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
*正在尝试::1。。。
*TCP_节点集
*已连接到本地主机(::1)端口3000(#0)
>POST/api/resources/AV2EbdzXWlL5FjuPDx0r HTTP/1.1
>主机:localhost:3000
>用户代理:curl/7.51.0
>接受:*/*
>内容长度:81
>内容类型:application/x-www-form-urlencoded
> 
*上传已完全发送:81个字节中有81个

文档未得到更新。有人能帮我找出我做错了什么。

你的
参数
散列不正确,
\u 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

请参阅官方文档

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(“修改资源:”,req.params.id,resource);client.update(参数,函数(错误,响应){//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-param={doc req.body};var-index:'test,type:'tes',id:req.params.id,body:{doc:resource,doc_as_upsert:true};console.log(“修改资源:,req.params.id,resource);client.update(参数,函数(错误,响应){//client.get({index:'test',type:'tes',id:req.params.id},函数(错误,资源){res send(响应);//});您仍然有
\u id
而不是
id