private static async Task CreateIndex(string searchServiceEndpoint, ILogger log) { log.LogInformation("init-accelerator: Creating index " + Constants.indexName); try { var idxclient = new SearchIndexClient(new Uri(searchServiceEndpoint), new AzureKeyCredential(GetAppSetting("SearchServiceApiKey"))); SearchIndex index = new SearchIndex(Constants.indexName) { Fields = { new SearchField("content", SearchFieldDataType.String) { IsSearchable = true, IsSortable = false, IsFilterable = false, IsFacetable = false }, new SearchField("metadata_storage_path", SearchFieldDataType.String) { IsSearchable = true, IsSortable = false, IsFilterable = false, IsFacetable = false }, new SearchField("id", SearchFieldDataType.String) { IsKey = true, IsSearchable = true, IsSortable = false, IsFilterable = false,IsFacetable = false }, new SearchField("metadata_storage_name", SearchFieldDataType.String) { IsSearchable = true, IsSortable = false, IsFilterable = false, IsFacetable = false }, new SearchField("status", SearchFieldDataType.String) { IsSearchable = false, IsSortable = false, IsFilterable = false, IsFacetable = false }, new SearchField("fileType", SearchFieldDataType.String) { IsSearchable = true, IsSortable = false, IsFilterable = true, IsFacetable = true }, new SearchField("keyPhrases", SearchFieldDataType.Collection(SearchFieldDataType.String)) { IsSearchable = true, IsSortable = false, IsFilterable = true, IsFacetable = true } } }; var suggester = new SearchSuggester("sg", new[] { "keyPhrases" }); index.Suggesters.Add(suggester); await idxclient.CreateIndexAsync(index); } catch (Exception e) { log.LogError("init-accelerator: Error while creating index " + e.Message); throw new Exception(e.Message); } }
public static async Task Create(SearchIndexClient indexClient, SearchIndexName searchIndexName) { var fieldBuilder = new FieldBuilder(); IList <SearchField>?searchFields = fieldBuilder.Build(typeof(IndexableProduct)); var definition = new SearchIndex(searchIndexName.Value, searchFields); var suggester = new SearchSuggester("sg", new[] { nameof(IndexableProduct.Title), nameof(IndexableProduct.Description) }); definition.Suggesters.Add(suggester); var _ = await indexClient.CreateOrUpdateIndexAsync(definition); }
// Create hotels-quickstart index private static void CreateIndex(string indexName, SearchIndexClient adminClient) { FieldBuilder fieldBuilder = new FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(Hotel)); var definition = new SearchIndex(indexName, searchFields); var suggester = new SearchSuggester("sg", new[] { "HotelName", "Category", "Address/City", "Address/StateProvince" }); definition.Suggesters.Add(suggester); adminClient.CreateOrUpdateIndex(definition); }
public async Task CreateIndexAsync(string indexName) { Azure.Search.Documents.Indexes.FieldBuilder fieldBuilder = new Azure.Search.Documents.Indexes.FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(Hotel)); var definition = new SearchIndex(indexName, searchFields); var suggester = new SearchSuggester("sg", new[] { "HotelName", "Category", "Address/City", "Address/StateProvince" }); definition.Suggesters.Add(suggester); var index = await _indexClient.CreateOrUpdateIndexAsync(definition); }
private Task CreateIndexAsync(string indexName) { var suggester = new SearchSuggester("sg", new[] { "Name", "FacilityName" }); var definition = new SearchIndex(indexName) { Fields = new List <SearchField> { new SearchField("Id", SearchFieldDataType.String) { IsKey = true }, new SearchField("FacilityId", SearchFieldDataType.Int32) { IsFilterable = true }, new SearchField("Name", SearchFieldDataType.String) { IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft }, new SearchField("Description", SearchFieldDataType.String) { IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft }, new SearchField("FacilityName", SearchFieldDataType.String) { IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft }, new SearchField("FacilityDescription", SearchFieldDataType.String) { IsSearchable = true, AnalyzerName = LexicalAnalyzerName.EnMicrosoft }, new SearchField("Location", SearchFieldDataType.GeographyPoint) { IsFilterable = true }, new SearchField("RoomCount", SearchFieldDataType.Int32) { IsFilterable = true }, new SearchField("Images", SearchFieldDataType.Collection(SearchFieldDataType.String)) { IsFilterable = false } } }; definition.Suggesters.Add(suggester); return(client.CreateOrUpdateIndexAsync(definition)); }
public async Task <int> BuildIndexAsync(IEnumerable <SearchLocationIndex> searchLocations) { logger.LogInformation($"Starting to build index for {searchLocations.Count()}"); try { var searchIndexClient = new SearchIndexClient(azureSearchIndexConfig.EndpointUri, GetAzureKeyCredential()); var searchClient = new SearchClient(azureSearchIndexConfig.EndpointUri, azureSearchIndexConfig.LocationSearchIndex, GetAzureKeyCredential()); var fieldBuilder = new FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(SearchLocationIndex)); var definition = new SearchIndex(azureSearchIndexConfig.LocationSearchIndex, searchFields); var suggester = new SearchSuggester(suggestorName, new[] { nameof(SearchLocationIndex.LocationName) }); definition.Suggesters.Add(suggester); logger.LogInformation("created search objects and creating index"); await searchIndexClient.CreateOrUpdateIndexAsync(definition).ConfigureAwait(false); logger.LogInformation("Created search index and uploading documents"); var batch = IndexDocumentsBatch.Upload(searchLocations); IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch).ConfigureAwait(false); var failedRecords = result.Results.Where(r => !r.Succeeded); if (failedRecords.Any()) { var sampleFailedRecord = failedRecords.FirstOrDefault(); var sampleMessage = $"{failedRecords.Count()} have failed to upload to the index, sample failed record message {sampleFailedRecord.ErrorMessage}, Status = {sampleFailedRecord.Status}"; logger.LogError(sampleMessage); throw new DfcIndexUploadException("sampleMessage"); } logger.LogInformation($"Created search index and uploaded {result.Results.Count} documents"); return(result.Results.Count); } catch (Exception ex) { logger.LogError("Building index had an error", ex); throw; } }
public SearchAPI(IOptions <Configuration.SearchOptions> options) { Uri endpointUri = new Uri(options.Value.Endpoint !); Azure.AzureKeyCredential credentials = new Azure.AzureKeyCredential(options.Value.AccessKey !); searchIndexClient = new SearchIndexClient(endpointUri, credentials); searchClient = new SearchClient(endpointUri, IndexName, credentials); FieldBuilder fieldBuilder = new FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(AlbumInfoSearchObject)); var definition = new SearchIndex(IndexName, searchFields); var suggester = new SearchSuggester("AlbumsSuggester", new[] { nameof(AlbumInfoSearchObject.AlbumName), nameof(AlbumInfoSearchObject.ArtistName), nameof(AlbumInfoSearchObject.Colors), nameof(AlbumInfoSearchObject.Tags), nameof(AlbumInfoSearchObject.Captions) }); definition.Suggesters.Add(suggester); searchIndexClient.CreateOrUpdateIndex(definition); }
private void InitializeConcertsIndex(SearchIndexClient serviceClient) { // Create the index that will contain the searchable data from the concerts. var concertsIndex = new SearchIndex(IndexNameConcerts) { Fields = new[] { new SearchField(nameof(Concert.Id), SearchFieldDataType.String) { IsKey = true, IsSearchable = false }, new SearchField(nameof(Concert.Artist), SearchFieldDataType.String) { AnalyzerName = LexicalAnalyzerName.EnMicrosoft, IsSearchable = true, }, new SearchField(nameof(Concert.Genre), SearchFieldDataType.String) { AnalyzerName = LexicalAnalyzerName.EnMicrosoft, IsSearchable = true, IsFilterable = true, IsFacetable = true }, new SearchField(nameof(Concert.Location), SearchFieldDataType.String) { AnalyzerName = LexicalAnalyzerName.EnMicrosoft, IsSearchable = true, IsFilterable = true, IsFacetable = true }, new SearchField(nameof(Concert.Title), SearchFieldDataType.String) { AnalyzerName = LexicalAnalyzerName.EnMicrosoft, IsSearchable = true, }, new SearchField(nameof(Concert.Description), SearchFieldDataType.String) { AnalyzerName = LexicalAnalyzerName.EnMicrosoft, IsSearchable = true, }, new SearchField(nameof(Concert.Price), SearchFieldDataType.Double) { IsSearchable = false, IsFilterable = true, IsFacetable = true, IsSortable = true, }, new SearchField(nameof(Concert.StartTime), SearchFieldDataType.DateTimeOffset) { IsSearchable = false, IsSortable = true, IsFilterable = true }, }, DefaultScoringProfile = "default-scoring", }; var suggester = new SearchSuggester("default-suggester", new[] { nameof(Concert.Artist), nameof(Concert.Location), nameof(Concert.Title) }); concertsIndex.Suggesters.Add(suggester); concertsIndex.ScoringProfiles.Add(new ScoringProfile("default-scoring") { // Add a lot of weight to the artist and above average weight to the title. TextWeights = new TextWeights(new Dictionary <string, double> { { nameof(Concert.Artist), 2.0 }, { nameof(Concert.Title), 1.5 } }) }); serviceClient.CreateOrUpdateIndex(concertsIndex); var searchIndexerClient = new SearchIndexerClient(this.searchServiceUri, this.azureKeyCredential); // Create the data source that connects to the SQL Database account containing the consult requests. var concertsDataSource = new SearchIndexerDataSourceConnection(IndexNameConcerts, SearchIndexerDataSourceType.AzureSql, this.concertsSqlDatabaseConnectionString, new SearchIndexerDataContainer("Concerts")) { DataChangeDetectionPolicy = new SqlIntegratedChangeTrackingPolicy() }; searchIndexerClient.CreateOrUpdateDataSourceConnection(concertsDataSource); // Create the indexer that will pull the data from the database into the search index. var concertsIndexer = new SearchIndexer(name: IndexNameConcerts, dataSourceName: IndexNameConcerts, targetIndexName: IndexNameConcerts) { Schedule = new IndexingSchedule(TimeSpan.FromMinutes(5)) }; searchIndexerClient.CreateOrUpdateIndexer(concertsIndexer); }