2016-10-13 8 views
1

Azure Search .net SDKを使用すると、ドキュメントのインデックスを作成しようとすると、例外IndexBatchExceptionが発生することがあります。Azure検索.net SDK- "FindFailedActionsToRetry"の使い方は?

From the documentation here

e.FindFailedActionsToRetry失敗したアクションのインデックスを再試行するための新しいバッチを作成するために使用する必要がありますどのように
 try 
     { 
      var batch = IndexBatch.Upload(documents); 
      indexClient.Documents.Index(batch); 
     } 
     catch (IndexBatchException e) 
     { 
      // Sometimes when your Search service is under load, indexing will fail for some of the documents in 
      // the batch. Depending on your application, you can take compensating actions like delaying and 
      // retrying. For this simple demo, we just log the failed document keys and continue. 
      Console.WriteLine(
       "Failed to index some of the documents: {0}", 
       String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key))); 
     } 

私はこのような機能を作成しました:

public void UploadDocuments<T>(SearchIndexClient searchIndexClient, IndexBatch<T> batch, int count) where T : class, IMyAppSearchDocument 
    { 
     try 
     { 
      searchIndexClient.Documents.Index(batch); 
     } 
     catch (IndexBatchException e) 
     { 
      if (count == 5) //we will try to index 5 times and give up if it still doesn't work. 
      { 
       throw new Exception("IndexBatchException: Indexing Failed for some documents."); 
      } 

      Thread.Sleep(5000); //we got an error, wait 5 seconds and try again (in case it's an intermitent or network issue 

      var retryBatch = e.FindFailedActionsToRetry<T>(batch, arg => arg.ToString()); 
      UploadDocuments(searchIndexClient, retryBatch, count++); 
     } 
    } 

をしかし、私はこの部分が間違っていると思う:

var retryBatch = e.FindFailedActionsToRetry<T>(batch, arg => arg.ToString()); 

答えて

3

keySelectorという名前FindFailedActionsToRetry 2番目のパラメータは、返すべき関数でありますモデルタイプのプロパティがドキュメントキーを表します。あなたの例では、あなたのモデルタイプはコンパイル時にUploadDocumentsの中には分かっていないので、keySelectorパラメータをとり、FindFailedActionsToRetryに渡すようにUploadsDocumentsを変更する必要があります。 UploadDocumentsの呼び出し元は、Tに固有のラムダを指定する必要があります。たとえば、Tthis articleというサンプルコードのHotelクラスのサンプルである場合、HotelIdが文書キーとして使用されるHotelのプロパティであるため、ラムダはhotel => hotel.HotelIdである必要があります。

ちなみに、catchブロック内の待機時間は一定の時間を待ってはいけません。検索サービスに負荷がかかっている場合は、一定の遅延を待っても、復旧の時間はほとんどありません。代わりに、指数関数的にバックオフすることをおすすめします(たとえば、最初の遅延は2秒、次に4秒、8秒、16秒、最大で最大)。

+0

ありがとうBruce。私はそれが働くことを見る。私はこのコードを変更しました:var retryBatch = e.FindFailedActionsToRetry (バッチ、searchDoc => searchDoc.id); – richard

+0

皮肉なことに私のコードは指数関数的に後退していましたが、このポストとシンプルさのために、私はそれをわずか5秒に変更しました。私は再びそれを変更します。指数関数的な増加を伴う再試行回数を推薦しますか?私は現在私の鉱山を5に設定している。 – richard

+1

進行中(バッチの最後のインデックス呼び出しよりもアイテムが少ない)であれば、再試行を続けることができ、進捗しない場合にのみ再試行の回数を制限します。その場合、最大リトライ回数は、遅延が指数関数的に増加してからどれくらい待っているかを基準にしてください。指数関数的なものから一定の遅延(例えば、遅延が数分に達した後、またはあなたの作品に見いだされたもののどれか)に切り替えることができる特定の時点を過ぎてください。 –

関連する問題