예제 #1
0
        public async Task HandleErrors()
        {
            await using SearchResources resources = await SearchResources.CreateWithHotelsIndexAsync(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"));
            SearchApiKeyCredential credential = new SearchApiKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));

            // Create an invalid SearchIndexClientClient
            string            fakeIndexName = "doesnotexist";
            SearchIndexClient index         = new SearchIndexClient(endpoint, fakeIndexName, credential);
            /*@@*/ index = InstrumentClient(new SearchIndexClient(endpoint, fakeIndexName, credential, GetSearchClientOptions()));
            try
            {
                //@@ index.GetCount();
                /*@@*/ await index.GetCountAsync();
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                Console.WriteLine("Index wasn't found.");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_HandleErrors
        }
예제 #2
0
        public async Task CreateClientAsync()
        {
            await using SearchResources resources = await SearchResources.CreateWithHotelsIndexAsync(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"));
            SearchApiKeyCredential credential = new SearchApiKeyCredential(
                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.GetStatisticsAsync();

            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);
        }
예제 #3
0
        public async Task FirstQuery()
        {
            #region Snippet:Azure_Search_Tests_Samples_Readme_FirstQuery
            // We'll connect to the Azure Cognitive Search public sandbox and send a
            // query to its "nycjobs" index built from a public dataset of available jobs
            // in New York.
            string serviceName = "azs-playground";
            string indexName   = "nycjobs";
            string apiKey      = "252044BE3886FE4A8E3BAA4F595114BB";

            // Create a SearchIndexClient to send queries
            Uri serviceEndpoint = new Uri($"https://{serviceName}.search.windows.net/");
            SearchApiKeyCredential credential = new SearchApiKeyCredential(apiKey);
            SearchIndexClient      client     = new SearchIndexClient(serviceEndpoint, indexName, credential);

            // Let's get the top 5 jobs related to Microsoft
            //@@ SearchResults<SearchDocument> response = client.Search("Microsoft", new SearchOptions { Size = 5 });
            //@@ foreach (SearchResult<SearchDocument> result in response.GetResults())
            /*@@*/ SearchResults <SearchDocument> response = await client.SearchAsync("Microsoft", new SearchOptions { Size = 5 });

            /*@@*/ await foreach (SearchResult <SearchDocument> result in response.GetResultsAsync())
            {
                // Print out the title and job description (we'll see below how to
                // use C# objects to make accessing these fields much easier)
                string title       = (string)result.Document["business_title"];
                string description = (string)result.Document["job_description"];
                Console.WriteLine($"{title}\n{description}\n");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_FirstQuery
        }
예제 #4
0
        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
            SearchApiKeyCredential credential = new SearchApiKeyCredential(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
        }
예제 #5
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"));
            SearchApiKeyCredential credential = new SearchApiKeyCredential(
                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.GetStatisticsAsync();

            Console.WriteLine($"You are using {stats.Value.Counters.IndexCounter.Usage} of {stats.Value.Counters.IndexCounter.Quota} indexes.");
            #endregion Snippet:Azure_Search_Tests_Samples_GetStatisticsAsync
        }
예제 #6
0
        public async Task GetCountAsync()
        {
            await using SearchResources resources = await SearchResources.CreateWithHotelsIndexAsync(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 SearchIndexClient
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            SearchApiKeyCredential credential = new SearchApiKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
            string            indexName = Environment.GetEnvironmentVariable("SEARCH_INDEX");
            SearchIndexClient index     = new SearchIndexClient(endpoint, indexName, credential);
            /*@@*/ index = InstrumentClient(new SearchIndexClient(endpoint, indexName, credential, GetSearchClientOptions()));

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

            Console.WriteLine($"Search index {indexName} has {count.Value} documents.");
            #endregion Snippet:Azure_Search_Tests_Samples_GetCountAsync
        }