// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddSingleton <IConfiguration>(Configuration); string serviceName = Configuration["SearchService_ServiceName"]; string apiKey = Configuration["SearchService_ApiKey"]; string indexName = Configuration["SearchService_HotelIndexName"]; string storageConnectionString = Configuration["AzureBlobConnectionString"]; string accountName = Configuration["AzureBlobAccountName"]; string accountKey = Configuration["AzureBlobAccountKey"]; string containerName = Configuration["AzureBlobContainerName"]; string indexAiName = Configuration["AiSearchIndexName"]; string indexerAiName = Configuration["AiSearchIndexerName"]; string cognitiveServiceKey = Configuration["CognitiveServiceKey"]; // Create a SearchIndexClient to send create/delete index commands Uri serviceEndpoint = new Uri($"https://{serviceName}.search.windows.net/"); AzureKeyCredential credential = new AzureKeyCredential(apiKey); SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential); SearchIndexerClient indexerClient = new SearchIndexerClient(serviceEndpoint, credential); // Create a SearchClient to load and query documents SearchClient srchclient = new SearchClient(serviceEndpoint, indexName, credential); SearchClient aiSrchclient = new SearchClient(serviceEndpoint, indexAiName, credential); services.AddTransient <ISearchIndexService, SearchIndexService>(s => new SearchIndexService(srchclient, adminClient)); services.AddTransient <ISearchService, SearchService>(s => new SearchService(srchclient)); services.AddTransient <IAiSearchIndexService, AiSearchIndexService>(s => new AiSearchIndexService(adminClient, indexerClient, storageConnectionString, containerName, indexAiName, indexerAiName, cognitiveServiceKey)); services.AddTransient <IAiSearchService, AiSearchService>(s => new AiSearchService(aiSrchclient, accountName, accountKey, containerName)); services.AddControllersWithViews(); services.AddRazorPages(); }
static void Main(string[] args) { DataSourceName = ConfigurationManager.AppSettings["DataSourceName"]; IndexName = ConfigurationManager.AppSettings["IndexName"]; SkillsetName = ConfigurationManager.AppSettings["SkillsetName"]; IndexerName = ConfigurationManager.AppSettings["IndexerName"]; SynonymMapName = ConfigurationManager.AppSettings["SynonymMapName"]; BlobContainerNameForImageStore = ConfigurationManager.AppSettings["BlobContainerNameForImageStore"]; Uri searchServiceEndpoint = new Uri(string.Format("https://{0}.{1}", ConfigurationManager.AppSettings["SearchServiceName"], ConfigurationManager.AppSettings["SearchServiceDnsSuffix"])); string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"]; _searchIndexClient = new SearchIndexClient(searchServiceEndpoint, new AzureKeyCredential(apiKey)); _searchIndexerClient = new SearchIndexerClient(searchServiceEndpoint, new AzureKeyCredential(apiKey)); bool result = RunAsync().GetAwaiter().GetResult(); if (!result && !DebugMode) { Console.WriteLine("Something went wrong. Set 'DebugMode' to true in order to see traces."); } else if (!result) { Console.WriteLine("Something went wrong."); } else { Console.WriteLine("All operations were successful."); } Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
public static async Task CheckIndexerOverallStatusAsync(SearchIndexerClient indexerClient, SearchIndexer indexer) { try { var demoIndexerExecutionInfo = await indexerClient.GetIndexerStatusAsync(indexer.Name); switch (demoIndexerExecutionInfo.Value.Status) { case IndexerStatus.Error: ExitProgram("Indexer has error status. Check the Azure Portal to further understand the error."); break; case IndexerStatus.Running: Console.WriteLine("Indexer is running"); break; case IndexerStatus.Unknown: Console.WriteLine("Indexer status is unknown"); break; default: Console.WriteLine("No indexer information"); break; } } catch (RequestFailedException ex) { Console.WriteLine("Failed to get indexer overall status\n Exception message: {0}\n", ex.Message); } }
static async Task Main(string[] args) { string serviceName = ""; string indexName = ""; string apiKey = ""; if (String.IsNullOrEmpty(apiKey)) { throw new InvalidOperationException("Update with your settings"); } // Create a SearchIndexClient to send create/delete index commands Uri serviceEndpoint = new Uri($"https://{serviceName}.search.windows.net/"); AzureKeyCredential credential = new AzureKeyCredential(apiKey); SearchIndexClient index = new SearchIndexClient(serviceEndpoint, credential); // Create a SearchClient to load and query documents SearchClient queryClient = new SearchClient(serviceEndpoint, indexName, credential); var count = await queryClient.GetDocumentCountAsync(); Console.WriteLine($"Found {count} docs"); SearchIndexerClient indexer = new SearchIndexerClient(serviceEndpoint, credential); (await indexer.GetIndexerNamesAsync()).Value .ToList() .ForEach((item) => Console.WriteLine($"Index: {item}")); // await index.UploadSampleDocumentAsync(indexName, apiKey); // await index.DeleteDocumentAsync(indexName, "todo", apiKey); }
public void RunIndexer() { SearchIndexerClient indexerClient = GetSearchIndexerClient(); indexerClient.RunIndexer(configs.IndexerName); return; }
private static void CheckIndexerStatus(SearchIndexerClient indexerClient, SearchIndexer indexer) { try { string indexerName = "hotels-sql-idxr"; SearchIndexerStatus execInfo = indexerClient.GetIndexerStatus(indexerName); Console.WriteLine("Indexer has run {0} times.", execInfo.ExecutionHistory.Count); Console.WriteLine("Indexer Status: " + execInfo.Status.ToString()); IndexerExecutionResult result = execInfo.LastResult; Console.WriteLine("Latest run"); Console.WriteLine("Run Status: {0}", result.Status.ToString()); Console.WriteLine("Total Documents: {0}, Failed: {1}", result.ItemCount, result.FailedItemCount); TimeSpan elapsed = result.EndTime.Value - result.StartTime.Value; Console.WriteLine("StartTime: {0:T}, EndTime: {1:T}, Elapsed: {2:t}", result.StartTime.Value, result.EndTime.Value, elapsed); string errorMsg = (result.ErrorMessage == null) ? "none" : result.ErrorMessage; Console.WriteLine("ErrorMessage: {0}", errorMsg); Console.WriteLine(" Document Errors: {0}, Warnings: {1}\n", result.Errors.Count, result.Warnings.Count); } catch (Exception e) { // Handle exception } }
public AzurSearchIndexerService( IOptions <AzureBlobOptions> azureBlobOptions, IOptions <SearchIndexerClientOptions> searchIndexerClientOptions ) { _azureBlobOptions = azureBlobOptions; _indexingParameters = new(); _indexingParameters.Configuration.Add("parsingMode", "json"); _searchIndexerClient = new SearchIndexerClient(null, null); _azureBlobIndexer = new SearchIndexer( name: searchIndexerClientOptions.Value.IndexerName, dataSourceName: searchIndexerClientOptions.Value.DataSourceName, targetIndexName: searchIndexerClientOptions.Value.IndexName) { Parameters = _indexingParameters, Schedule = new IndexingSchedule(TimeSpan.FromDays(1)) }; _azureBlobIndexer.FieldMappings.Add( new FieldMapping("Id") { TargetFieldName = "HotelId" }); _searchIndexerDataContainer = new SearchIndexerDataContainer("hotel-rooms"); }
static async Task Main() { string searchServiceUri = configuration["SearchServiceUri"]; string adminApiKey = configuration["SearchServiceAdminApiKey"]; SearchIndexClient indexClient = new SearchIndexClient(new Uri(searchServiceUri), new AzureKeyCredential(adminApiKey)); SearchIndexerClient indexerClient = new SearchIndexerClient(new Uri(searchServiceUri), new AzureKeyCredential(adminApiKey)); string indexName = "book"; // Delete the search index, if exist Console.WriteLine($"Deleting index {indexName} if exist...\n"); await DeleteIndexIfExistsAsync(indexName, indexClient); // Create sesrach index Console.WriteLine("Creating index...\n"); await CreateIndexAsync(indexName, indexClient); // Set up a SQL data source and indexer, and run the indexer to import book metadata from SQL DB Console.WriteLine("Indexing SQL DB meta data...\n"); await CreateAndRunSQLIndexerAsync(indexName, indexerClient); // Set up a Blob Storage data source and indexer, and run the indexer to merge book data Console.WriteLine("Indexing and merging book data from blob storage...\n"); await CreateAndRunBlobIndexerAsync(indexName, indexerClient); Console.WriteLine("Complete. Press any key to end application...\n"); Console.ReadKey(); }
private static SearchIndexerDataSourceConnection CreateOrUpdateDataSource(SearchIndexerClient indexerClient, IConfigurationRoot configuration) { SearchIndexerDataSourceConnection dataSource = new SearchIndexerDataSourceConnection( name: "demodata", type: SearchIndexerDataSourceType.AzureBlob, connectionString: configuration["AzureBlobConnectionString"], container: new SearchIndexerDataContainer("cog-search-demo")) { Description = "Demo files to demonstrate cognitive search capabilities." }; // The data source does not need to be deleted if it was already created // since we are using the CreateOrUpdate method try { indexerClient.CreateOrUpdateDataSourceConnection(dataSource); } catch (Exception ex) { Console.WriteLine("Failed to create or update the data source\n Exception message: {0}\n", ex.Message); ExitProgram("Cannot continue without a data source"); } return(dataSource); }
static async Task Main(string[] args) { string searchServiceUri = configuration["SearchServiceUri"]; string adminApiKey = configuration["SearchServiceAdminApiKey"]; SearchIndexClient indexClient = new SearchIndexClient(new Uri(searchServiceUri), new AzureKeyCredential(adminApiKey)); SearchIndexerClient indexerClient = new SearchIndexerClient(new Uri(searchServiceUri), new AzureKeyCredential(adminApiKey)); string indexName = "hotel-rooms-sample"; // Next, create the search index Console.WriteLine("Deleting index...\n"); await DeleteIndexIfExistsAsync(indexName, indexClient); Console.WriteLine("Creating index...\n"); await CreateIndexAsync(indexName, indexClient); // Set up a CosmosDB data source and indexer, and run the indexer to import hotel data Console.WriteLine("Indexing Cosmos DB hotel data...\n"); await CreateAndRunCosmosDbIndexerAsync(indexName, indexerClient); // Set up a Blob Storage data source and indexer, and run the indexer to merge hotel room data Console.WriteLine("Indexing and merging hotel room data from blob storage...\n"); await CreateAndRunBlobIndexerAsync(indexName, indexerClient); Console.WriteLine("Complete. Press any key to end application...\n"); Console.ReadKey(); }
public SearchManager(IServiceProvider serviceProvider, ILocalization localization) { _tableStorage = serviceProvider.GetService <IAzureTableStorage>(); _globalSearchTermRepository = serviceProvider.GetService <GlobalSearchTermRepository>(); _localization = localization; _searchIndexClient = serviceProvider.GetService <SearchIndexClient>(); _searchIndexerClient = serviceProvider.GetService <SearchIndexerClient>(); }
private static async Task CreateAndRunCosmosDbIndexerAsync(string indexName, SearchIndexerClient indexerClient) { // Append the database name to the connection string string cosmosConnectString = configuration["CosmosDBConnectionString"] + ";Database=" + configuration["CosmosDBDatabaseName"]; SearchIndexerDataSourceConnection cosmosDbDataSource = new SearchIndexerDataSourceConnection( name: configuration["CosmosDBDatabaseName"], type: SearchIndexerDataSourceType.CosmosDb, connectionString: cosmosConnectString, container: new SearchIndexerDataContainer("hotels")); // The Cosmos DB data source does not need to be deleted if it already exists, // but the connection string might need to be updated if it has changed. await indexerClient.CreateOrUpdateDataSourceConnectionAsync(cosmosDbDataSource); Console.WriteLine("Creating Cosmos DB indexer...\n"); SearchIndexer cosmosDbIndexer = new SearchIndexer( name: "hotel-rooms-cosmos-indexer", dataSourceName: cosmosDbDataSource.Name, targetIndexName: indexName) { Schedule = new IndexingSchedule(TimeSpan.FromDays(1)) }; // Indexers keep metadata about how much they have already indexed. // If we already ran this sample, the indexer will remember that it already // indexed the sample data and not run again. // To avoid this, reset the indexer if it exists. try { await indexerClient.GetIndexerAsync(cosmosDbIndexer.Name); //Rest the indexer if it exsits. await indexerClient.ResetIndexerAsync(cosmosDbIndexer.Name); } catch (RequestFailedException ex) when(ex.Status == 404) { //if the specified indexer not exist, 404 will be thrown. } await indexerClient.CreateOrUpdateIndexerAsync(cosmosDbIndexer); Console.WriteLine("Running Cosmos DB indexer...\n"); try { await indexerClient.RunIndexerAsync(cosmosDbIndexer.Name); } catch (RequestFailedException ex) when(ex.Status == 429) { Console.WriteLine("Failed to run indexer: {0}", ex.Message); } }
/// <summary> /// Initiates a run of the search indexer. /// </summary> public async Task RunIndexer() { SearchIndexerClient _searchIndexerClient = new SearchIndexerClient(new Uri($"https://{searchServiceName}.search.windows.net/"), new AzureKeyCredential(apiKey)); var indexStatus = await _searchIndexerClient.GetIndexerStatusAsync(IndexerName); if (indexStatus.Value.LastResult.Status != IndexerExecutionStatus.InProgress) { _searchIndexerClient.RunIndexer(IndexerName); } }
public CognitiveSearchClient(string endpoint, string apiKey) { _restClient = new CognitiveSearchRestClient(endpoint, apiKey); Uri serviceEndpoint = new Uri(endpoint); AzureKeyCredential credential = new AzureKeyCredential(apiKey); _searchIndexClient = new SearchIndexClient(serviceEndpoint, credential); _searchIndexerClient = new SearchIndexerClient(serviceEndpoint, credential); }
public static async Task DeleteDataSourceIfExistsAsync(string name, SearchIndexerClient idxclient) { Console.WriteLine($"Deleting search indexer datasource {name}..."); await idxclient.GetDataSourceConnectionNamesAsync(); { await idxclient.DeleteDataSourceConnectionAsync(name); } }
public static async Task DeleteSkillSetIfExistsAsync(string name, SearchIndexerClient idxclient) { Console.WriteLine($"Deleting search indexer datasource {name}..."); await idxclient.GetSkillsetNamesAsync(); { await idxclient.DeleteSkillsetAsync(name); } }
public static async Task DeleteIndexerIfExistsAsync(string name, SearchIndexerClient idxclient) { Console.WriteLine($"Deleting search indexer {name}..."); await idxclient.GetIndexerNamesAsync(); { await idxclient.DeleteIndexerAsync(name); } }
/// <summary> /// Initializes a new instance of the <see cref="AzureSearch" /> class. /// </summary> /// <param name="endpoint">Azure search Endpoint.</param> /// <param name="key">Service Key.</param> /// <param name="config">Configuration.</param> public AzureSearch(Uri endpoint, string key, IConfiguration config = null) { this.config = config; var credential = new AzureKeyCredential(key); client = new SearchIndexClient(endpoint, credential); indexer = new SearchIndexerClient(endpoint, credential); queryBuilder = new ODataQuery(this) { FilterMaker = new AzureSearchFilterMaker(), }; }
private static async Task CreateAndRunSQLIndexerAsync(string indexName, SearchIndexerClient indexerClient) { SearchIndexerDataSourceConnection sqlDataSource = new SearchIndexerDataSourceConnection( name: configuration["SQLDatabaseName"], type: SearchIndexerDataSourceType.AzureSql, connectionString: configuration["SQLConnectSctring"], container: new SearchIndexerDataContainer("books")); // The data source does not need to be deleted if it already exists, // but the connection string might need to be updated if it has changed. await indexerClient.CreateOrUpdateDataSourceConnectionAsync(sqlDataSource); Console.WriteLine("Creating SQL indexer...\n"); SearchIndexer sqlIndexer = new SearchIndexer( name: "books-indexer", dataSourceName: sqlDataSource.Name, targetIndexName: indexName) { //here you can set the desired schedule for indexing repetitions Schedule = new IndexingSchedule(TimeSpan.FromDays(1)) }; // Indexers keep metadata about how much they have already indexed. // If we already ran this sample, the indexer will remember that it already // indexed the sample data and not run again. // To avoid this, reset the indexer if it exists. try { await indexerClient.GetIndexerAsync(sqlIndexer.Name); //Rest the indexer if it exsits. await indexerClient.ResetIndexerAsync(sqlIndexer.Name); } catch (RequestFailedException ex) when(ex.Status == 404) { //if the specified indexer not exist, 404 will be thrown. } await indexerClient.CreateOrUpdateIndexerAsync(sqlIndexer); Console.WriteLine("Running SQL indexer...\n"); try { await indexerClient.RunIndexerAsync(sqlIndexer.Name); } catch (RequestFailedException ex) when(ex.Status == 429) { Console.WriteLine("Failed to run sql indexer: {0}", ex.Message); } }
public static void Main(string[] args) { // Create service client IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); IConfigurationRoot configuration = builder.Build(); string searchServiceUri = configuration["SearchServiceUri"]; string adminApiKey = configuration["SearchServiceAdminApiKey"]; string cognitiveServicesKey = configuration["CognitiveServicesKey"]; SearchIndexClient indexClient = new SearchIndexClient(new Uri(searchServiceUri), new AzureKeyCredential(adminApiKey)); SearchIndexerClient indexerClient = new SearchIndexerClient(new Uri(searchServiceUri), new AzureKeyCredential(adminApiKey)); // Create or Update the data source Console.WriteLine("Creating or updating the data source..."); SearchIndexerDataSourceConnection dataSource = CreateOrUpdateDataSource(indexerClient, configuration); // Create the skills Console.WriteLine("Creating the skills..."); OcrSkill ocrSkill = CreateOcrSkill(); MergeSkill mergeSkill = CreateMergeSkill(); EntityRecognitionSkill entityRecognitionSkill = CreateEntityRecognitionSkill(); LanguageDetectionSkill languageDetectionSkill = CreateLanguageDetectionSkill(); SplitSkill splitSkill = CreateSplitSkill(); KeyPhraseExtractionSkill keyPhraseExtractionSkill = CreateKeyPhraseExtractionSkill(); // Create the skillset Console.WriteLine("Creating or updating the skillset..."); List <SearchIndexerSkill> skills = new List <SearchIndexerSkill>(); skills.Add(ocrSkill); skills.Add(mergeSkill); skills.Add(languageDetectionSkill); skills.Add(splitSkill); skills.Add(entityRecognitionSkill); skills.Add(keyPhraseExtractionSkill); SearchIndexerSkillset skillset = CreateOrUpdateDemoSkillSet(indexerClient, skills, cognitiveServicesKey); // Create the index Console.WriteLine("Creating the index..."); SearchIndex demoIndex = CreateDemoIndex(indexClient); // Create the indexer, map fields, and execute transformations Console.WriteLine("Creating the indexer and executing the pipeline..."); SearchIndexer demoIndexer = CreateDemoIndexer(indexerClient, dataSource, skillset, demoIndex); // Check indexer overall status Console.WriteLine("Check the indexer overall status..."); CheckIndexerOverallStatus(indexerClient, demoIndexer); }
public void Constructor() { var serviceName = "my-svc-name"; var endpoint = new Uri($"https://{serviceName}.search.windows.net"); var service = new SearchIndexerClient(endpoint, new AzureKeyCredential("fake")); Assert.NotNull(service); Assert.AreEqual(endpoint, service.Endpoint); Assert.AreEqual(serviceName, service.ServiceName); Assert.Throws <ArgumentNullException>(() => new SearchIndexerClient(null, new AzureKeyCredential("fake"))); Assert.Throws <ArgumentNullException>(() => new SearchIndexerClient(endpoint, null)); Assert.Throws <ArgumentException>(() => new SearchIndexerClient(new Uri("http://bing.com"), null)); }
private static void CleanupSearchIndexerClientResources(SearchIndexerClient indexerClient, SearchIndexer indexer) { try { if (indexerClient.GetIndexer(indexer.Name) != null) { indexerClient.ResetIndexer(indexer.Name); } } catch (RequestFailedException e) when(e.Status == 404) { //if exception occurred and status is "Not Found", this is working as expected Console.WriteLine("Failed to find indexer and this is because it doesn't exist."); } }
public AiSearchIndexService(SearchIndexClient indexClient, SearchIndexerClient searchIndexerClient, string storageConnectionString, string containerName, string indexName, string indexerName, string cognitiveServicesKey) { _indexClient = indexClient; _indexerClient = searchIndexerClient; _storageConnectionString = storageConnectionString; _containerName = containerName; _indexName = indexName; _indexerName = indexerName; _cognitiveServicesKey = cognitiveServicesKey; }
private static async Task <string> CheckIndexerOverallStatusAsync(SearchIndexerClient indexerClient, string indexerName) { try { var demoIndexerExecutionInfo = await indexerClient.GetIndexerStatusAsync(indexerName); if (demoIndexerExecutionInfo.Value?.Status != null) { return(demoIndexerExecutionInfo.Value.Status.ToString()); } return(null); } catch (RequestFailedException ex) { throw new Exception("Failed to get indexer overall status", ex); } }
static async Task PollSearchIndexer(AppSettings settings) { await Task.Delay(TimeSpan.FromSeconds(5)); SearchIndexerClient indexerClient = new SearchIndexerClient(settings.SearchEndpointUri, settings.SearchKeyCredential); while (true) { SearchIndexerStatus status = await indexerClient.GetIndexerStatusAsync(SEARCH_ACL_INDEXER_NAME); if (status.LastResult != null && status.LastResult.Status != IndexerExecutionStatus.InProgress) { Console.WriteLine("Completed indexing sample data"); break; } Console.WriteLine("Indexing has not finished. Waiting 5 seconds and polling again..."); await Task.Delay(TimeSpan.FromSeconds(5)); } }
private SearchIndexerSkillset CreateOrUpdateDemoSkillSet(SearchIndexerClient indexerClient, IList <SearchIndexerSkill> skills, string cognitiveServicesKey) { SearchIndexerSkillset skillset = new SearchIndexerSkillset("demoskillset", skills) { Description = "Demo skillset", CognitiveServicesAccount = new CognitiveServicesAccountKey(cognitiveServicesKey) }; // Create the skillset in your search service. // The skillset does not need to be deleted if it was already created // since we are using the CreateOrUpdate method try { indexerClient.CreateOrUpdateSkillset(skillset); } catch (RequestFailedException ex) { throw new Exception("Failed to create the skillset", ex); } return(skillset); }
private static SearchIndexerSkillset CreateOrUpdateDemoSkillSet(SearchIndexerClient indexerClient, IList <SearchIndexerSkill> skills, string cognitiveServicesKey) { SearchIndexerSkillset skillset = new SearchIndexerSkillset("demoskillset", skills) { Description = "Demo skillset", CognitiveServicesAccount = new CognitiveServicesAccountKey(cognitiveServicesKey) }; // Create the skillset in your search service. // The skillset does not need to be deleted if it was already created // since we are using the CreateOrUpdate method try { indexerClient.CreateOrUpdateSkillset(skillset); } catch (RequestFailedException ex) { Console.WriteLine("Failed to create the skillset\n Exception message: {0}\n", ex.Message); ExitProgram("Cannot continue without a skillset"); } return(skillset); }
private SearchIndexerDataSourceConnection CreateOrUpdateDataSource(SearchIndexerClient indexerClient) { SearchIndexerDataSourceConnection dataSource = new SearchIndexerDataSourceConnection( name: "demodata", type: SearchIndexerDataSourceType.AzureBlob, connectionString: _storageConnectionString, container: new SearchIndexerDataContainer(_containerName)) { Description = "Demo files to demonstrate cognitive search capabilities." }; // The data source does not need to be deleted if it was already created // since we are using the CreateOrUpdate method try { indexerClient.CreateOrUpdateDataSourceConnection(dataSource); } catch (Exception ex) { throw new Exception("Failed to create or update the data source", ex); } return(dataSource); }
private static SearchIndexerDataSourceConnection CreateOrUpdateDataSource(SearchIndexerClient indexerClient, IConfigurationRoot configuration) { SearchIndexerDataSourceConnection dataSource = new SearchIndexerDataSourceConnection( name: "demodata", type: SearchIndexerDataSourceType.AzureBlob, connectionString: configuration["AzureBlobConnectionString"], container: new SearchIndexerDataContainer(configuration["ContainerName"])) { Description = "Demo files to demonstrate cognitive search capabilities." }; try { indexerClient.CreateOrUpdateDataSourceConnection(dataSource); Console.WriteLine("Data source successfully created"); } catch (Exception ex) { Console.WriteLine("Failed to create or update the data source\n Exception message: {0}\n", ex.Message); ExitProgram("Cannot continue without a data source"); } return(dataSource); }
private static async Task CreateAndRunBlobIndexerAsync(string indexName, SearchIndexerClient indexerClient) { SearchIndexerDataSourceConnection blobDataSource = new SearchIndexerDataSourceConnection( name: configuration["BlobStorageAccountName"], type: SearchIndexerDataSourceType.AzureBlob, connectionString: configuration["BlobStorageConnectionString"], container: new SearchIndexerDataContainer("gapzap-pdf-docs")); // The blob data source does not need to be deleted if it already exists, // but the connection string might need to be updated if it has changed. await indexerClient.CreateOrUpdateDataSourceConnectionAsync(blobDataSource); Console.WriteLine("Creating Blob Storage indexer...\n"); // Add a field mapping to match the Id field in the documents to // the HotelId key field in the index List <FieldMapping> map = new List <FieldMapping> { new FieldMapping("Id") { TargetFieldName = "HotelId" } }; IndexingParameters parameters = new IndexingParameters(); parameters.Configuration.Add("parsingMode", "json"); SearchIndexer blobIndexer = new SearchIndexer( name: "hotel-rooms-blob-indexer", dataSourceName: blobDataSource.Name, targetIndexName: indexName) { Parameters = parameters, Schedule = new IndexingSchedule(TimeSpan.FromDays(1)) }; blobIndexer.FieldMappings.Add(new FieldMapping("Id") { TargetFieldName = "HotelId" }); // Reset the indexer if it already exists try { await indexerClient.GetIndexerAsync(blobIndexer.Name); //Rest the indexer if it exsits. await indexerClient.ResetIndexerAsync(blobIndexer.Name); } catch (RequestFailedException ex) when(ex.Status == 404) { } await indexerClient.CreateOrUpdateIndexerAsync(blobIndexer); Console.WriteLine("Running Blob Storage indexer...\n"); try { await indexerClient.RunIndexerAsync(blobIndexer.Name); } catch (RequestFailedException ex) when(ex.Status == 429) { Console.WriteLine("Failed to run indexer: {0}", ex.Message); } }