2017-07-28 96 views
0

我試圖通過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 

該文件沒有得到更新。有人能幫我弄清楚我在這裏做錯了什麼。

回答

1

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