通过节点js api更新elasticsearch doc

问题描述:

我试图通过elasticsearch中的Node.js api更新具有特定id的文档。我是Node.js的新手,所以我无法弄清楚为什么更新失败。下面是我的webapp.js文件中的相关snipet:通过节点js api更新elasticsearch doc

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 -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 

该文件没有得到更新。有人能帮我弄清楚我在这里做错了什么。

param哈希是不正确的,_ididdoc应该是body。另一件事是,对于部分更新,您需要封闭部分领域在doc结构:

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

请参见官方文档here

+0

app.post(“/ API /资源/:身份证”,函数(req,res)var resource = req.body; var param = {index:'test',type:'tes',_id:req.params.id,body:{doc:resource,doc_as_upsert:true (“修改资源:”,req.params.id,资源); client.update(param,function(error,response){ // client.get({index:'test',type:'tes',id:req.params.id},function(err,resource){res.send(response); }); //}); }); – nelalx

+0

仍然不能正常工作。 – nelalx

+0

app.post('/ api/resources /:id',function(req,res){var resource = req.body; var resource = {doc:req.body}; (“修改资源:文件”,“文件”,“文件”,“文件”,“文件”,“文件” “,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); }); //}); }); – nelalx