Skip to content

Commit d814447

Browse files
author
Derek Legenzoff
authored
Merge pull request #47 from Azure-Samples/optimize-indexing
Updating retry logic for V11 sdk
2 parents 6a55138 + 3fb6173 commit d814447

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

optimize-data-indexing/v11/OptimizeDataIndexing/ExponentialBackoff.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Azure.Search.Documents.Models;
44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Threading.Tasks;
78

89
namespace OptimizeDataIndexing
@@ -14,6 +15,8 @@ private static async Task<IndexDocumentsResult> ExponentialBackoffAsync(SearchCl
1415
{
1516
// Create batch of documents for indexing
1617
var batch = IndexDocumentsBatch.Upload(hotels);
18+
19+
// Create an object to hold the result
1720
IndexDocumentsResult result = null;
1821

1922
// Define parameters for exponential backoff
@@ -28,12 +31,43 @@ private static async Task<IndexDocumentsResult> ExponentialBackoffAsync(SearchCl
2831
{
2932
attempts++;
3033
result = await searchClient.IndexDocumentsAsync(batch).ConfigureAwait(false);
34+
35+
var failedDocuments = result.Results.Where(r => r.Succeeded != true).ToList();
36+
37+
// handle partial failure
38+
if (failedDocuments.Count > 0)
39+
{
40+
41+
if (attempts == maxRetryAttempts)
42+
{
43+
Console.WriteLine("[MAX RETRIES HIT] - Giving up on the batch starting at {0}", id);
44+
break;
45+
}
46+
else
47+
{
48+
Console.WriteLine("[Batch starting at doc {0} had partial failure]", id);
49+
//Console.WriteLine("[Attempt: {0} of {1} Failed]", attempts, maxRetryAttempts);
50+
Console.WriteLine("[Retrying {0} failed documents] \n", failedDocuments.Count);
51+
52+
// creating a batch of failed documents to retry
53+
var failedDocumentKeys = failedDocuments.Select(doc => doc.Key).ToList();
54+
hotels = hotels.Where(h => failedDocumentKeys.Contains(h.HotelId)).ToList();
55+
batch = IndexDocumentsBatch.Upload(hotels);
56+
57+
Task.Delay(delay).Wait();
58+
delay = delay * 2;
59+
continue;
60+
}
61+
}
62+
63+
3164
return result;
3265
}
3366
catch (RequestFailedException ex)
3467
{
35-
Console.WriteLine("BATCH STARTING AT DOC {0}:", id);
36-
Console.WriteLine("[Attempt: {0} of {1} Failed] - Error: {2} \n", attempts, maxRetryAttempts, ex.Message);
68+
Console.WriteLine("[Batch starting at doc {0} failed]", id);
69+
//Console.WriteLine("[Attempt: {0} of {1} Failed] - Error: {2} \n", attempts, maxRetryAttempts, ex.Message);
70+
Console.WriteLine("[Retrying entire batch] \n");
3771

3872
if (attempts == maxRetryAttempts)
3973
{

optimize-data-indexing/v11/OptimizeDataIndexing/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static async Task Main(string[] args)
4040

4141
//long numDocuments = 100000;
4242
//DataGenerator dg = new DataGenerator();
43-
//List<Hotel> hotels = dg.GetHotels(numDocuments);
43+
//List<Hotel> hotels = dg.GetHotels(numDocuments, "large");
4444

4545
//Console.WriteLine("{0}", "Uploading using exponential backoff...\n");
4646
//await ExponentialBackoff.IndexDataAsync(searchClient, hotels, 1000, 8);
@@ -71,8 +71,8 @@ private static async Task CreateIndexAsync(string indexName, SearchIndexClient i
7171
// Create a new search index structure that matches the properties of the Hotel class.
7272
// The Address class is referenced from the Hotel class. The FieldBuilder
7373
// will enumerate these to create a complex data structure for the index.
74-
FieldBuilder bulder = new FieldBuilder();
75-
var definition = new SearchIndex(indexName, bulder.Build(typeof(Hotel)));
74+
FieldBuilder builder = new FieldBuilder();
75+
var definition = new SearchIndex(indexName, builder.Build(typeof(Hotel)));
7676

7777
await indexClient.CreateIndexAsync(definition);
7878
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"SearchServiceUri": "<YourSearchServiceSearchServiceUri>",
3-
"SearchServiceAdminApiKey": "<YourSearchServiceAdminApiKey>",
2+
"SearchServiceUri": "https://{service-name}.search.windows.net",
3+
"SearchServiceAdminApiKey": "",
44
"SearchIndexName": "optimize-indexing"
55
}

0 commit comments

Comments
 (0)