Skip to content

Commit a39da4e

Browse files
committed
Fix search setup.
1 parent a11193a commit a39da4e

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

dotnetv4/IoT/Actions/IoTWrapper.cs

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,7 @@ public async Task<List<ThingDocument>> SearchIndexAsync(string queryString)
384384
{
385385
try
386386
{
387-
await _amazonIoT.UpdateIndexingConfigurationAsync(
388-
new UpdateIndexingConfigurationRequest()
389-
{
390-
ThingIndexingConfiguration = new ThingIndexingConfiguration()
391-
{
392-
ThingIndexingMode = ThingIndexingMode.REGISTRY
393-
}
394-
});
395-
387+
// First, try to perform the search
396388
var request = new SearchIndexRequest
397389
{
398390
QueryString = queryString
@@ -402,6 +394,16 @@ await _amazonIoT.UpdateIndexingConfigurationAsync(
402394
_logger.LogInformation($"Search found {response.Things.Count} Things");
403395
return response.Things;
404396
}
397+
catch (Amazon.IoT.Model.IndexNotReadyException ex)
398+
{
399+
_logger.LogWarning($"Search index not ready, setting up indexing configuration: {ex.Message}");
400+
return await SetupIndexAndRetrySearchAsync(queryString);
401+
}
402+
catch (Amazon.IoT.Model.ResourceNotFoundException ex) when (ex.Message.Contains("index") || ex.Message.Contains("Index"))
403+
{
404+
_logger.LogWarning($"Search index not configured, setting up indexing configuration: {ex.Message}");
405+
return await SetupIndexAndRetrySearchAsync(queryString);
406+
}
405407
catch (Amazon.IoT.Model.ThrottlingException ex)
406408
{
407409
_logger.LogWarning($"Request throttled, please try again later: {ex.Message}");
@@ -413,6 +415,78 @@ await _amazonIoT.UpdateIndexingConfigurationAsync(
413415
return new List<ThingDocument>();
414416
}
415417
}
418+
419+
/// <summary>
420+
/// Sets up the indexing configuration and retries the search after waiting for the index to be ready.
421+
/// </summary>
422+
/// <param name="queryString">The search query string.</param>
423+
/// <returns>List of Things that match the search criteria, or empty list if setup/search failed.</returns>
424+
private async Task<List<ThingDocument>> SetupIndexAndRetrySearchAsync(string queryString)
425+
{
426+
try
427+
{
428+
// Update indexing configuration to REGISTRY mode
429+
_logger.LogInformation("Setting up IoT search indexing configuration...");
430+
await _amazonIoT.UpdateIndexingConfigurationAsync(
431+
new UpdateIndexingConfigurationRequest()
432+
{
433+
ThingIndexingConfiguration = new ThingIndexingConfiguration()
434+
{
435+
ThingIndexingMode = ThingIndexingMode.REGISTRY
436+
}
437+
});
438+
439+
_logger.LogInformation("Indexing configuration updated. Waiting for index to be ready...");
440+
441+
// Wait for the index to be set up - this can take some time
442+
const int maxRetries = 10;
443+
const int retryDelaySeconds = 10;
444+
445+
for (int attempt = 1; attempt <= maxRetries; attempt++)
446+
{
447+
try
448+
{
449+
_logger.LogInformation($"Waiting for index to be ready (attempt {attempt}/{maxRetries})...");
450+
await Task.Delay(TimeSpan.FromSeconds(retryDelaySeconds));
451+
452+
// Try to get the current indexing configuration to see if it's ready
453+
var configResponse = await _amazonIoT.GetIndexingConfigurationAsync(new GetIndexingConfigurationRequest());
454+
if (configResponse.ThingIndexingConfiguration?.ThingIndexingMode == ThingIndexingMode.REGISTRY)
455+
{
456+
// Try the search again
457+
var request = new SearchIndexRequest
458+
{
459+
QueryString = queryString
460+
};
461+
462+
var response = await _amazonIoT.SearchIndexAsync(request);
463+
_logger.LogInformation($"Search found {response.Things.Count} Things after index setup");
464+
return response.Things;
465+
}
466+
}
467+
catch (Amazon.IoT.Model.IndexNotReadyException)
468+
{
469+
// Index still not ready, continue waiting
470+
_logger.LogInformation("Index still not ready, continuing to wait...");
471+
continue;
472+
}
473+
catch (Amazon.IoT.Model.InvalidRequestException ex) when (ex.Message.Contains("index") || ex.Message.Contains("Index"))
474+
{
475+
// Index still not ready, continue waiting
476+
_logger.LogInformation("Index still not ready, continuing to wait...");
477+
continue;
478+
}
479+
}
480+
481+
_logger.LogWarning("Timeout waiting for search index to be ready after configuration update");
482+
return new List<ThingDocument>();
483+
}
484+
catch (Exception ex)
485+
{
486+
_logger.LogError($"Couldn't set up search index configuration. Here's why: {ex.Message}");
487+
return new List<ThingDocument>();
488+
}
489+
}
416490
// snippet-end:[iot.dotnetv4.SearchIndex]
417491

418492
// snippet-start:[iot.dotnetv4.DetachThingPrincipal]

scenarios/basics/iot/SPECIFICATION.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ This scenario demonstrates the following key AWS IoT Service operations:
6969
- Use the `ListTopicRules` API to retrieve a list of all AWS IoT Rules.
7070

7171
12. **Search AWS IoT Things**:
72-
- Use the `SearchThings` API to search for AWS IoT Things based on various criteria, such as Thing name, attributes, or shadow state.
72+
- Use the `SearchIndex` API to search for AWS IoT Things based on various criteria, such as Thing name, attributes, or shadow state.
73+
- **Automatic Index Configuration**: The search functionality includes intelligent handling of index setup:
74+
- If the search index is not configured, the system automatically detects this condition through exception handling
75+
- Catches `IndexNotReadyException` and `InvalidRequestException` that indicate the search index needs to be set up
76+
- Automatically configures the Thing indexing mode to `REGISTRY` to enable search functionality
77+
- Implements a retry mechanism with up to 10 attempts, waiting 10 seconds between each attempt for the index to become ready
78+
- Validates the indexing configuration status before retrying search operations
79+
- Provides detailed logging throughout the index setup process to keep users informed of progress
7380

7481
13. **Delete an AWS IoT Thing**:
7582
- Use the `DeleteThing` API to delete an AWS IoT Thing.

0 commit comments

Comments
 (0)