private static Index CreateDemoIndex(SearchServiceClient serviceClient) { var index = new Index() { Name = "demoindex", Fields = FieldBuilder.BuildForType <DemoIndex>() }; try { bool exists = serviceClient.Indexes.Exists(index.Name); if (exists) { serviceClient.Indexes.Delete(index.Name); } serviceClient.Indexes.Create(index); } catch (Exception e) { Console.WriteLine("Failed to create the index\n Exception message: {0}\n", e.Message); ExitProgram("Cannot continue without an index"); } return(index); }
private static void AssertAnalysisComponentsEqual(Index expected, Index actual) { // Compare analysis components directly so that test failures show better comparisons. Assert.Equal(expected.Analyzers?.Count ?? 0, actual.Analyzers?.Count ?? 0); for (int i = 0; i < expected.Analyzers?.Count; i++) { Assert.Equal(expected.Analyzers[i], actual.Analyzers[i], new DataPlaneModelComparer <Analyzer>()); } Assert.Equal(expected.Tokenizers?.Count ?? 0, actual.Tokenizers?.Count ?? 0); for (int i = 0; i < expected.Tokenizers?.Count; i++) { Assert.Equal(expected.Tokenizers[i], actual.Tokenizers[i], new DataPlaneModelComparer <Tokenizer>()); } Assert.Equal(expected.TokenFilters?.Count ?? 0, actual.TokenFilters?.Count ?? 0); for (int i = 0; i < expected.TokenFilters?.Count; i++) { Assert.Equal(expected.TokenFilters[i], actual.TokenFilters[i], new DataPlaneModelComparer <TokenFilter>()); } Assert.Equal(expected.CharFilters?.Count ?? 0, actual.CharFilters?.Count ?? 0); for (int i = 0; i < expected.CharFilters?.Count; i++) { Assert.Equal(expected.CharFilters[i], actual.CharFilters[i], new DataPlaneModelComparer <CharFilter>()); } }
private async Task CreateIndexAsync(Index index) { _logger.LogInformation("Creating index {IndexName}.", index.Name); await _serviceClient.Indexes.CreateAsync(index); _logger.LogInformation("Done creating index {IndexName}.", index.Name); }
public void CanChangeIndexAfterConstructionWithoutServiceName() { Run(() => { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = serviceClient.Indexes.Get(Data.IndexName); // Make sure first index works. SearchIndexClient indexClient = Data.GetSearchIndexClient(); indexClient.Documents.Count(); // Delete the first index so we know we're not accidentally using it below. serviceClient.Indexes.Delete(Data.IndexName); // Create a second index. string newIndexName = index.Name + "2"; index.Name = newIndexName; serviceClient.Indexes.Create(index); // Target the second index and make sure it works too. indexClient.IndexName = newIndexName; indexClient.Documents.Count(); }); }
public async Task <IActionResult> RemoveCustomIndex(string key) { string searchServiceName = _configuration["SearchServiceName"]; string adminApiKey = _configuration["SearchServiceAdminApiKey"]; SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey)); // Create custom index var definition = new Microsoft.Azure.Search.Models.Index() { Name = "tomcustomindex", Fields = FieldBuilder.BuildForType <TomTestModel>() }; //create Index if (!serviceClient.Indexes.Exists(definition.Name)) { serviceClient.Indexes.Create(definition); } //var index = serviceClient.Indexes.Create(definition); if (!string.IsNullOrEmpty(key)) { var keys = new List <string>(); keys.Add(key); var batch = IndexBatch.Delete("fileId", keys); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("tomcustomindex"); indexClient.Documents.Index(batch); return(Ok("remove succeed")); } return(StatusCode(StatusCodes.Status500InternalServerError)); }
public void CanGetStaticallyTypedDocumentWithPascalCaseFields() { Run(() => { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = Book.DefineIndex(); serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); var expectedDoc = new Book() { ISBN = "123", Title = "Lord of the Rings", Author = new Author() { FirstName = "J.R.R.", LastName = "Tolkien" } }; var batch = IndexBatch.Upload(new[] { expectedDoc }); indexClient.Documents.Index(batch); Book actualDoc = indexClient.Documents.Get <Book>("123"); Assert.Equal(expectedDoc, actualDoc); }); }
private static void CreateIndex(SearchServiceClient searchClient) { Console.WriteLine("\nCreating index..."); // Define the index with a name and fields based on the MargiesIndex class var index = new Index() { Name = IndexName, Fields = FieldBuilder.BuildForType <MargiesIndex>() }; // Create the index try { bool exists = searchClient.Indexes.Exists(index.Name); // Delete and recreate the index if it already exists if (exists) { searchClient.Indexes.Delete(index.Name); } searchClient.Indexes.Create(index); } catch (Exception e) { Console.WriteLine("Failed to create the index\n Exception message: {0}\n", e.Message); } }
protected void TestCanSuggestWithDateTimeInStaticModel() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = Book.DefineIndex(); serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); var tolkien = new Author() { FirstName = "J.R.R.", LastName = "Tolkien" }; var doc1 = new Book() { ISBN = "123", Title = "Lord of the Rings", Author = tolkien }; var doc2 = new Book() { ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18) }; var batch = IndexBatch.Upload(new[] { doc1, doc2 }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); var parameters = new SuggestParameters() { Select = new[] { "ISBN", "Title", "PublishDate" } }; DocumentSuggestResult <Book> response = indexClient.Documents.Suggest <Book>("War", "sg", parameters); Assert.Equal(1, response.Results.Count); Assert.Equal(doc2, response.Results[0].Document); }
private static Index CreateAzureIndexDefinition(string indexName) { var index = new Index { Name = indexName }; index.Fields = new List <Field> { new Field("DocumentKey", DataType.String) { IsKey = true }, new Field("From", DataType.String) { IsSearchable = true, IsFacetable = true }, new Field("To", DataType.String) { IsSearchable = true, IsFacetable = true }, new Field("Value", DataType.String), new Field("BlockNumber", DataType.String), new Field("TxHash", DataType.String), new Field("LogAddress", DataType.String), new Field("LogIndex", DataType.Int64) }; return(index); }
public IndexerFixture() { SearchServiceClient searchClient = this.GetSearchServiceClient(); TargetIndexName = TestUtilities.GenerateName(); DataSourceName = TestUtilities.GenerateName(); var index = new Index( TargetIndexName, new[] { new Field("feature_id", DataType.String) { IsKey = true }, }); AzureOperationResponse createResponse = searchClient.Indexes.Create(index); Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode); var dataSource = new DataSource( DataSourceName, DataSourceType.AzureSql, new DataSourceCredentials(AzureSqlReadOnlyConnectionString), new DataContainer("GeoNamesRI")); createResponse = searchClient.DataSources.Create(dataSource); Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode); }
private static void CreateIndex() { // Create the Azure Search index based on the included schema try { var definition = new Index() { Name = GeoNamesIndex, Fields = new[] { new Field("FEATURE_ID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("FEATURE_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("FEATURE_CLASS", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("STATE_ALPHA", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("STATE_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("COUNTY_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("COUNTY_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("ELEV_IN_M", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("ELEV_IN_FT", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("MAP_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("DESCRIPTION", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("HISTORY", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("DATE_CREATED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("DATE_EDITED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true} } }; _searchClient.Indexes.Create(definition); } catch (Exception ex) { Console.WriteLine("Error creating index: {0}\r\n", ex.Message); } }
public static async Task ForceCreateIndexAsync(ISearchServiceClient serviceClient, string indexName) { if (await serviceClient.Indexes.ExistsAsync(indexName)) { Console.WriteLine($"Index '{indexName}' exists. Deleting the existing index..."); await serviceClient.Indexes.DeleteAsync(indexName); Console.WriteLine($"Index '{indexName}' was deleted."); } Console.WriteLine($"Creating index '{indexName}'..."); var definition = new Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Document>(), ScoringProfiles = new List <ScoringProfile>() { Document.CreatePrimaryFieldFavoredScoringProfile() }, }; await serviceClient.Indexes.CreateAsync(definition); Console.WriteLine($"Index '{indexName}' was created."); }
protected virtual Index CreateIndexDefinition(string indexName, IList <Field> providerFields) { var minGram = GetMinGram(); var maxGram = GetMaxGram(); var index = new Index { Name = indexName, Fields = providerFields.OrderBy(f => f.Name).ToArray(), TokenFilters = new TokenFilter[] { new NGramTokenFilterV2(NGramFilterName, minGram, maxGram), new EdgeNGramTokenFilterV2(EdgeNGramFilterName, minGram, maxGram) }, Analyzers = new Analyzer[] { new CustomAnalyzer { Name = ContentAnalyzerName, Tokenizer = TokenizerName.Standard, TokenFilters = new[] { TokenFilterName.Lowercase, GetTokenFilterName() } } }, }; return(index); }
public void DeleteIndexIsIdempotent() { Run(() => { SearchServiceClient searchClient = Data.GetSearchServiceClient(); Index index = CreateTestIndex(); // Try delete before the index even exists. AzureOperationResponse deleteResponse = searchClient.Indexes.DeleteWithHttpMessagesAsync(index.Name).Result; Assert.Equal(HttpStatusCode.NotFound, deleteResponse.Response.StatusCode); AzureOperationResponse <Index> createResponse = searchClient.Indexes.CreateWithHttpMessagesAsync(index).Result; Assert.Equal(HttpStatusCode.Created, createResponse.Response.StatusCode); // Now delete twice. deleteResponse = searchClient.Indexes.DeleteWithHttpMessagesAsync(index.Name).Result; Assert.Equal(HttpStatusCode.NoContent, deleteResponse.Response.StatusCode); deleteResponse = searchClient.Indexes.DeleteWithHttpMessagesAsync(index.Name).Result; Assert.Equal(HttpStatusCode.NotFound, deleteResponse.Response.StatusCode); }); }
public void CanCreateAndListIndexes() { Run(() => { SearchServiceClient searchClient = Data.GetSearchServiceClient(); Index index1 = CreateTestIndex(); Index index2 = CreateTestIndex(); searchClient.Indexes.Create(index1); searchClient.Indexes.Create(index2); IndexListResult listResponse = searchClient.Indexes.List(); Assert.Equal(2, listResponse.Indexes.Count); IList <string> indexNames = listResponse.Indexes.Select(i => i.Name).ToList(); Assert.Contains(index1.Name, indexNames); Assert.Contains(index2.Name, indexNames); indexNames = searchClient.Indexes.ListNames(); Assert.Equal(2, indexNames.Count); Assert.Contains(index1.Name, indexNames); Assert.Contains(index2.Name, indexNames); }); }
private static void AddSynonyms(SearchServiceClient searchClient) { Console.WriteLine("\nCreating synonym map..."); try { // Create a synonym map var synonymMap = new SynonymMap() { Name = SynonymMapName, Synonyms = "SA,United States,America,United States of America\nUK,GB,United Kingdom,Great Britain,Britain\nUAE,United Arab Emirates,Emirates\n" }; searchClient.SynonymMaps.CreateOrUpdate(synonymMap); // Get the index Index index = searchClient.Indexes.Get(IndexName); // Apply the synonym map to the content field for (int i = 0; i < index.Fields.Count; i++) { if (index.Fields[i].Name == "content") { index.Fields[i].SynonymMaps = new[] { SynonymMapName }; } } searchClient.Indexes.CreateOrUpdate(index); } catch (Exception e) { Console.WriteLine("Failed to add synonyms\n Exception message: {0}\n", e.Message); } }
public void CanUpdateIndexDefinition() { Run(() => { SearchServiceClient searchClient = Data.GetSearchServiceClient(); Index fullFeaturedIndex = CreateTestIndex(); Index initialIndex = CreateTestIndex(); // Start out with no scoring profiles and different CORS options. initialIndex.Name = fullFeaturedIndex.Name; initialIndex.ScoringProfiles = new ScoringProfile[0]; initialIndex.DefaultScoringProfile = null; initialIndex.CorsOptions.AllowedOrigins = new[] { "*" }; Index index = searchClient.Indexes.Create(initialIndex); // Give the index time to stabilize before continuing the test. // TODO: Remove this workaround once the retry hang bug is fixed. TestUtilities.Wait(TimeSpan.FromSeconds(20)); // Now update the index. index.ScoringProfiles = fullFeaturedIndex.ScoringProfiles; index.DefaultScoringProfile = fullFeaturedIndex.DefaultScoringProfile; index.CorsOptions.AllowedOrigins = fullFeaturedIndex.CorsOptions.AllowedOrigins; Index updatedIndex = searchClient.Indexes.CreateOrUpdate(index); AssertIndexesEqual(fullFeaturedIndex, updatedIndex); }); }
public void CanRoundtripIndexerWithFieldMappingFunctions() => Run(() => { Indexer expectedIndexer = new Indexer(SearchTestUtilities.GenerateName(), Data.DataSourceName, Data.TargetIndexName) { FieldMappings = new[] { // Try all the field mapping functions and parameters (even if they don't make sense in the context of the test DB). new FieldMapping("feature_id", "a", FieldMappingFunction.Base64Encode()), new FieldMapping("feature_id", "b", FieldMappingFunction.Base64Encode(useHttpServerUtilityUrlTokenEncode: true)), new FieldMapping("feature_id", "c", FieldMappingFunction.ExtractTokenAtPosition(delimiter: " ", position: 0)), new FieldMapping("feature_id", "d", FieldMappingFunction.Base64Decode()), new FieldMapping("feature_id", "e", FieldMappingFunction.Base64Decode(useHttpServerUtilityUrlTokenDecode: false)), new FieldMapping("feature_id", "f", FieldMappingFunction.JsonArrayToStringCollection()), new FieldMapping("feature_id", "g", FieldMappingFunction.UrlEncode()), new FieldMapping("feature_id", "h", FieldMappingFunction.UrlDecode()), } }; SearchServiceClient searchClient = Data.GetSearchServiceClient(); // We need to add desired fields to the index before those fields can be referenced by the field mappings Index index = searchClient.Indexes.Get(Data.TargetIndexName); string[] fieldNames = new[] { "a", "b", "c", "d", "e", "f", "g", "h" }; index.Fields = index.Fields.Concat(fieldNames.Select(name => new Field(name, DataType.String))).ToList(); searchClient.Indexes.CreateOrUpdate(index); searchClient.Indexers.Create(expectedIndexer); Indexer actualIndexer = searchClient.Indexers.Get(expectedIndexer.Name); AssertIndexersEqual(expectedIndexer, actualIndexer); });
public async Task <Microsoft.Azure.Search.Models.Index> GetOrCreateIndex(Microsoft.Azure.Search.Models.Index index) { if (await SearchServiceClient.Indexes.ExistsAsync(index.Name)) { return(await SearchServiceClient.Indexes.GetAsync(index.Name)); } return(await SearchServiceClient.Indexes.CreateOrUpdateAsync(index)); }
/// <summary> /// Creates a new Azure Search index. (see /// https://msdn.microsoft.com/library/azure/dn798941.aspx for more /// information) /// </summary> /// <param name='operations'> /// Reference to the Microsoft.Azure.Search.IIndexOperations. /// </param> /// <param name='index'> /// Required. The definition of the index to create. /// </param> /// <returns> /// Response from a Create, Update, or Get Index request. If /// successful, it includes the full definition of the index that was /// created, updated, or retrieved. /// </returns> public static IndexDefinitionResponse Create(this IIndexOperations operations, Index index) { return Task.Factory.StartNew((object s) => { return ((IIndexOperations)s).CreateAsync(index); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult(); }
private static void CreateIndexs(string indexName, SearchServiceClient searchServiceClient) { var definition = new Microsoft.Azure.Search.Models.Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Hotel>() }; var res = searchServiceClient.Indexes.Create(definition); }
public void CanSearchWithCustomAnalyzer() { Run(() => { const string CustomAnalyzerName = "my_email_analyzer"; const string CustomCharFilterName = "my_email_filter"; Index index = new Index() { Name = SearchTestUtilities.GenerateName(), Fields = new[] { new Field("id", DataType.String) { IsKey = true }, new Field("message", (AnalyzerName)CustomAnalyzerName) { IsSearchable = true } }, Analyzers = new[] { new CustomAnalyzer() { Name = CustomAnalyzerName, Tokenizer = TokenizerName.Standard, CharFilters = new[] { (CharFilterName)CustomCharFilterName } } }, CharFilters = new[] { new PatternReplaceCharFilter(CustomCharFilterName, "@", "_") } }; Data.GetSearchServiceClient().Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); var documents = new[] { new Document() { { "id", "1" }, { "message", "My email is [email protected]." } }, new Document() { { "id", "2" }, { "message", "His email is [email protected]." } }, }; indexClient.Documents.Index(IndexBatch.Upload(documents)); SearchTestUtilities.WaitForIndexing(); DocumentSearchResult <Document> result = indexClient.Documents.Search("*****@*****.**"); Assert.Equal("1", result.Results.Single().Document["id"]); }); }
static async Task Main(string[] args) { IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile(@"C:\Users\shmadhav\source\repos\SearchCacheSample\AzureSearch\appsettings.json"); IConfigurationRoot configuration = builder.Build(); string serviceName = configuration["SearchServiceName"]; string searchServiceUri = configuration["SearchServiceUri"]; string indexName = "books"; string apiKey = configuration["SearchServiceAdminApiKey"]; string queryKey = configuration["SearchServiceQueryKey"]; string sqlConnectionString = configuration["AzureSQLConnectionString"]; SearchServiceClient searchService = new SearchServiceClient(serviceName, new SearchCredentials(apiKey)); Microsoft.Azure.Search.Models.Index index = new Microsoft.Azure.Search.Models.Index(indexName, FieldBuilder.BuildForType <Book>()); bool exists = await searchService.Indexes.ExistsAsync(index.Name); if (exists) { await searchService.Indexes.DeleteAsync(index.Name); } await searchService.Indexes.CreateAsync(index); DataSource dataSource = DataSource.AzureSql("books-index", sqlConnectionString, "Book", new SoftDeleteColumnDeletionDetectionPolicy("IsDeleted", "true")); dataSource.DataChangeDetectionPolicy = new SqlIntegratedChangeTrackingPolicy(); await searchService.DataSources.CreateOrUpdateAsync(dataSource); Indexer indexer = new Indexer(name: "books-indexer", dataSource.Name, index.Name, null, null, new IndexingSchedule(TimeSpan.FromDays(1))); exists = await searchService.Indexers.ExistsAsync(indexer.Name); if (exists) { await searchService.Indexers.ResetAsync(indexer.Name); } await searchService.Indexers.CreateOrUpdateAsync(indexer); try { // await searchService.Indexers.RunAsync(indexer.Name); } catch (CloudException e) when(e.Response.StatusCode == (HttpStatusCode)429) { Console.WriteLine("Failed to run indexer: {0}", e.Response.Content); } SearchClient searchclient = new SearchClient(new Uri(searchServiceUri), indexName, new Azure.AzureKeyCredential(queryKey)); RunQueries(searchclient); }
private static void CreateIndex(SearchServiceClient serviceClient) { var definition = new Microsoft.Azure.Search.Models.Index { Name = indexName, Fields = FieldBuilder.BuildForType <SearchBook>() }; serviceClient.Indexes.Create(definition); }
private static async Task CreateIndexForDoctor(string indexName, SearchServiceClient searchService) { var definition = new Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Doctor>() }; await searchService.Indexes.CreateAsync(definition); }
private void CreateIndex(string indexName, SearchServiceClient serviceClient) { var definition = new Microsoft.Azure.Search.Models.Index() { Name = indexName, Fields = FieldBuilder.BuildForType <SearchCustomer>() }; serviceClient.Indexes.Create(definition); }
static void CreateIndex(string indexName, SearchServiceClient serviceClient) { Microsoft.Azure.Search.Models.Index index = new Microsoft.Azure.Search.Models.Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Customer>() }; serviceClient.Indexes.Create(index); }
private static void CreateHotelsIndex(SearchServiceClient serviceClient) { var definition = new Index() { Name = "hotels", Fields = FieldBuilder.BuildForType<Hotel>() }; serviceClient.Indexes.Create(definition); }
public void CreateIndex <T>() { var definition = new Index() { Name = indexName, Fields = FieldBuilder.BuildForType <T>() }; searchServiceClient.Indexes.Create(definition); }
public IndexFixture() { SearchServiceClient searchClient = this.GetSearchServiceClient(); IndexName = TestUtilities.GenerateName(); // This is intentionally a different index definition than the one returned by IndexTests.CreateTestIndex(). // That index is meant to exercise serialization of the index definition itself, while this one is tuned // more for exercising document serialization, indexing, and querying operations. var index = new Index() { Name = IndexName, Fields = new[] { new Field("hotelId", DataType.String) { IsKey = true, IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("baseRate", DataType.Double) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("description", DataType.String) { IsSearchable = true }, new Field("descriptionFr", AnalyzerName.FrLucene), new Field("hotelName", DataType.String) { IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("category", DataType.String) { IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("tags", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true }, new Field("parkingIncluded", DataType.Boolean) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("smokingAllowed", DataType.Boolean) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("lastRenovationDate", DataType.DateTimeOffset) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("rating", DataType.Int32) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("location", DataType.GeographyPoint) { IsFilterable = true, IsSortable = true } }, Suggesters = new[] { new Suggester( name: "sg", searchMode: SuggesterSearchMode.AnalyzingInfixMatching, sourceFields: new[] { "description", "hotelName" }) }, ScoringProfiles = new[] { new ScoringProfile("nearest") { FunctionAggregation = ScoringFunctionAggregation.Sum, Functions = new[] { new DistanceScoringFunction(new DistanceScoringParameters("myloc", 100), "location", 2) } } } }; IndexDefinitionResponse createIndexResponse = searchClient.Indexes.Create(index); Assert.Equal(HttpStatusCode.Created, createIndexResponse.StatusCode); // Give the index time to stabilize before running tests. // TODO: Remove this workaround once the retry hang bug is fixed. TestUtilities.Wait(TimeSpan.FromSeconds(20)); }
public static async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { // Upload data to the sql database try { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder { DataSource = dataSource, UserID = userID, Password = password, InitialCatalog = initialCatalog }; await UploadDataToSqlDbAsync(builder); } catch (SqlException e) { log.Error("There was an error uploading the data to the sql database", e); } //Upload data to the DocumentDb try { client = new DocumentClient(new Uri(endpoint), authKey); await CreateDatabaseIfNotExistsAsync(); await CreateCollectionIfNotExistsAsync(); await CreateItemAsync(); } catch (Exception e) { log.Error("There was an error uploading the data to the documentdb", e); } // Create index try { SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey)); var definition = new Microsoft.Azure.Search.Models.Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Person>(), Suggesters = new List <Suggester>() { new Suggester("sg", SuggesterSearchMode.AnalyzingInfixMatching, "FirstName", "LastName") } }; await serviceClient.Indexes.CreateOrUpdateAsync(definition); } catch (Exception e) { log.Error("There was an error creating the index", e); } }
public void ExistsReturnsTrueForExistingIndex() { Run(() => { SearchServiceClient client = Data.GetSearchServiceClient(); Index index = CreateTestIndex(); client.Indexes.Create(index); Assert.True(client.Indexes.Exists(index.Name)); }); }
private async Task CreateIndexIfNotExistsAsync(Index index) { if (!(await _serviceClient.Indexes.ExistsAsync(index.Name))) { await CreateIndexAsync(index); } else { _logger.LogInformation("Skipping the creation of index {IndexName} since it already exists.", index.Name); } }
static async Task CreateTargetIndexAsync(Microsoft.Azure.Search.Models.Index indexSchema) { try { await TargetSearchClient.Indexes.CreateOrUpdateAsync(indexSchema); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message.ToString()); } }
private static void CreateProductIndex(string indexName, ISearchServiceClient serviceClient) { var definition = new Microsoft.Azure.Search.Models.Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Product>(), ScoringProfiles = GetScoringProfile() }; serviceClient.Indexes.Create(definition); }
public void BuildAzureIndexSchema(AzureField keyField, AzureField idField) { if (!this.AzureSchemaBuilt) { try { //this.AzureIndexFields = this.AzureIndexFields.Where(f => f.Name != keyField.Name).ToList(); AddAzureIndexField(keyField); AddAzureIndexField(idField); var indexName = index.Name; var fields = AzureIndexFields .GroupBy(f => f.Name) .Select(f => f.First().Field).ToList(); var definition = new Index() { Name = indexName, Fields = fields }; var boostFields = AzureIndexFields.Where(f => f.Boost > 0 && f.Field.IsSearchable); if (boostFields.Any()) { var scoringProfile = new ScoringProfile(); scoringProfile.Name = index.AzureConfiguration.AzureDefaultScoringProfileName; scoringProfile.TextWeights = new TextWeights(new Dictionary<string, double>()); foreach (var boostField in boostFields) { if (!scoringProfile.TextWeights.Weights.Any(w => w.Key == boostField.Name)) { scoringProfile.TextWeights.Weights.Add(boostField.Name, boostField.Boost); } } if (scoringProfile.TextWeights.Weights.Any()) { definition.ScoringProfiles = new List<ScoringProfile>(); definition.ScoringProfiles.Add(scoringProfile); definition.DefaultScoringProfile = index.AzureConfiguration.AzureDefaultScoringProfileName; } } AzureIndex = index.AzureServiceClient.Indexes.Create(definition); this.AzureSchemaBuilt = true; } catch (Exception ex) { CrawlingLog.Log.Fatal("Error creating index" + index.Name, ex); } } }
public async override Task ExecuteAsync() { var searchServiceName = "beerdrinkin"; string apiKey; if (!Services.Settings.TryGetValue("SEARCH_PRIMARY_ADMIN_KEY", out apiKey)) { Services.Log.Error("Failed to find Search Service admin key"); } var serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); var indexClient = serviceClient.Indexes.GetClient("beers"); Services.Log.Info("Started updating Beer Drinkin's search engine index!"); var definition = new Index() { Name = "beers", Fields = new[] { new Field("id", DataType.String) { IsKey = true }, new Field("name", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("description", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("brewery", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("abv", DataType.String) { IsFilterable = true, IsSortable = true } } }; await serviceClient.Indexes.CreateOrUpdateAsync(definition); try { var context = new BeerDrinkinContext(); var beerItems = context.Beers; if (beerItems.Count() != 0) { var beers = beerItems.ToArray(); await indexClient.Documents.IndexAsync(IndexBatch.Create(beers.Select(IndexAction.Create))); } else Services.Log.Info("Failed to find any beers in the DB to index"); } catch (IndexBatchException e) { // Sometimes when your Search service is under load, indexing will fail for some of the documents in // the batch. Depending on your application, you can take compensating actions like delaying and // retrying. For this simple demo, we just log the failed document keys and continue. Services.Log.Error( $"Failed to index some of the documents: {string.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))}"); } Services.Log.Info("Finished updating Beer Drinkin's search engine index!"); }
/// <summary> /// Set up the index at the Azure endpoint. /// </summary> public async Task CreateBlogPostsIndex() { if (await serviceClient.Indexes.ExistsAsync(Config.AzureSearchIndexName) == false) { var blogPostIndex = new Index { Name = Config.AzureSearchIndexName, Fields = IndexFields().ToList() }; await serviceClient.Indexes.CreateAsync(blogPostIndex); } }
private static void CreateIndex(SearchServiceClient serviceClient) { var definition = new Index() { Name = "test", Fields = new[] { new Field("id", DataType.String) { IsKey = true }, new Field("title", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("author", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("createdDate", DataType.DateTimeOffset) { IsFilterable = true, IsSortable = true, IsFacetable = true }, } }; serviceClient.Indexes.Create(definition); }
private static void CreateHotelsIndex(SearchServiceClient serviceClient) { var definition = new Index() { Name = "hotels", Fields = new[] { new Field("hotelId", DataType.String) { IsKey = true }, new Field("hotelName", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("baseRate", DataType.Double) { IsFilterable = true, IsSortable = true }, new Field("category", DataType.String) { IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("tags", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true }, new Field("parkingIncluded", DataType.Boolean) { IsFilterable = true, IsFacetable = true }, new Field("lastRenovationDate", DataType.DateTimeOffset) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("rating", DataType.Int32) { IsFilterable = true, IsSortable = true, IsFacetable = true }, new Field("location", DataType.GeographyPoint) { IsFilterable = true, IsSortable = true } } }; serviceClient.Indexes.Create(definition); }
public static void CreateIndex(SearchServiceClient serviceClient, string indexName) { if (serviceClient.Indexes.Exists(indexName)) { serviceClient.Indexes.Delete(indexName); } var definition = new Index() { Name = indexName, Fields = new[] { new Field("fileId", DataType.String) { IsKey = true }, new Field("fileName", DataType.String) { IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false }, new Field("ocrText", DataType.String) { IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false } } }; serviceClient.Indexes.Create(definition); }
private static void CreateIndex() { // Create the Azure Search index based on the included schema try { var definition = new Index() { Name = searchIndexName, Fields = new[] { new Field("CustomerAddressID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false,IsRetrievable = true}, new Field("Title", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("FirstName", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("MiddleName", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("LastName", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("Suffix", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false,IsRetrievable = true}, new Field("CompanyName", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("SalesPerson", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("EmailAddress", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false,IsRetrievable = true}, new Field("Phone", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false,IsRetrievable = true}, new Field("AddressType", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("AddressLine1", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false,IsRetrievable = true}, new Field("AddressLine2", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false,IsRetrievable = true}, new Field("City", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("StateProvince", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("CountryRegion", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("PostalCode", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false,IsRetrievable = true}, new Field("ProductNames", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true }, } }; _searchClient.Indexes.Create(definition); } catch (Exception ex) { Console.WriteLine("Error creating index: {0}\r\n", ex.Message.ToString()); } }
private static void CreateRetsPropertiesIndex(SearchServiceClient serviceClient) { var definition = new Microsoft.Azure.Search.Models.Index() { Name = RETS_PROPERTY_INDEX_NAME, Fields = new[] { //new Field("tags", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true }, new Field("ListingID", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = false }, new Field("ListingKey", Microsoft.Azure.Search.Models.DataType.String) { IsKey = true }, new Field("LotSizeArea", Microsoft.Azure.Search.Models.DataType.Int64) { IsFilterable = true, IsSortable = false}, new Field("LotAreaAcre", Microsoft.Azure.Search.Models.DataType.Double) { IsSearchable = false, IsFilterable = false }, new Field("PricePerSqFt", Microsoft.Azure.Search.Models.DataType.Double) { IsSearchable = false, IsFilterable = false, IsSortable=true }, new Field("GrossSQFT", Microsoft.Azure.Search.Models.DataType.Double) { IsSearchable = false, IsFilterable = false, IsSortable=true }, //new Field("LisAltAgentFirstName", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true }, // TotalTaxes string //new Field("TotalTaxes", Microsoft.Azure.Search.Models.DataType.Double) { IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = true }, //new Field("ApproximateAge", DataType.Int32) { IsFilterable = false, IsFacetable = false }, new Field("FullStreetAddress", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, new Field("BuilderTractName", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true }, new Field("City", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, //new Field("ListAgentAgentID", DataType.String) { IsFilterable = false, IsSortable = false }, //new Field("LisListAgentLastName", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true }, //new Field("Community", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, //new Field("ComplexName", DataType.String) { IsFilterable = false, IsSortable = true }, //new Field("Countrycode", DataType.String) { IsFilterable = false, IsSortable = true }, new Field("County", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, new Field("GeoLocation", Microsoft.Azure.Search.Models.DataType.GeographyPoint) { IsFilterable = true, IsSortable = true }, new Field("RandomNumber", Microsoft.Azure.Search.Models.DataType.Int32) { IsSortable = true }, new Field("ListPrice", Microsoft.Azure.Search.Models.DataType.Double) { IsFilterable = true, IsSortable = true }, new Field("ModificationTimestamp", Microsoft.Azure.Search.Models.DataType.DateTimeOffset) { IsFilterable = true, IsSortable = false }, new Field("PropertyType", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true }, new Field("PropertySubTypes", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true, IsSearchable = true }, //new Field("ModelCode", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true }, //new Field("ListAgentAgentID", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = false }, //new Field("LisSaleAgentLastName", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, //new Field("ListOfficeOfficeID", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = false }, //new Field("LisSaleOfficeFullOfficeName", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, //new Field("LisSaleOfficeOfficePhoneDisplay", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = false }, new Field("State", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, //new Field("StreetDirection", DataType.String) { IsFilterable = false, IsSortable = true }, new Field("StreetNumber", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true }, new Field("StreetName", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = true, IsSearchable = true }, //new Field("StreetType", DataType.String) { IsFilterable = false, IsSortable = true }, new Field("ThumbnailUrl", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = false, IsSortable = false }, new Field("Baths", Microsoft.Azure.Search.Models.DataType.Double) { IsFilterable = true, IsSortable = true }, new Field("BathsFull", Microsoft.Azure.Search.Models.DataType.Int32) { IsFilterable = true, IsSortable = true }, //new Field("Fireplaces", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true}, new Field("Beds", Microsoft.Azure.Search.Models.DataType.Int32) { IsFilterable = true, IsSortable = true }, //new Field("Stories", Microsoft.Azure.Search.Models.DataType.Int32) { IsFilterable = true, IsSortable = false }, //new Field("TotalPartialBaths", DataType.Int32) { IsFilterable = true, IsSortable = false }, new Field("BathsHalf", Microsoft.Azure.Search.Models.DataType.Int32) { IsFilterable = true, IsSortable = true }, new Field("SearchLocality", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true, IsSearchable = true }, new Field("PoolFlag", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true }, new Field("ListingStatus", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true, IsSearchable = false }, new Field("LocaleListingStatus", Microsoft.Azure.Search.Models.DataType.String) { IsFilterable = true, IsSortable = true, IsSearchable = false }, } }; serviceClient.Indexes.Create(definition); }
private void BuildAzureIndex() { try { if (this.Configuration.DocumentOptions.IndexAllFields) { } var definition = new Index() { Name = this.Name, Fields = new[] { new Field("FEATURE_ID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("FEATURE_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("FEATURE_CLASS", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("STATE_ALPHA", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("STATE_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("COUNTY_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("COUNTY_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("ELEV_IN_M", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("ELEV_IN_FT", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("MAP_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("DESCRIPTION", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("HISTORY", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("DATE_CREATED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("DATE_EDITED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true} } }; _searchClient.Indexes.CreateOrUpdate(definition); } catch (Exception ex) { Console.WriteLine("Error creating index: {0}\r\n", ex.Message.ToString()); } }
private static void LoadPackagesIntoAzureSearch(IEnumerable<Package> packagesToLoad) { _searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; _apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"]; _SearchClient = new SearchServiceClient(_searchServiceName, new SearchCredentials(_apiKey)); _IndexClient = new SearchIndexClient(_searchServiceName, "packages", new SearchCredentials(_apiKey)); Suggester sg = new Suggester(); sg.Name = "sg"; sg.SearchMode = SuggesterSearchMode.AnalyzingInfixMatching; sg.SourceFields = new List<string> { "NuGetIdRaw", "NuGetIdCollection" }; var indexDefinition = new Index() { Name = "packages", Suggesters = new List<Suggester> { sg }, Fields = new[] { new Field("NuGetIdRaw", DataType.String) { IsKey=false, IsSearchable=true, IsSortable=true, IsRetrievable=true }, new Field("NuGetIdCollection", DataType.Collection(DataType.String)) { IsKey=false, IsSearchable=true, IsSortable=false, IsRetrievable=true, IsFacetable=true, IsFilterable=true }, new Field("NuGetId", DataType.String) { IsKey=true, IsSearchable=false, IsSortable=false, IsRetrievable=true } } }; // NOTE: ALready exists _SearchClient.Indexes.Delete("packages"); _SearchClient.Indexes.Create(indexDefinition); // Populate try { for (var i = 0; i < packagesToLoad.Count(); i += 1000) { _IndexClient.Documents.Index( IndexBatch.Create( packagesToLoad.Skip(i).Take(1000).Select( doc => IndexAction.Create(new { NuGetId = Guid.NewGuid(), NuGetIdRaw = doc.NuGetId, NuGetIdCollection = doc.NuGetId.Split('.') }) ) ) ); } } catch (IndexBatchException e) { // Sometimes when your Search service is under load, indexing will fail for some of the documents in // the batch. Depending on your application, you can take compensating actions like delaying and // retrying. For this simple demo, we just log the failed document keys and continue. Console.WriteLine( "Failed to index some of the documents: {0}", String.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))); } Console.Out.WriteLine("Completed"); }
private static void CreateIndex() { // Create the Azure Search index based on the included schema try { // Create the suggester for suggestions Suggester sg = new Suggester(); sg.Name = "sg"; sg.SearchMode = SuggesterSearchMode.AnalyzingInfixMatching; sg.SourceFields = new List<string>() { "FEATURE_NAME", "COUNTY_NAME" }; var definition = new Index() { Name = "geonames", Fields = new[] { new Field("FEATURE_ID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("FEATURE_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("FEATURE_CLASS", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("STATE_ALPHA", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("STATE_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("COUNTY_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("COUNTY_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("ELEV_IN_M", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("ELEV_IN_FT", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("MAP_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("DESCRIPTION", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("HISTORY", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("LOCATION", DataType.GeographyPoint) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("DATE_CREATED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("DATE_EDITED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true} }, Suggesters = new List<Suggester> { sg } }; _searchClient.Indexes.Create(definition); } catch (Exception ex) { Console.WriteLine("Error creating index: {0}\r\n", ex.Message.ToString()); } }
private async Task EnsureTotalsIndex() { bool exists = await ServiceClient.Indexes.ExistsAsync(Constants.TotalsIndexName); if (!exists) { var definition = new Index() { Name = Constants.TotalsIndexName, Fields = new[] { new Field("docUniqueID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("clientInternalID", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("customerNumber", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("mvaNumber", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("productID", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("date", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("amount", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true }, } }; await ServiceClient.Indexes.CreateAsync(definition); } }
private Index GetIndex() { var index = new Index(_indexname, new List<Field> { new Field("id", DataType.String) {IsKey = true}, new Field("name", DataType.String) {IsSearchable = true}, new Field("party", DataType.String) {IsSearchable = true}, new Field("rawTerms", DataType.String), new Field("termStart", DataType.Int32) {IsFilterable = true}, new Field("termEnd", DataType.Int32) {IsFilterable = true}, }); return index; }
protected void TestCanSuggestWithDateTimeInStaticModel() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = new Index() { Name = TestUtilities.GenerateName(), Fields = new[] { new Field("ISBN", DataType.String) { IsKey = true }, new Field("Title", DataType.String) { IsSearchable = true }, new Field("Author", DataType.String), new Field("PublishDate", DataType.DateTimeOffset) }, Suggesters = new[] { new Suggester("sg", SuggesterSearchMode.AnalyzingInfixMatching, "Title") } }; IndexDefinitionResponse createIndexResponse = serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name); var doc1 = new Book() { ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien" }; var doc2 = new Book() { ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18) }; var batch = IndexBatch.Create(IndexAction.Create(doc1), IndexAction.Create(doc2)); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); var parameters = new SuggestParameters() { Select = new[] { "ISBN", "Title", "PublishDate" } }; DocumentSuggestResponse<Book> response = indexClient.Documents.Suggest<Book>("War", "sg", parameters); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(1, response.Results.Count); Assert.Equal(doc2, response.Results[0].Document); }
private static void CreateIndex(string indexName, string suggesterName) { // Create the Azure Search index based on the included schema try { // Create the suggester for suggestions Suggester sg = new Suggester(); sg.Name = suggesterName; sg.SearchMode = SuggesterSearchMode.AnalyzingInfixMatching; sg.SourceFields = new List<string>() { "name", "county" }; var definition = new Index() { Name = indexName, Fields = new[] { new Field("id", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("name", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("county", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("elevation", DataType.Int64) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("location", DataType.GeographyPoint) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, }, Suggesters = new List<Suggester> { sg } }; _searchClient.Indexes.Create(definition); } catch (Exception ex) { Console.WriteLine("Error creating index: {0}\r\n", ex.Message.ToString()); } }
/// <summary> /// Creates a new Azure Search index. (see /// https://msdn.microsoft.com/library/azure/dn798941.aspx for more /// information) /// </summary> /// <param name='operations'> /// Reference to the Microsoft.Azure.Search.IIndexOperations. /// </param> /// <param name='index'> /// Required. The definition of the index to create. /// </param> /// <returns> /// Response from a Create, Update, or Get Index request. If /// successful, it includes the full definition of the index that was /// created, updated, or retrieved. /// </returns> public static Task<IndexDefinitionResponse> CreateAsync(this IIndexOperations operations, Index index) { return operations.CreateAsync(index, CancellationToken.None); }
public static void CreateIndex() { SearchClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchApiKey)); IndexClient = SearchClient.Indexes.GetClient(IndexName); try { if (DeleteIndex(SearchClient, IndexName)) { // Create index schema for this dataset Console.WriteLine("{0}", "Creating index...\n"); var definition = new Index() { Name = IndexName, Fields = new[] { new Field("id", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("title", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("imdbID", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("spanishTitle", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("imdbPictureURL", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("year", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("rtID", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("rtAllCriticsRating",DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("recommendations",DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true } }, Suggesters = new[] { new Suggester("sg", SuggesterSearchMode.AnalyzingInfixMatching, new string[] { "title" }) }, CorsOptions = new CorsOptions(new string[] { "*" }) // This * option should only be enabled for demo purposes or when you fully trust your users }; SearchClient.Indexes.Create(definition); // first apply the changes and if we succeed then record the new version that // we'll use as starting point next time Console.WriteLine("{0}", "Uploading Content...\n"); ApplyData(@"data\movies.dat"); } } catch (Exception ex) { Console.WriteLine("Error: {0}:\r\n", ex.Message.ToString()); } }
private static void CreateIndex() { try { Suggester sg = new Suggester(); sg.Name = "sg"; sg.SearchMode = SuggesterSearchMode.AnalyzingInfixMatching; sg.SourceFields = new string[] { "brand_name", "product_name", "sku", "product_subcategory", "product_category", "product_department", "product_family" }; var definition = new Index() { Name = IndexName, Fields = new[] { new Field("product_id", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("product_class_id", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = true, IsFacetable = false, IsRetrievable = true }, new Field("brand_name", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("product_name", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("sku", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("srp", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("gross_weight", DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("net_weight", DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("recyclable_package", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("low_fat", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("units_per_case", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("cases_per_pallet", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("shelf_width", DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("shelf_height", DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("shelf_depth", DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("product_subcategory",DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("product_category", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("product_department", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("product_family", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = true, IsRetrievable = true }, new Field("url", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("recommendations", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true } } }; definition.Suggesters.Add(sg); _searchClient.Indexes.Create(definition); } catch (Exception ex) { Console.WriteLine("Error creating index: {0}:\r\n", ex.Message.ToString()); } }
public bool ReconcileAzureIndexSchema(Document document, int retryCount = 0) { try { var fieldCount = AzureIndex.Fields.Count; if (document != null) { //Look for fields that are different from the standards: foreach (var key in document.Keys) { if (!AzureIndexFields.Any(f => f.Name == key)) { object objVal; document.TryGetValue(key, out objVal); var field = AzureFieldBuilder.BuildField(key, objVal, index); var azureField = new AzureField(key, objVal, field); AddAzureIndexField(azureField); } } } if (AzureIndexFields.Count > fieldCount) { var indexName = index.Name; var fields = AzureIndexFields .GroupBy(f => f.Name) .Select(f => f.First().Field).ToList(); AzureIndex.Fields = fields; AzureIndex = index.AzureServiceClient.Indexes.CreateOrUpdate(AzureIndex); } return true; } catch (Exception ex) { if (!ReconcileAzureIndexSchema(null)) { Thread.Sleep(50); if (retryCount < 6) { return ReconcileAzureIndexSchema(null, retryCount++); } else { CrawlingLog.Log.Warn("Error updating index" + index.Name); } } return false; } }
public string CreateIndex() { // Create the Azure Search index based on the included schema try { //Create the index definition var definition = new Index() { Name = _indexName, Fields = new[] { new Field ( "DocumentID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field ( "NodeAliasPath", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field ( "QuoteAuthor", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field ( "QuoteText", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field ( "QuoteDate", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true} } }; definition.Suggesters = new List<Suggester> { new Suggester() { Name = "quoteauthor", SearchMode = SuggesterSearchMode.AnalyzingInfixMatching, SourceFields = new List<string> { "QuoteAuthor" } } }; List<ScoringProfile> lstScoringProfiles = new List<ScoringProfile>(); TextWeights twauthor = new TextWeights(); twauthor.Weights.Add("QuoteAuthor", 100); TextWeights twtext = new TextWeights(); twtext.Weights.Add("QuoteText", 100); lstScoringProfiles.Add(new ScoringProfile() { Name = "QuoteAuthor", TextWeights = twauthor }); lstScoringProfiles.Add(new ScoringProfile() { Name = "QuoteText", TextWeights = twtext }); definition.ScoringProfiles = lstScoringProfiles; _searchClient.Indexes.Create(definition); return "Index created!"; } catch (Exception ex) { return "There was an issue creating the index: {0}\r\n" + ex.Message.ToString(); } }
private static IndexDefinitionResponse CreateIndex() { // Create the Azure Search index based on the included schema try { var definition = new Index() { Name = IndexName, Fields = new[] { new Field("id", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field("name", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true}, new Field("orgin", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("gender", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field("meaning", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, } }; var response = _searchClient.Indexes.Create(definition); return response; } catch { // So I mean this sucks. I need to figure out what to do with this error. When it happens. return null; } }