public async Task CreateAndQuery() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); string indexName = resources.IndexName; #region Snippet:Azure_Search_Tests_Samples_Readme_Client // Get the service endpoint and API key from the environment Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); //@@ string indexName = "hotels"; // Create a client AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient client = new SearchIndexClient(endpoint, indexName, credential); /*@@*/ client = InstrumentClient(new SearchIndexClient(endpoint, indexName, credential, GetSearchClientOptions())); #endregion Snippet:Azure_Search_Tests_Samples_Readme_Client #region Snippet:Azure_Search_Tests_Samples_Readme_Dict //@@ SearchResults<SearchDocument> response = client.Search("luxury"); /*@@*/ SearchResults <SearchDocument> response = await client.SearchAsync("luxury"); //@@ foreach (SearchResult<SearchDocument> result in response.GetResults()) /*@@*/ await foreach (SearchResult <SearchDocument> result in response.GetResultsAsync()) { SearchDocument doc = result.Document; string id = (string)doc["hotelId"]; string name = (string)doc["hotelName"]; Console.WriteLine("{id}: {name}"); } #endregion Snippet:Azure_Search_Tests_Samples_Readme_Dict #region Snippet:Azure_Search_Tests_Samples_Readme_Dynamic //@@ SearchResults<SearchDocument> response = client.Search("luxury"); /*@@*/ response = await client.SearchAsync("luxury"); //@@ foreach (SearchResult<SearchDocument> result in response.GetResults()) /*@@*/ await foreach (SearchResult <SearchDocument> result in response.GetResultsAsync()) { dynamic doc = result.Document; string id = doc.hotelId; string name = doc.hotelName; Console.WriteLine("{id}: {name}"); } #endregion Snippet:Azure_Search_Tests_Samples_Readme_Dynamic }
public async Task SimpleIndexing() { await using SearchResources resources = SearchResources.CreateWithNoIndexes(this); SearchClient searchClient = null; try { searchClient = await CreateIndexAsync(resources); // Simple #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing1 IEnumerable <Product> products = GenerateCatalog(count: 1000); await searchClient.UploadDocumentsAsync(products); #endregion await WaitForDocumentCountAsync(searchClient, 1000); // Check #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing2 Assert.AreEqual(1000, (int)await searchClient.GetDocumentCountAsync()); #endregion // Too many try { #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing3 IEnumerable <Product> all = GenerateCatalog(count: 100000); await searchClient.UploadDocumentsAsync(all); #endregion Assert.Fail("Expected too many documents failure."); } catch (RequestFailedException ex) { Assert.AreEqual(400, ex.Status); } } finally { if (searchClient != null) { await resources.GetIndexClient().DeleteIndexAsync(searchClient.IndexName); } } }
public async Task Authenticate() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Tests_Samples_Readme_Authenticate // Get the service endpoint and API key from the environment Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); // Create a client AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient client = new SearchIndexClient(endpoint, credential); #endregion Snippet:Azure_Search_Tests_Samples_Readme_Authenticate }
public async Task Troubleshooting() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); #region Snippet:Azure_Search_Tests_Samples_Readme_Troubleshooting try { //@@ return client.GetDocument<Hotel>("12345"); /*@@*/ await client.GetDocumentAsync <Hotel>("12345"); } catch (RequestFailedException ex) when(ex.Status == 404) { Console.WriteLine("We couldn't find the hotel you are looking for!"); Console.WriteLine("Please try selecting another."); } #endregion Snippet:Azure_Search_Tests_Samples_Readme_Troubleshooting }
public async Task Options() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); #region Snippet:Azure_Search_Tests_Samples_Readme_Options int stars = 4; SearchOptions options = new SearchOptions { // Filter to only ratings greater than or equal our preference Filter = SearchFilter.Create($"rating ge {stars}"), Size = 5, // Take only 5 results OrderBy = new[] { "rating desc" } // Sort by rating from high to low }; SearchResults <Hotel> response = client.Search <Hotel>("luxury", options); // ... #endregion Snippet:Azure_Search_Tests_Samples_Readme_Options }
public async Task GetStastistics() { await using SearchResources search = await SearchResources.CreateWithEmptyHotelsIndexAsync(this); SearchServiceClient client = search.GetServiceClient(); Response <SearchServiceStatistics> response = await client.GetStatisticsAsync(); Assert.AreEqual(200, response.GetRawResponse().Status); Assert.IsNotNull(response.Value); Assert.IsNotNull(response.Value.Counters); Assert.IsNotNull(response.Value.Counters.DataSourceCounter); Assert.IsNotNull(response.Value.Counters.DocumentCounter); Assert.IsNotNull(response.Value.Counters.IndexCounter); Assert.IsNotNull(response.Value.Counters.IndexerCounter); Assert.IsNotNull(response.Value.Counters.StorageSizeCounter); Assert.IsNotNull(response.Value.Counters.SynonymMapCounter); Assert.IsNotNull(response.Value.Limits); Assert.NotZero(response.Value.Counters.IndexCounter.Quota ?? 0); Assert.AreEqual(1, response.Value.Counters.IndexCounter.Usage ?? 0); }
public async Task GetStatisticsAsync() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Tests_Samples_GetStatisticsAsync // Create a new SearchServiceClient Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); SearchServiceClient search = new SearchServiceClient(endpoint, credential); /*@@*/ search = InstrumentClient(new SearchServiceClient(endpoint, credential, GetSearchClientOptions())); // Get and report the Search Service statistics Response <SearchServiceStatistics> stats = await search.GetServiceStatisticsAsync(); Console.WriteLine($"You are using {stats.Value.Counters.IndexCounter.Usage} of {stats.Value.Counters.IndexCounter.Quota} indexes."); #endregion Snippet:Azure_Search_Tests_Samples_GetStatisticsAsync }
private void ApplicationStartup(object sender, StartupEventArgs e) { Log.Information("Started Catapult."); if (!Directory.Exists(CatapultPaths.DataPath)) { Directory.CreateDirectory(CatapultPaths.DataPath); } var loader = new JsonConfigLoader(); var configuration = loader.LoadUserConfig(CatapultPaths.ConfigPath); loader.SaveUserConfig(configuration, CatapultPaths.ConfigPath); Task.Factory.StartNew(() => { SearchResources.SetConfig(configuration); SearchResources.GetFiles(); }); _taskbarIcon = (TaskbarIcon)FindResource("MyNotifyIcon"); InitializeTaskBarIcon(_taskbarIcon); _hotKeyManager = new HotKeyManager(); RegisterHotKey(Key.Space, configuration.UseControlKey ? ModifierKeys.Control : ModifierKeys.Alt); _mainWindow = new MainWindow(); _mainWindow.IsVisibleChanged += _mainWindow_IsVisibleChanged; if (Program.UseSingleLaunchMode) { ToggleMainWindow(); } SquirrelIntegration.Instance.StartPeriodicUpdateCheck(); }
public async Task BufferedSender() { await using SearchResources resources = SearchResources.CreateWithNoIndexes(this); SearchClient searchClient = null; try { searchClient = await CreateIndexAsync(resources); // Simple { searchClient = GetOriginal(searchClient); #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender1 await using SearchIndexingBufferedSender <Product> indexer = new SearchIndexingBufferedSender <Product>(searchClient); await indexer.UploadDocumentsAsync(GenerateCatalog(count : 100000)); #endregion } await WaitForDocumentCountAsync(searchClient, 100000); // Check #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender2 #if SNIPPET await indexer.FlushAsync(); #endif Assert.AreEqual(100000, (int)await searchClient.GetDocumentCountAsync()); #endregion } finally { if (searchClient != null) { await resources.GetIndexClient().DeleteIndexAsync(searchClient.IndexName); } } }
public async Task CreateIndex() { await using SearchResources resources = SearchResources.CreateWithNoIndexes(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); // Create a service client AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient client = new SearchIndexClient(endpoint, credential); #if !SNIPPET client = resources.GetIndexClient(); #endif // Create the index using FieldBuilder. #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex_New_SearchIndex #if SNIPPET SearchIndex index = new SearchIndex("hotels") #else SearchIndex index = new SearchIndex(Recording.Random.GetName()) #endif { Fields = new FieldBuilder().Build(typeof(Hotel)), Suggesters = { // Suggest query terms from the hotelName field. new SearchSuggester("sg", "hotelName") } }; #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex_New_SearchIndex client.CreateIndex(index); #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex resources.IndexName = index.Name; }
private async Task <SearchClient> CreateIndexAsync(SearchResources resources) { Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_CreateIndex_Connect Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); // Create a client for manipulating search indexes AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential); #endregion indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_CreateIndex_Create // Create the search index string indexName = "Products"; #if !SNIPPET indexName = Recording.Random.GetName(); #endif await indexClient.CreateIndexAsync( new SearchIndex(indexName) { Fields = new FieldBuilder().Build(typeof(Product)) }); #endregion #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_CreateIndex_Client SearchClient searchClient = indexClient.GetSearchClient(indexName); #endregion searchClient = InstrumentClient(searchClient); return(searchClient); }
public async Task GetCountAsync() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); Environment.SetEnvironmentVariable("SEARCH_INDEX", resources.IndexName); #region Snippet:Azure_Search_Tests_Samples_GetCountAsync // Create a SearchClient Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); string indexName = Environment.GetEnvironmentVariable("SEARCH_INDEX"); SearchClient client = new SearchClient(endpoint, indexName, credential); /*@@*/ client = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); // Get and report the number of documents in the index Response <long> count = await client.GetDocumentCountAsync(); Console.WriteLine($"Search index {indexName} has {count.Value} documents."); #endregion Snippet:Azure_Search_Tests_Samples_GetCountAsync }
public async Task CreateClient() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Tests_Samples_CreateClient // Get the service endpoint and API key from the environment Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); // Create a new SearchServiceClient SearchServiceClient search = new SearchServiceClient(endpoint, credential); /*@@*/ search = InstrumentClient(new SearchServiceClient(endpoint, credential, GetSearchClientOptions())); // Perform an operation Response <SearchServiceStatistics> stats = search.GetServiceStatistics(); Console.WriteLine($"You are using {stats.Value.Counters.IndexCounter.Usage} indexes."); #endregion Snippet:Azure_Search_Tests_Samples_CreateClient Assert.AreEqual(1, stats.Value.Counters.IndexCounter.Usage); }
public void Run() { SearchResources.EnqueueDelayedIndexing(true); }
public async Task CreateDoubleEncryptedIndex() { string keyVaultUrl = TestEnvironment.KeyVaultUrl; if (string.IsNullOrEmpty(keyVaultUrl)) { Assert.Ignore("A Key Vault was not deployed"); } // Create the key and persist the name and version. KeyVaultKey key = await CreateEncryptionKey(keyVaultUrl); Environment.SetEnvironmentVariable("KEYVAULT_URL", keyVaultUrl); Environment.SetEnvironmentVariable("KEYVAULT_KEY_NAME", key.Name); Environment.SetEnvironmentVariable("KEYVAULT_KEY_VERSION", key.Properties.Version); // Persist the service principal. Environment.SetEnvironmentVariable("APPLICATION_ID", TestEnvironment.ClientId); Environment.SetEnvironmentVariable("APPLICATION_SECRET", TestEnvironment.RecordedClientSecret); // Create the blob container and persist connection information. await using SearchResources resources = await SearchResources.CreateWithBlobStorageAndIndexAsync(this, populate : true); Environment.SetEnvironmentVariable("STORAGE_CONNECTION_STRING", resources.StorageAccountConnectionString); Environment.SetEnvironmentVariable("STORAGE_CONTAINER_NAME", resources.BlobContainerName); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); // Define clean up tasks to be invoked in reverse order added. Stack <Func <Task> > cleanUpTasks = new Stack <Func <Task> >(); try { #region Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Index // Create a credential to connect to Key Vault and use a specific key version created previously. SearchResourceEncryptionKey encryptionKey = new SearchResourceEncryptionKey( new Uri(Environment.GetEnvironmentVariable("KEYVAULT_URL")), Environment.GetEnvironmentVariable("KEYVAULT_KEY_NAME"), Environment.GetEnvironmentVariable("KEYVAULT_KEY_VERSION")) { ApplicationId = Environment.GetEnvironmentVariable("APPLICATION_ID"), ApplicationSecret = Environment.GetEnvironmentVariable("APPLICATION_SECRET"), }; // Create a connection to our storage blob container using the credential. string dataSourceConnectionName = "hotels-data-source"; #if !SNIPPET dataSourceConnectionName = Recording.Random.GetName(); #endif SearchIndexerDataSourceConnection dataSourceConnection = new SearchIndexerDataSourceConnection( dataSourceConnectionName, SearchIndexerDataSourceType.AzureBlob, Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"), new SearchIndexerDataContainer( Environment.GetEnvironmentVariable("STORAGE_CONTAINER_NAME") ) ) { EncryptionKey = encryptionKey }; // Create an indexer to process documents from the blob container into the index. // You can optionally configure a skillset to use cognitive services when processing documents. // Set the SearchIndexerSkillset.EncryptionKey to the same credential if you use a skillset. string indexName = "hotels"; string indexerName = "hotels-indexer"; #if !SNIPPET indexName = resources.IndexName; indexerName = Recording.Random.GetName(); #endif SearchIndexer indexer = new SearchIndexer( indexerName, dataSourceConnectionName, indexName) { EncryptionKey = encryptionKey, // Map the fields in our documents we want to index. FieldMappings = { new FieldMapping("hotelId"), new FieldMapping("hotelName"), new FieldMapping("description"), new FieldMapping("tags"), new FieldMapping("address") }, Parameters = new IndexingParameters { // Tell the indexer to parse each blob as a separate JSON document. IndexingParametersConfiguration = new IndexingParametersConfiguration { ParsingMode = BlobIndexerParsingMode.Json } } }; // Now connect to our Search service and set up the data source and indexer. // Documents already in the storage blob will begin indexing immediately. Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); SearchIndexerClient indexerClient = new SearchIndexerClient(endpoint, credential); #if !SNIPPET indexerClient = resources.GetIndexerClient(); #endif indexerClient.CreateDataSourceConnection(dataSourceConnection); #if !SNIPPET cleanUpTasks.Push(() => indexerClient.DeleteDataSourceConnectionAsync(dataSourceConnectionName)); #endif indexerClient.CreateIndexer(indexer); #if !SNIPPET cleanUpTasks.Push(() => indexerClient.DeleteIndexerAsync(indexerName)); #endif #endregion Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Index await WaitForIndexingAsync(indexerClient, indexerName); #region Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Query // Create a SearchClient and search for luxury hotels. In production, be sure to use the query key. SearchClient searchClient = new SearchClient(endpoint, "hotels", credential); #if !SNIPPET searchClient = resources.GetSearchClient(); bool found = false; #endif Response <SearchResults <Hotel> > results = searchClient.Search <Hotel>("luxury hotels"); foreach (SearchResult <Hotel> result in results.Value.GetResults()) { Hotel hotel = result.Document; #if !SNIPPET found = true; #endif Console.WriteLine($"{hotel.HotelName} ({hotel.HotelId})"); Console.WriteLine($" Description: {hotel.Description}"); } #endregion Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Query Assert.IsTrue(found, "No luxury hotels were found in index"); } finally { // We want to await these individual to create a deterministic order for playing back tests. foreach (Func <Task> cleanUpTask in cleanUpTasks) { await cleanUpTask(); } } }
public async Task CreateIndexerAsync() { await using SearchResources resources = await SearchResources.CreateWithBlobStorageAsync(this, populate : true); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); Environment.SetEnvironmentVariable("STORAGE_CONNECTION_STRING", resources.StorageAccountConnectionString); Environment.SetEnvironmentVariable("STORAGE_CONTAINER", resources.BlobContainerName); Environment.SetEnvironmentVariable("COGNITIVE_SERVICES_KEY", resources.CognitiveServicesKey); // Define clean up tasks to be invoked in reverse order added. Stack <Func <Task> > cleanUpTasks = new Stack <Func <Task> >(); try { #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateSynonymMap // Create a new SearchIndexClient Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential); #if !SNIPPET indexClient = resources.GetIndexClient(new SearchClientOptions()); #endif // Create a synonym map from a file containing country names and abbreviations // using the Solr format with entry on a new line using \n, for example: // United States of America,US,USA\n string synonymMapName = "countries"; #if !SNIPPET synonymMapName = Recording.Random.GetName(); #endif string synonymMapPath = "countries.txt"; #if !SNIPPET synonymMapPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Samples", "countries.txt"); #endif SynonymMap synonyms; #if SNIPPET using (StreamReader file = File.OpenText(synonymMapPath)) { synonyms = new SynonymMap(synonymMapName, file); } #else synonyms = new SynonymMap(synonymMapName, CountriesSolrSynonymMap); #endif await indexClient.CreateSynonymMapAsync(synonyms); #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateSynonymMap // Make sure our synonym map gets deleted, which is not deleted when our // index is deleted when our SearchResources goes out of scope. cleanUpTasks.Push(() => indexClient.DeleteSynonymMapAsync(synonymMapName)); #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndex // Create the index string indexName = "hotels"; #if !SNIPPET indexName = Recording.Random.GetName(); #endif SearchIndex index = new SearchIndex(indexName) { Fields = { new SimpleField("hotelId", SearchFieldDataType.String) { IsKey = true, IsFilterable = true, IsSortable = true }, new SearchableField("hotelName") { IsFilterable = true, IsSortable = true }, new SearchableField("description") { AnalyzerName = LexicalAnalyzerName.EnLucene }, new SearchableField("descriptionFr") { AnalyzerName = LexicalAnalyzerName.FrLucene }, new SearchableField("tags", collection: true) { IsFilterable = true, IsFacetable = true }, new ComplexField("address") { Fields = { new SearchableField("streetAddress"), new SearchableField("city") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("stateProvince") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("country") { SynonymMapNames = new[] { synonymMapName },IsFilterable = true, IsSortable = true,IsFacetable = true }, new SearchableField("postalCode") { IsFilterable = true, IsSortable = true, IsFacetable = true } } } } }; await indexClient.CreateIndexAsync(index); #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndex // Make sure our synonym map gets deleted, which is not deleted when our // index is deleted when our SearchResources goes out of scope. cleanUpTasks.Push(() => indexClient.DeleteIndexAsync(indexName)); #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateDataSourceConnection // Create a new SearchIndexerClient SearchIndexerClient indexerClient = new SearchIndexerClient(endpoint, credential); #if !SNIPPET indexerClient = resources.GetIndexerClient(); #endif string dataSourceConnectionName = "hotels"; #if !SNIPPET dataSourceConnectionName = Recording.Random.GetName(); #endif SearchIndexerDataSourceConnection dataSourceConnection = new SearchIndexerDataSourceConnection( dataSourceConnectionName, SearchIndexerDataSourceType.AzureBlob, Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"), new SearchIndexerDataContainer(Environment.GetEnvironmentVariable("STORAGE_CONTAINER"))); await indexerClient.CreateDataSourceConnectionAsync(dataSourceConnection); #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateDataSourceConnection // Make sure our data source gets deleted, which is not deleted when our // index is deleted when our SearchResources goes out of scope. cleanUpTasks.Push(() => indexerClient.DeleteDataSourceConnectionAsync(dataSourceConnectionName)); #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Skillset // Translate English descriptions to French. // See https://docs.microsoft.com/azure/search/cognitive-search-skill-text-translation for details of the Text Translation skill. TextTranslationSkill translationSkill = new TextTranslationSkill( inputs: new[] { new InputFieldMappingEntry("text") { Source = "/document/description" } }, outputs: new[] { new OutputFieldMappingEntry("translatedText") { TargetName = "descriptionFrTranslated" } }, TextTranslationSkillLanguage.Fr) { Name = "descriptionFrTranslation", Context = "/document", DefaultFromLanguageCode = TextTranslationSkillLanguage.En }; // Use the human-translated French description if available; otherwise, use the translated description. // See https://docs.microsoft.com/azure/search/cognitive-search-skill-conditional for details of the Conditional skill. ConditionalSkill conditionalSkill = new ConditionalSkill( inputs: new[] { new InputFieldMappingEntry("condition") { Source = "= $(/document/descriptionFr) == null" }, new InputFieldMappingEntry("whenTrue") { Source = "/document/descriptionFrTranslated" }, new InputFieldMappingEntry("whenFalse") { Source = "/document/descriptionFr" } }, outputs: new[] { new OutputFieldMappingEntry("output") { TargetName = "descriptionFrFinal" } }) { Name = "descriptionFrConditional", Context = "/document", }; // Create a SearchIndexerSkillset that processes those skills in the order given below. string skillsetName = "translations"; #if !SNIPPET skillsetName = Recording.Random.GetName(); #endif SearchIndexerSkillset skillset = new SearchIndexerSkillset( skillsetName, new SearchIndexerSkill[] { translationSkill, conditionalSkill }) { CognitiveServicesAccount = new CognitiveServicesAccountKey( Environment.GetEnvironmentVariable("COGNITIVE_SERVICES_KEY")), KnowledgeStore = new SearchIndexerKnowledgeStore( Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"), new List <SearchIndexerKnowledgeStoreProjection>()), }; await indexerClient.CreateSkillsetAsync(skillset); #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Skillset // Make sure our skillset gets deleted, which is not deleted when our // index is deleted when our SearchResources goes out of scope. cleanUpTasks.Push(() => indexerClient.DeleteSkillsetAsync(skillsetName)); #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndexer string indexerName = "hotels"; #if !SNIPPET indexerName = Recording.Random.GetName(); #endif SearchIndexer indexer = new SearchIndexer( indexerName, dataSourceConnectionName, indexName) { // We only want to index fields defined in our index, excluding descriptionFr if defined. FieldMappings = { new FieldMapping("hotelId"), new FieldMapping("hotelName"), new FieldMapping("description"), new FieldMapping("tags"), new FieldMapping("address") }, OutputFieldMappings = { new FieldMapping("/document/descriptionFrFinal") { TargetFieldName = "descriptionFr" } }, Parameters = new IndexingParameters { // Tell the indexer to parse each blob as a separate JSON document. IndexingParametersConfiguration = new IndexingParametersConfiguration { ParsingMode = BlobIndexerParsingMode.Json } }, SkillsetName = skillsetName }; // Create the indexer which, upon successful creation, also runs the indexer. await indexerClient.CreateIndexerAsync(indexer); #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndexer // Make sure our indexer gets deleted, which is not deleted when our // index is deleted when our SearchResources goes out of scope. cleanUpTasks.Push(() => indexerClient.DeleteIndexerAsync(indexerName)); // Wait till the indexer is done. await WaitForIndexingAsync(indexerClient, indexerName); #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Query // Get a SearchClient from the SearchIndexClient to share its pipeline. SearchClient searchClient = indexClient.GetSearchClient(indexName); #if !SNIPPET searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); #endif // Query for hotels with an ocean view. SearchResults <Hotel> results = await searchClient.SearchAsync <Hotel>("ocean view"); #if !SNIPPET bool found = false; #endif await foreach (SearchResult <Hotel> result in results.GetResultsAsync()) { Hotel hotel = result.Document; #if !SNIPPET if (hotel.HotelId == "6") { Assert.IsNotNull(hotel.DescriptionFr); found = true; } #endif Console.WriteLine($"{hotel.HotelName} ({hotel.HotelId})"); Console.WriteLine($" Description (English): {hotel.Description}"); Console.WriteLine($" Description (French): {hotel.DescriptionFr}"); } #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Query Assert.IsTrue(found, "Expected hotel #6 not found in search results"); } finally { // We want to await these individual to create a deterministic order for playing back tests. foreach (Func <Task> cleanUpTask in cleanUpTasks) { await cleanUpTask(); } } }
public async Task CreateIndex() { await using SearchResources resources = SearchResources.CreateWithNoIndexes(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_CreateIndex Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); // Define client options to use camelCase when serializing property names. SearchClientOptions options = new SearchClientOptions(ServiceVersion) { Serializer = new JsonObjectSerializer( new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }) }; // Create a service client. AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential, options); #if !SNIPPET indexClient = resources.GetIndexClient(options); #endif // Create the FieldBuilder using the same serializer. FieldBuilder fieldBuilder = new FieldBuilder { Serializer = options.Serializer }; // Create the index using FieldBuilder. #if SNIPPET SearchIndex index = new SearchIndex("movies") #else SearchIndex index = new SearchIndex(Recording.Random.GetName()) #endif { Fields = fieldBuilder.Build(typeof(Movie)), Suggesters = { // Suggest query terms from the "name" field. new SearchSuggester("n", "name") } }; // Define the "genre" field as a string. SearchableField genreField = new SearchableField("genre") { AnalyzerName = LexicalAnalyzerName.Values.EnLucene, IsFacetable = true, IsFilterable = true }; index.Fields.Add(genreField); // Create the index. indexClient.CreateIndex(index); #endregion Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_CreateIndex // Make sure the index is removed. resources.IndexName = index.Name; #region Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_UploadDocument Movie movie = new Movie { Id = Guid.NewGuid().ToString("n"), Name = "The Lord of the Rings: The Return of the King", Genre = MovieGenre.Fantasy, Year = 2003, Rating = 9.1 }; // Add a movie to the index. SearchClient searchClient = indexClient.GetSearchClient(index.Name); searchClient.UploadDocuments(new[] { movie }); #endregion Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_UploadDocument }
public async Task CreateIndex() { await using SearchResources resources = SearchResources.CreateWithNoIndexes(this); Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString()); Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey); #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); // Create a service client AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient client = new SearchIndexClient(endpoint, credential); /*@@*/ client = resources.GetIndexClient(); // Create the index //@@SearchIndex index = new SearchIndex("hotels") /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName()) { Fields = { new SimpleField("hotelId", SearchFieldDataType.String) { IsKey = true, IsFilterable= true, IsSortable = true }, new SearchableField("hotelName") { IsFilterable = true, IsSortable = true }, new SearchableField("description") { AnalyzerName = LexicalAnalyzerName.EnLucene }, new SearchableField("tags", collection: true) { IsFilterable = true, IsFacetable = true }, new ComplexField("address") { Fields = { new SearchableField("streetAddress"), new SearchableField("city") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("stateProvince") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("country") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("postalCode") { IsFilterable = true, IsSortable = true, IsFacetable = true } } } }, Suggesters = { // Suggest query terms from both the hotelName and description fields. new SearchSuggester("sg", "hotelName", "description") } }; client.CreateIndex(index); #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex }
public async Task CreateManualIndex() { await using SearchResources resources = SearchResources.CreateWithNoIndexes(this); SearchIndexClient client = resources.GetIndexClient(); #region Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex // Create the index using field definitions. #region Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex //@@SearchIndex index = new SearchIndex("hotels") /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName()) { Fields = { new SimpleField("hotelId", SearchFieldDataType.String) { IsKey = true, IsFilterable= true, IsSortable = true }, new SearchableField("hotelName") { IsFilterable = true, IsSortable = true }, new SearchableField("description") { AnalyzerName = LexicalAnalyzerName.EnLucene }, new SearchableField("tags", collection: true) { IsFilterable = true, IsFacetable = true }, new ComplexField("address") { Fields = { new SearchableField("streetAddress"), new SearchableField("city") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("stateProvince") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("country") { IsFilterable = true, IsSortable = true, IsFacetable = true }, new SearchableField("postalCode") { IsFilterable = true, IsSortable = true, IsFacetable = true } } } }, Suggesters = { // Suggest query terms from the hotelName field. new SearchSuggester("sg", "hotelName") } }; #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex client.CreateIndex(index); #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex resources.IndexName = index.Name; }