@@ -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]
0 commit comments