Azure搜索.net SDK-如何使用"FindFailedActionsToRetry"? [英] Azure Search .net SDK- How to use "FindFailedActionsToRetry"?
问题描述
使用Azure Search .net SDK,当您尝试为文档建立索引时,可能会出现异常IndexBatchException
.
Using the Azure Search .net SDK, when you try to index documents you might get an exception 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创建一个新批处理,以重试为失败的操作编制索引?
How should e.FindFailedActionsToRetry be used to create a new batch to retry the indexing for failed actions?
我已经创建了一个像这样的函数:
I've created a function like this:
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++);
}
}
但是我认为这部分是错误的:
But I think this part is wrong:
var retryBatch = e.FindFailedActionsToRetry<T>(batch, arg => arg.ToString());
推荐答案
FindFailedActionsToRetry
的第二个参数名为keySelector
,该函数应返回模型类型上代表文档密钥的任何属性.在您的示例中,UploadDocuments
中的模型类型在编译时未知,因此您需要更改UploadsDocuments
来同时使用keySelector
参数并将其传递给FindFailedActionsToRetry
. UploadDocuments
的调用者将需要指定特定于类型T
的lambda.例如,如果T
是
The second parameter to FindFailedActionsToRetry
, named keySelector
, is a function that should return whatever property on your model type represents your document key. In your example, your model type is not known at compile time inside UploadDocuments
, so you'll need to change UploadsDocuments
to also take the keySelector
parameter and pass it through to FindFailedActionsToRetry
. The caller of UploadDocuments
would need to specify a lambda specific to type T
. For example, if T
is the sample Hotel
class from the sample code in this article, the lambda must be hotel => hotel.HotelId
since HotelId
is the property of Hotel
that is used as the document key.
顺便说一句,在catch块内的等待不应等待固定的时间.如果您的搜索服务负担沉重,那么等待持续的延迟并不会真正给它带来恢复的时间.相反,我们建议以指数形式退缩(例如-第一个延迟是2秒,然后是4秒,然后是8秒,然后是16秒,直到某个最大值).
Incidentally, the wait inside your catch block should not wait a constant amount of time. If your search service is under heavy load, waiting for a constant delay won't really help to give it time to recover. Instead, we recommend exponentially backing off (e.g. -- the first delay is 2 seconds, then 4 seconds, then 8 seconds, then 16 seconds, up to some maximum).
这篇关于Azure搜索.net SDK-如何使用"FindFailedActionsToRetry"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!