使用Azure Search .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());

最佳答案

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

顺便说一句,在catch块内的等待不应等待固定的时间。如果您的搜索服务负担沉重,那么等待持续的延迟并不会真正给它带来恢复的时间。相反,我们建议以指数形式退缩(例如-第一个延迟是2秒,然后是4秒,然后是8秒,然后是16秒,直到某个最大值)。

关于Azure Search .net SDK-如何使用 "FindFailedActionsToRetry"?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40012885/

10-09 22:29