Exemplo n.º 1
0
        public void 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/");
            AzureKeyCredential credential = new AzureKeyCredential(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())
            {
                // 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
        }
Exemplo n.º 2
0
        public async Task QueryStatic()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient 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
        }
Exemplo n.º 3
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
            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");
            foreach (SearchResult <SearchDocument> result in response.GetResults())
            {
                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 = client.Search("luxury");
            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
        }
Exemplo n.º 4
0
        public async Task Options()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient 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
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get the next (server-side) page of results.
        /// </summary>
        /// <param name="async">
        /// Whether to execute synchronously or asynchronously.
        /// </param>
        /// <param name="cancellationToken">
        /// Optional <see cref="CancellationToken"/> to propagate notifications
        /// that the operation should be canceled.
        /// </param>
        /// <returns>The next page of SearchResults.</returns>
        internal async Task <SearchResults <T> > GetNextPageAsync(bool async, CancellationToken cancellationToken)
        {
            SearchResults <T> next = null;

            if (_indexClient != null && NextOptions != null)
            {
                next = async ?
                       await _indexClient.SearchAsync <T>(
                    NextOptions.SearchText,
                    NextOptions,
                    cancellationToken)
                       .ConfigureAwait(false) :
                       _indexClient.Search <T>(
                    NextOptions.SearchText,
                    NextOptions,
                    cancellationToken);
            }
            return(next);
        }