Ejemplo n.º 1
0
        private static async void GetAutoSuggest(string query)
        {
            var searchServiceClient = new SearchServiceClient(_subscriptionKey);
            var searchResult        = await searchServiceClient.SearchAsync(query, 10);

            Console.WriteLine("Query: " + query);
            Console.WriteLine(" Type: " + searchResult._type);
            Console.WriteLine(" Total Estimated Match: " + searchResult.webPages.totalEstimatedMatches);
            Console.WriteLine(" Web search url: " + searchResult.webPages.webSearchUrl);

            foreach (var webPage in searchResult.webPages.value)
            {
                Console.WriteLine("  - Name: " + webPage.name);
                Console.WriteLine("  - Url: " + webPage.displayUrl);
                Console.WriteLine("  - Snippet: " + webPage.snippet);
                if (webPage.searchTags != null)
                {
                    var tags = webPage.searchTags.Aggregate(string.Empty,
                                                            (current, searchTag) => current + $@"{searchTag.name} [{searchTag.content}],");
                    Console.WriteLine("  - Tags: " + tags);
                }
                if (webPage.deepLinks != null)
                {
                    var deepLinks = webPage.deepLinks.Aggregate(string.Empty,
                                                                (current, deepLink) => current + $@"{deepLink.name} [{deepLink.url}],");
                    Console.WriteLine("  - Deep Links: " + deepLinks);
                }
            }

            Console.WriteLine(" Images");
            foreach (var image in searchResult.images.value)
            {
                Console.WriteLine("  - Name: " + image.name);
                System.Diagnostics.Process.Start(image.thumbnailUrl);
            }

            Console.WriteLine("Done !");
            Console.WriteLine("");
        }
        /// <summary>Snippet for SearchAsync</summary>
        public async Task SearchRequestObjectAsync()
        {
            // Snippet: SearchAsync(SearchRequest, CallSettings)
            // Create client
            SearchServiceClient searchServiceClient = await SearchServiceClient.CreateAsync();

            // Initialize request argument(s)
            SearchRequest request = new SearchRequest
            {
                Placement          = "",
                BranchAsBranchName = BranchName.FromProjectLocationCatalogBranch("[PROJECT]", "[LOCATION]", "[CATALOG]", "[BRANCH]"),
                Query      = "",
                VisitorId  = "",
                UserInfo   = new UserInfo(),
                Offset     = 0,
                Filter     = "",
                OrderBy    = "",
                FacetSpecs =
                {
                    new SearchRequest.Types.FacetSpec(),
                },
                BoostSpec          = new SearchRequest.Types.BoostSpec(),
                QueryExpansionSpec = new SearchRequest.Types.QueryExpansionSpec(),
                VariantRollupKeys  = { "", },
                DynamicFacetSpec   = new SearchRequest.Types.DynamicFacetSpec(),
                PageCategories     = { "", },
                CanonicalFilter    = "",
            };
            // Make the request
            PagedAsyncEnumerable <SearchResponse, SearchResponse.Types.SearchResult> response = searchServiceClient.SearchAsync(request);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((SearchResponse.Types.SearchResult item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over pages (of server-defined size), performing one RPC per page
            await response.AsRawResponses().ForEachAsync((SearchResponse page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (SearchResponse.Types.SearchResult item in page)
                {
                    // Do something with each item
                    Console.WriteLine(item);
                }
            });

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int pageSize = 10;
            Page <SearchResponse.Types.SearchResult> singlePage = await response.ReadPageAsync(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (SearchResponse.Types.SearchResult item in singlePage)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
Ejemplo n.º 3
0
 public void CallSearch()
 {
     InitSearchClient();
     Busy();
     searchServiceClient.SearchAsync(query, sources, PageIndex, PageSize);
 }