Azure搜索.net SDK-如何使用" FindFailedActionsToRetry"?

时间:2016-10-13 05:23:34

标签: azure azure-search azure-search-.net-sdk

使用Azure搜索.net SDK,当您尝试索引文档时,可能会遇到异常IndexBatchException

From the documentation here

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

如何使用e.FindFailedActionsToRetry创建新批处理以重试失败操作的索引?

我创建了这样的函数:

    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());

2 个答案:

答案 0 :(得分:5)

keySelector的第二个参数,名为UploadDocuments,是一个函数,应返回模型类型上的任何属性表示文档键。在您的示例中,您的模型类型在编译时UploadsDocuments内未知,因此您需要更改keySelector以获取FindFailedActionsToRetry参数并将其传递给{ {1}}。 UploadDocuments的调用者需要指定特定于T类型的lambda。例如,如果Tthis article中示例代码中的示例Hotel类,则lambda必须为hotel => hotel.HotelId,因为HotelId是{{1}的属性1}}用作文档密钥。

顺便说一句,catch块内的等待不应该等待一段时间。如果你的搜索服务负载很重,等待一个持续的延迟真的有助于给它时间恢复。相反,我们建议指数后退(例如 - 第一个延迟是2秒,然后是4秒,然后是8秒,然后是16秒,最多到某个最大值)。

答案 1 :(得分:1)

我已经使用Bruce's recommendations in his answercomment并使用Polly来实现它。

  • 指数回退最多一分钟,此后每隔一分钟重试一次。
  • 只要有进展,请重试。 5个请求后超时,没有任何进展。
  • <com.stackoverflow.custom butt..... is also thrown for unknown documents。我选择忽略这种非暂时性故障,因为它们可能表示不再相关的请求(例如,在单独的请求中删除了文档)。
IndexBatchException