/// <summary> /// Initializes a new instance of the Indexer class. /// </summary> /// <param name="name">Gets or sets the name of the indexer.</param> /// <param name="dataSourceName">Gets or sets the name of the /// datasource from which this indexer reads data.</param> /// <param name="targetIndexName">Gets or sets the name of the index to /// which this indexer writes data.</param> /// <param name="description">Gets or sets the description of the /// indexer.</param> /// <param name="schedule">Gets or sets the schedule for this /// indexer.</param> /// <param name="parameters">Gets or sets parameters for indexer /// execution.</param> public Indexer(string name, string dataSourceName, string targetIndexName, string description = default(string), IndexingSchedule schedule = default(IndexingSchedule), IndexingParameters parameters = default(IndexingParameters)) { Name = name; Description = description; DataSourceName = dataSourceName; TargetIndexName = targetIndexName; Schedule = schedule; Parameters = parameters; CustomInit(); }
private void AssertSchedulesEqual(IndexingSchedule expected, IndexingSchedule actual) { if (expected == null) { Assert.Null(actual); } else { Assert.NotNull(actual); Assert.Equal(expected.Interval, actual.Interval); Assert.Equal(expected.StartTime, actual.StartTime); } }
public static async Task Main(string[] args) { IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); IConfigurationRoot configuration = builder.Build(); if (configuration["SearchServiceEndPoint"] == "Put your search service endpoint here") { Console.Error.WriteLine("Specify SearchServiceEndPoint in appsettings.json"); Environment.Exit(-1); } if (configuration["SearchServiceAdminApiKey"] == "Put your search service admin API key here") { Console.Error.WriteLine("Specify SearchServiceAdminApiKey in appsettings.json"); Environment.Exit(-1); } if (configuration["AzureSQLConnectionString"] == "Put your Azure SQL database connection string here") { Console.Error.WriteLine("Specify AzureSQLConnectionString in appsettings.json"); Environment.Exit(-1); } SearchIndexClient indexClient = new SearchIndexClient(new Uri(configuration["SearchServiceEndPoint"]), new AzureKeyCredential(configuration["SearchServiceAdminApiKey"])); SearchIndexerClient indexerClient = new SearchIndexerClient(new Uri(configuration["SearchServiceEndPoint"]), new AzureKeyCredential(configuration["SearchServiceAdminApiKey"])); Console.WriteLine("Creating index..."); FieldBuilder fieldBuilder = new FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(Hotel)); var searchIndex = new SearchIndex("hotels-sql-idx", searchFields); // If we have run the sample before, this index will be populated // We can clear the index by deleting it if it exists and creating // it again CleanupSearchIndexClientResources(indexClient, searchIndex); indexClient.CreateOrUpdateIndex(searchIndex); Console.WriteLine("Creating data source..."); // The sample data set has a table name of "hotels" // The sample data set table has a "soft delete" column named IsDeleted // When this column is set to true and the indexer sees it, it will remove the // corresponding document from the search service // See this link for more information // https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.search.models.softdeletecolumndeletiondetectionpolicy // The sample data set uses SQL integrated change tracking for change detection // This means that when the indexer runs, it will be able to detect which data has // changed since the last run using built in change tracking // See this link for more information // https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/about-change-tracking-sql-server var dataSource = new SearchIndexerDataSourceConnection( "hotels-sql-ds", SearchIndexerDataSourceType.AzureSql, configuration["AzureSQLConnectionString"], new SearchIndexerDataContainer("hotels")); // The data source does not need to be deleted if it was already created, // but the connection string may need to be updated if it was changed indexerClient.CreateOrUpdateDataSourceConnection(dataSource); Console.WriteLine("Creating Azure SQL indexer..."); var schedule = new IndexingSchedule(TimeSpan.FromDays(1)) { StartTime = DateTimeOffset.Now }; var parameters = new IndexingParameters() { BatchSize = 100, MaxFailedItems = 0, MaxFailedItemsPerBatch = 0 }; // Indexer declarations require a data source and search index. // Common optional properties include a schedule, parameters, and field mappings // The field mappings below are redundant due to how the Hotel class is defined, but // we included them anyway to show the syntax var indexer = new SearchIndexer("hotels-sql-idxr", dataSource.Name, searchIndex.Name) { Description = "Data indexer", Schedule = schedule, Parameters = parameters, FieldMappings = { new FieldMapping("_id") { TargetFieldName = "HotelId" }, new FieldMapping("Amenities") { TargetFieldName = "Tags" } } }; // Indexers contain metadata about how much they have already indexed // If we already ran the 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 CleanupSearchIndexerClientResources(indexerClient, indexer); await indexerClient.CreateOrUpdateIndexerAsync(indexer); // We created the indexer with a schedule, but we also // want to run it immediately Console.WriteLine("Running Azure SQL indexer..."); try { await indexerClient.RunIndexerAsync(indexer.Name); } catch (CloudException e) when(e.Response.StatusCode == (HttpStatusCode)429) { Console.WriteLine("Failed to run indexer: {0}", e.Response.Content); } // Wait 5 seconds for indexing to complete before checking status Console.WriteLine("Waiting for indexing...\n"); System.Threading.Thread.Sleep(5000); // After an indexer run, you can retrieve status. CheckIndexerStatus(indexerClient, indexer); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Environment.Exit(0); }
// This method requires index and datasource private async Task CreateIndexerAsync() { var indexSchedule = new IndexingSchedule(TimeSpan.FromMinutes(30), DateTimeOffset.Now); var indexer = new Indexer(INDEXER_NAME, DATASOURCE_NAME, INDEX_NAME, "hourly scheduled", indexSchedule); await _client.Indexers.CreateOrUpdateWithHttpMessagesAsync(indexer); }
public void CreateIndex <T>() { string azureSQLConnectionStr = ConfigurationManager.AppSettings["AzureSqlConnectionString"].ToString(); Uri Uri = new Uri(ConfigurationManager.AppSettings["SearchServiceEndPoint"]); string searchServiceKey = ConfigurationManager.AppSettings["SearchServiceAdminApiKey"]; AzureKeyCredential keyCredential = new AzureKeyCredential(searchServiceKey); SearchIndexClient indexClient = new SearchIndexClient(Uri, keyCredential); SearchIndexerClient indexerClient = new SearchIndexerClient(Uri, keyCredential); Console.WriteLine("Creating index..."); _logger.LogInformation("Creating index for Orion Customer"); FieldBuilder fieldBuilder = new FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(T)); var searchIndex = new SearchIndex(GetIndexName(HdsConstants.Orion, typeof(T).ToString()), searchFields); CleanupSearchIndexClientResources(indexClient, searchIndex); indexClient.CreateOrUpdateIndex(searchIndex); Console.WriteLine("Creating data source..."); _logger.LogInformation("Creating data source for Orion Customer"); var dataSource = new SearchIndexerDataSourceConnection( GetDataSourceName(HdsConstants.Orion, typeof(T).ToString()), SearchIndexerDataSourceType.AzureSql, azureSQLConnectionStr, new SearchIndexerDataContainer($"[{GetTableName(typeof(T).ToString())}]")); indexerClient.CreateOrUpdateDataSourceConnection(dataSource); //Creating indexer Console.WriteLine("Creating Azure SQL indexer..."); _logger.LogInformation("Creating Azure SQL indexer for Orion Customer"); var schedule = new IndexingSchedule(TimeSpan.FromDays(1)) { StartTime = DateTimeOffset.Now }; var parameters = new IndexingParameters() { BatchSize = 100, MaxFailedItems = 0, MaxFailedItemsPerBatch = 0 }; var indexer = new SearchIndexer(GetIndexerName(HdsConstants.Orion, typeof(T).ToString()), dataSource.Name, searchIndex.Name) { Description = "Data indexer", Schedule = schedule, Parameters = parameters, }; indexerClient.CreateOrUpdateIndexerAsync(indexer); Console.WriteLine("Running Azure SQL indexer..."); try { indexerClient.RunIndexerAsync(indexer.Name); } catch (CloudException e) when(e.Response.StatusCode == (HttpStatusCode)429) { Console.WriteLine("Failed to run indexer: {0}", e.Response.Content); _logger.LogError("Failed to run indexer: {0}", e.Response.Content); } }