public async Task CreateClientAsync()
        {
            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_CreateClientAsync
            // 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 = await search.GetServiceStatisticsAsync();

            Console.WriteLine($"You are using {stats.Value.Counters.IndexCounter.Usage} indexes.");
            #endregion Snippet:Azure_Search_Tests_Samples_CreateClientAsync

            Assert.AreEqual(1, stats.Value.Counters.IndexCounter.Usage);
        }
        public async Task HandleErrors()
        {
            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_HandleErrors
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            AzureKeyCredential credential = new AzureKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));

            // Create an invalid SearchClient
            string       fakeIndexName = "doesnotexist";
            SearchClient client        = new SearchClient(endpoint, fakeIndexName, credential);
            /*@@*/ client = InstrumentClient(new SearchClient(endpoint, fakeIndexName, credential, GetSearchClientOptions()));
            try
            {
                client.GetDocumentCount();
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                Console.WriteLine("Index wasn't found.");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_HandleErrors
        }
Beispiel #3
0
        public async Task Troubleshooting()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient client = resources.GetQueryClient();

            LookupHotel();

            // We want the sample to have a return but the unit test doesn't
            // like that so we move it into a block scoped function
            Response <Hotel> LookupHotel()
            {
                #region Snippet:Azure_Search_Tests_Samples_Readme_Troubleshooting
                try
                {
                    return(client.GetDocument <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.");
                    return(null);
                }
                #endregion Snippet:Azure_Search_Tests_Samples_Readme_Troubleshooting
            }
        }
        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 searchClient = new SearchClient(endpoint, indexName, credential);
#if !SNIPPET
            searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions()));
#endif

            // Get and report the number of documents in the index
            Response <long> count = await searchClient.GetDocumentCountAsync();

            Console.WriteLine($"Search index {indexName} has {count.Value} documents.");
            #endregion Snippet:Azure_Search_Tests_Samples_GetCountAsync
        }
Beispiel #5
0
        public async Task QueryStatic()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient client = resources.GetQueryClient();

            #region Snippet:Azure_Search_Tests_Samples_Readme_StaticQuery
            SearchResults <Hotel> response = client.Search <Hotel>("luxury");
            foreach (SearchResult <Hotel> result in response.GetResults())
            {
                Hotel doc = result.Document;
                Console.WriteLine($"{doc.Id}: {doc.Name}");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_StaticQuery

            #region Snippet:Azure_Search_Tests_Samples_Readme_StaticQueryAsync
            //@@SearchResults<Hotel> response = await client.SearchAsync<Hotel>("luxury");
            /*@@*/ response = await client.SearchAsync <Hotel>("luxury");

            await foreach (SearchResult <Hotel> result in response.GetResultsAsync())
            {
                Hotel doc = result.Document;
                Console.WriteLine($"{doc.Id}: {doc.Name}");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_StaticQueryAsync
        }
Beispiel #6
0
        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 Rating greater than or equal our preference
#if SNIPPET
                Filter = SearchFilter.Create($"Rating ge {stars}"),
#else
                Filter = SearchFilter.Create($"rating ge {stars}"),
#endif
                Size = 5, // Take only 5 results
#if SNIPPET
                OrderBy = { "Rating desc" } // Sort by Rating from high to low
#else
                OrderBy = { "rating desc" } // Sort by rating from high to low
#endif
            };
            SearchResults <Hotel> response = client.Search <Hotel>("luxury", options);
            // ...
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_Options
        }
        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");
#if SNIPPET
            string indexName = "hotels";
#endif

            // Create a client
            AzureKeyCredential credential = new AzureKeyCredential(key);
            SearchClient       client     = new SearchClient(endpoint, indexName, credential);
#if !SNIPPET
            client = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions()));
#endif
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_Client

            #region Snippet:Azure_Search_Tests_Samples_Readme_Dict
            SearchResults <SearchDocument> response = client.Search <SearchDocument>("luxury");
            foreach (SearchResult <SearchDocument> result in response.GetResults())
            {
                SearchDocument doc = result.Document;
#if SNIPPET
                string id   = (string)doc["HotelId"];
                string name = (string)doc["HotelName"];
#else
                string id   = (string)doc["hotelId"];
                string name = (string)doc["hotelName"];
#endif
                Console.WriteLine("{id}: {name}");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_Dict

            #region Snippet:Azure_Search_Tests_Samples_Readme_Dynamic
#if SNIPPET
            SearchResults <SearchDocument> response = client.Search <SearchDocument>("luxury");
#else
            response = client.Search <SearchDocument>("luxury");
#endif
            foreach (SearchResult <SearchDocument> result in response.GetResults())
            {
                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 GetDocument()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient client = resources.GetQueryClient();

            #region Snippet:Azure_Search_Tests_Samples_Readme_GetDocument
            Hotel doc = client.GetDocument <Hotel>("1");
            Console.WriteLine($"{doc.Id}: {doc.Name}");
            #endregion
        }
Beispiel #9
0
        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
        }
Beispiel #11
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 SearchIndexClient
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            AzureKeyCredential credential = new AzureKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
            SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential);
            /*@@*/ indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions()));

            // Get and report the Search Service statistics
            Response <SearchServiceStatistics> stats = await indexClient.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
        }