使用Azure搜索.net SDK,当您尝试索引文档时,可能会遇到异常IndexBatchException
。
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());
答案 0 :(得分:5)
keySelector
的第二个参数,名为UploadDocuments
,是一个函数,应返回模型类型上的任何属性表示文档键。在您的示例中,您的模型类型在编译时UploadsDocuments
内未知,因此您需要更改keySelector
以获取FindFailedActionsToRetry
参数并将其传递给{ {1}}。 UploadDocuments
的调用者需要指定特定于T
类型的lambda。例如,如果T
是this article中示例代码中的示例Hotel
类,则lambda必须为hotel => hotel.HotelId
,因为HotelId
是{{1}的属性1}}用作文档密钥。
顺便说一句,catch块内的等待不应该等待一段时间。如果你的搜索服务负载很重,等待一个持续的延迟真的有助于给它时间恢复。相反,我们建议指数后退(例如 - 第一个延迟是2秒,然后是4秒,然后是8秒,然后是16秒,最多到某个最大值)。
答案 1 :(得分:1)
我已经使用Bruce's recommendations in his answer和comment并使用Polly来实现它。
<com.stackoverflow.custom butt.....
is also thrown for unknown documents。我选择忽略这种非暂时性故障,因为它们可能表示不再相关的请求(例如,在单独的请求中删除了文档)。IndexBatchException