2016-05-03 10 views
0

私はノードを使用してREST APIを構築しており、elasticsearchデータベースとの通信を再構築しています。今私はオブジェクトを削除するとき、私はこれをある種の他のオブジェクトへのカスケード削除をしたい。私はこれが本当に弾性検索を使うのではなく、私と一緒に裸であることを知っています。restitutionの複数の機能がelasticsearchクライアントに機能する

は、だからここに私のコードです:だから基本的に私はちょうどホスト名www.test.comを持つすべてのオブジェクトを検索したい

function deleteHostname(req, res, next) { 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    var endpoints = []; 

    client.search({ 
     index: 'test', 
     type: 'something', 
     body: { 
      from: 0, size: 100, 
      query: { 
       match: { 
        hostname: 'www.test.com' 
       } 
      } 
     } 
    }).then(function (error, resp) { 
     if(error) { 
      res.send(error); 
     } 

     endpoints = resp.hits.hits; 

     for (index = 0, len = endpoints.length; index < len; ++index) { 
      client.delete({ 
       index: 'test', 
       type: 'something', 
       id: endpoints[index]._id 
      }, function (error, response) { 
       if(error) { 
        res.send(error); 
       } 
      }); 
     } 

     res.send(endpoints); 
     return next(); 
    }); 
} 

(私はハードそれをテストするために、これをコード化されました)。次に、私が見つけたすべてのオブジェクトを削除したい。それはエラーの経路に従い、私にこれを送ります:

{ 
    "took":1, 
    "timed_out":false, 
    "_shards":{ 
     "total":5, 
     "successful":5, 
     "failed":0 
    }, 
    "hits":{ 
     "total":1, 
     "max_score":2.098612, 
     "hits":[ 
     { 
      "_index":"test", 
      "_type":"something", 
      "_id":"123456", 
      "_score":2.098612, 
      "_source":{ 
       "duration":107182, 
       "date":"2016-05-04 00:54:43", 
       "isExceptional":true, 
       "hostname":"www.test.com", 
       "eta":613, 
       "hasWarnings":false, 
       "grade":"A+", 
       "ipAddress":"ipip", 
       "progress":100, 
       "delegation":2, 
       "statusMessage":"Ready" 
      } 
     } 
     ] 
    } 
} 

私の意見では、これはエラーのようですか?だから私はなぜそれをエラーとして戻すのですか?削除した場合:

 if(error) { 
      res.send(error); 
     } 

私のコードから、私は何の応答も得られません。

ご協力いただきありがとうございます。

答えて

1

あなたが(左へ->で示される変更を参照)、このようなあなたのコードを変更する必要があります。

if(error) { 
1->  return res.send(error); 
    } 

    endpoints = resp.hits.hits; 

    for (index = 0, len = endpoints.length; index < len; ++index) { 
2->  (function(id){ 
      client.delete({ 
       index: 'test', 
       type: 'something', 
3->    id: id 
      }, function (error, response) { 
       if(error) { 
4->     next(error); 
       } 
      }); 
5->  })(endpoints[index._id]); 
    } 

6-> //res.send(endpoints); 

私は今、それぞれの変化を説明しています:

  1. そうでない場合はreturnエラーを送信した後、処理を続行します
  2. (3/5)client.deleteは非同期関数なので、匿名関数で呼び出す必要があります
  3. エラーが発生した場合は、next(error)に電話する必要はありませんres.send
  4. forループはまだ終了していない可能性がありますので、この時点で回答を送信することはできません。

    var async = require('async'); 
        ... 
    
        if(error) { 
         return res.send(error); 
        } 
    
        endpoints = resp.hits.hits; 
    
        async.each(endpoints, 
         function(endpoint, callback) { 
          client.delete({ 
           index: 'test', 
           type: 'something', 
           id: endpoint._id 
          }, callback); 
         }, 
         // this is called when all deletes are done 
         function(err){ 
          if (err) { 
           next(err); 
          } else { 
           res.send(endpoints); 
           next(); 
          } 
         } 
        ); 
    
    はあなたが望むものを正確に達成するために別の解決策がある

    :ループのために、あなたの代わりに優れたasyncライブラリを使用する必要があります代わりに

非同期例を(以下asynch.eachの使用例を参照してください) delete by query pluginを使用してください。この機能を使用すると、上記のすべてを1つのクエリで実行できます。

まだES 1.xにいる場合は、依然としてdelete-by-queryがコアの一部です.JavascriptクライアントのdeleteByQuery functionを呼び出すことができます。

あなたはES 2.xの上にある場合は、削除・バイ・問い合わせは今のプラグインなので、install itによ必要性と、その後もJavascriptのクライアントのためにdeleteByQuery拡張ライブラリを必要と

function deleteHostname(req, res, next) { 
    res.setHeader('Access-Control-Allow-Origin', '*'); 

    client.deleteByQuery({ 
     index: 'test', 
     type: 'something', 
     body: { 
      query: { 
       match: { hostname: 'www.test.com' } 
      } 
     } 
    }, function (error, response) { 
     if (error) { 
      next(error); 
     } else { 
      res.send(endpoints); 
      next(); 
     } 
    }); 
} 
+0

[OK]をので、私がいましたもう一つの事を変えるために何らかの理由でそれが実際に応答している間にいつも私にエラーを与えていたので、それを変更しました。(function(error、resp){to。(function(resp){ – Matthias

+0

ああ、良い点、私はそれを逃した、コールバックはエラー引数を持っていません。 – Val

関連する問題