2017-07-28 9 views
0

elasticsearchのNode.js APIを使用して、特定のIDを持つドキュメントを更新しようとしています。私はNode.jsを初めて使用しているので、なぜ更新が失敗するのか分かりません。私は、このコマンドを使用して、コマンドラインからカール経由して、これをテストしていelasticsearch docをノードjs apiから更新します。

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); 
    }); 
// }); 
}); 

:ここに私のwebapp.jsファイルから関連snipetがある。ここ

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ハッシュが正しくない、_ididであるべきとdocbodyをする必要があります。もう一つは、部分更新のために、あなたは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 /リソース/:ID」を参照してください、関数:(req、res){ var resource = req.body; var param = {index: 'test'、タイプ: 'tes'、_id:req.params.id、body:{doc:resource、doc_as_upsert:true } }; console.log( "リソースの変更:"、req.params.id、resource); client.update(param、function(error、response){ // client.get({index: 'test'、タイプ: '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}; }; console.log( "リソースを変更しています:リソースを変更しています: "、id:req.params(" id:req.params.id、resource); client.update(param、function(error、response){ ) (応答); }); //}); }); – nelalx

関連する問題