public async Task <IEnumerable <SearchLocationIndex> > SuggestAsync(string term)
        {
            logger.LogInformation($"Starting to get suggestions for {term}");
            try
            {
                var suggestOptions = new SuggestOptions()
                {
                    Size = 20,
                };

                suggestOptions.Select.Add(nameof(SearchLocationIndex.LocationId));
                suggestOptions.Select.Add(nameof(SearchLocationIndex.LocationName));
                suggestOptions.Select.Add(nameof(SearchLocationIndex.LocationAuthorityDistrict));
                suggestOptions.Select.Add(nameof(SearchLocationIndex.LocalAuthorityName));
                suggestOptions.Select.Add(nameof(SearchLocationIndex.Longitude));
                suggestOptions.Select.Add(nameof(SearchLocationIndex.Latitude));

                var searchClient = new SearchClient(azureSearchIndexConfig.EndpointUri, azureSearchIndexConfig.LocationSearchIndex, GetAzureKeyCredential());

                var suggestResults = await searchClient.SuggestAsync <SearchLocationIndex>(term, suggestorName, suggestOptions)
                                     .ConfigureAwait(false);

                return(suggestResults.Value.Results.Select(i => i.Document));
            }
            catch (Exception ex)
            {
                logger.LogError("Getting suggestions had an error", ex);
                throw;
            }
        }
Example #2
0
        public async Task <ActionResult> SuggestAsync(bool highlights, bool fuzzy, string term)
        {
            InitSearch();

            // Setup the suggest parameters.
            var options = new SuggestOptions()
            {
                UseFuzzyMatching = fuzzy,
                Size             = 8,
            };

            if (highlights)
            {
                options.HighlightPreTag  = "<b>";
                options.HighlightPostTag = "</b>";
            }

            // Only one suggester can be specified per index. The name of the suggester is set when the suggester is specified by other API calls.
            // The suggester for the hotel database is called "sg" and simply searches the hotel name.
            var suggestResult = await _searchClient.SuggestAsync <Hotel>(term, "sg", options).ConfigureAwait(false);

            // Convert the suggest query results to a list that can be displayed in the client.
            List <string> suggestions = suggestResult.Value.Results.Select(x => x.Text).ToList();

            // Return the list of suggestions.
            return(new JsonResult(suggestions));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            // Get Document Id
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    data        = JsonSerializer.Deserialize <RequestBodySuggest>(requestBody);

            // Cognitive Search
            Uri serviceEndpoint = new Uri($"https://{searchServiceName}.search.windows.net/");

            SearchClient searchClient = new SearchClient(
                serviceEndpoint,
                searchIndexName,
                new AzureKeyCredential(searchApiKey)
                );

            SuggestOptions options = new SuggestOptions()
            {
                Size = data.Size
            };

            var suggesterResponse = await searchClient.SuggestAsync <GpdModel>(data.SearchText, data.SuggesterName, options);

            var response = new Dictionary <string, List <SearchSuggestion <GpdModel> > >();

            response["suggestions"] = suggesterResponse.Value.Results.ToList();

            return(new OkObjectResult(response));
        }
        public async Task <SuggestResults <PersonCity> > Suggest(
            bool highlights, bool fuzzy, string term)
        {
            SuggestOptions sp = new SuggestOptions()
            {
                UseFuzzyMatching = fuzzy,
                Size             = 5,
            };

            sp.Select.Add("Id");
            sp.Select.Add("Name");
            sp.Select.Add("FamilyName");
            sp.Select.Add("Info");
            sp.Select.Add("CityCountry");
            sp.Select.Add("Web");

            if (highlights)
            {
                sp.HighlightPreTag  = "<b>";
                sp.HighlightPostTag = "</b>";
            }

            var suggestResults = await _searchClient.SuggestAsync <PersonCity>(term, "personSg", sp).ConfigureAwait(false);

            return(suggestResults.Value);
        }
Example #5
0
        public async Task <IList <SuggestResult> > SuggestAsync(string searchText)
        {
            var options = new SuggestOptions
            {
                UseFuzzyMatching = true,
                HighlightPreTag  = "<b>",
                HighlightPostTag = "</b>"
            };

            var result = await client.SuggestAsync <Accommodation>(searchText, "sg", options);

            var items = result.Value.Results;

            return(result.Value != null?mapper.MapFrom(items.ToArray()) : null);
        }
Example #6
0
        public async Task <List <string> > AutoCompleteAndSuggestAsync(string term)
        {
            if (string.IsNullOrEmpty(term))
            {
                return(new List <string>());
            }

            // Setup the type-ahead search parameters.
            var ap = new AutocompleteOptions()
            {
                Mode = AutocompleteMode.OneTermWithContext,
                Size = 1,
            };
            var autocompleteResult = await _searchclient.AutocompleteAsync(term, "sg", ap);

            // Setup the suggest search parameters.
            var sp = new SuggestOptions()
            {
                Size = 8,
            };

            // Only one suggester can be specified per index. The name of the suggester is set when the suggester is specified by other API calls.
            // The suggester for the hotel database is called "sg" and simply searches the hotel name.
            var suggestResult = await _searchclient.SuggestAsync <Hotel>(term, "sg", sp).ConfigureAwait(false);

            // Create an empty list.
            var results = new List <string>();

            if (autocompleteResult.Value.Results.Count > 0)
            {
                // Add the top result for type-ahead.
                results.Add(autocompleteResult.Value.Results[0].Text);
            }
            else
            {
                // There were no type-ahead suggestions, so add an empty string.
                results.Add("");
            }

            for (int n = 0; n < suggestResult.Value.Results.Count; n++)
            {
                // Now add the suggestions.
                results.Add(suggestResult.Value.Results[n].Text);
            }

            return(results);
        }
Example #7
0
        private void CreateSearchClient()
        {
            this.suggestOptions = new SuggestOptions()
            {
                Size = azureSearchIndexConfig.SearchMaxNumberToSuggest,
            };

            suggestOptions.Select.Add(nameof(SearchLocationIndex.LocationId));
            suggestOptions.Select.Add(nameof(SearchLocationIndex.LocationName));
            suggestOptions.Select.Add(nameof(SearchLocationIndex.LocationAuthorityDistrict));
            suggestOptions.Select.Add(nameof(SearchLocationIndex.LocalAuthorityName));
            suggestOptions.Select.Add(nameof(SearchLocationIndex.Longitude));
            suggestOptions.Select.Add(nameof(SearchLocationIndex.Latitude));

            var azureKeyCredential = new AzureKeyCredential(azureSearchIndexConfig.SearchServiceAdminAPIKey);

            this.searchClient = new SearchClient(azureSearchIndexConfig.EndpointUri, azureSearchIndexConfig.LocationSearchIndex, azureKeyCredential);
        }
Example #8
0
        public SuggestResults<SearchDocument> Suggest(string searchText, bool fuzzy)
        {
            // Execute search based on query string
            try
            {
                SuggestOptions options = new SuggestOptions()
                {
                    UseFuzzyMatching = fuzzy,
                    Size = 8
                };

                return _searchClient.Suggest<SearchDocument>(searchText, "sg", options);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error querying index: {0}\r\n", ex.Message.ToString());
            }
            return null;
        }
Example #9
0
 public virtual Response <SuggestDocumentsResult> SuggestGet(string searchText, string suggesterName, SuggestOptions suggestOptions = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("DocumentsClient.SuggestGet");
     scope.Start();
     try
     {
         return(RestClient.SuggestGet(searchText, suggesterName, suggestOptions, requestOptions, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }