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 } }
private static async Task <SantaResponse> Låge2(ParticipateResponse participateResponse, HttpClient httpClient) { string documentId = participateResponse.Id.ToString(); Console.WriteLine("Creating search client..."); SearchClient client = new SearchClient(participateResponse.Credentials.Username, participateResponse.Credentials.Password, cloudId); Console.WriteLine("Fetching document..."); SantaInformation santaInfo = client.GetDocument(index, documentId); Console.WriteLine("Converting to meters..."); IEnumerable <SantaMovement> alignedSantaMovements = santaInfo.ConvertAllMovementsToMeters(); Console.WriteLine($"Moving santa...starting from: {santaInfo.CanePosition.ToString()}"); GeoPoint santaEndlocation = SantaMover.Move(santaInfo.CanePosition, alignedSantaMovements); Console.WriteLine($"lat:{santaEndlocation.lat.ToString(CultureInfo.GetCultureInfo("en-US"))}, lon: {santaEndlocation.lon.ToString(CultureInfo.GetCultureInfo("en-US"))}"); HttpResponseMessage apiResponse = await httpClient.PostAsJsonAsync("/api/santarescue", new { id = participateResponse.Id, position = santaEndlocation }); if (!apiResponse.IsSuccessStatusCode) { throw new ChristmasException($"{apiResponse.StatusCode}: {(await apiResponse.Content.ReadAsStringAsync())}"); } Console.WriteLine(await apiResponse.Content.ReadAsStringAsync()); return(await apiResponse.Content.ReadAsAsync <SantaResponse>()); }
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 }
public SearchDocument LookUp(string id) { // Execute geo search based on query string try { return _searchClient.GetDocument<SearchDocument>(id); } catch (Exception ex) { Console.WriteLine("Error querying index: {0}\r\n", ex.Message.ToString()); } return null; }
private static void RunQueries(SearchClient srchclient) { SearchOptions options; SearchResults <Hotel> response; Console.WriteLine("Query #1: Search on empty term '*' to return all documents, showing a subset of fields...\n"); options = new SearchOptions() { IncludeTotalCount = true, Filter = "", OrderBy = { "" } }; options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Address/City"); response = srchclient.Search <Hotel>("*", options); WriteDocuments(response); Console.WriteLine("Query #2: Search on 'hotels', filter on 'Rating gt 4', sort by Rating in descending order...\n"); options = new SearchOptions() { Filter = "Rating gt 4", OrderBy = { "Rating desc" } }; options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Rating"); response = srchclient.Search <Hotel>("hotels", options); WriteDocuments(response); Console.WriteLine("Query #3: Limit search to specific fields (pool in Tags field)...\n"); options = new SearchOptions() { SearchFields = { "Tags" } }; options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Tags"); response = srchclient.Search <Hotel>("pool", options); WriteDocuments(response); Console.WriteLine("Query #4: Facet on 'Category'...\n"); options = new SearchOptions() { Filter = "" }; options.Facets.Add("Category"); options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Category"); response = srchclient.Search <Hotel>("*", options); WriteDocuments(response); Console.WriteLine("Query #5: Look up a specific document...\n"); Response <Hotel> lookupResponse = srchclient.GetDocument <Hotel>("3"); Console.WriteLine(lookupResponse.Value.HotelId); Console.WriteLine(lookupResponse.Value); }
// Run queries, use WriteDocuments to print output private static void RunQueries(SearchClient srchclient) { SearchOptions options; SearchResults <Hotel> response; // Query 1 Console.WriteLine("Query #1: Search on empty term '*' to return all documents, showing a subset of fields...\n"); options = new SearchOptions() { IncludeTotalCount = true, Filter = "", OrderBy = { "" } }; options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Rating"); response = srchclient.Search <Hotel>("*", options); WriteDocuments(response); // Query 2 Console.WriteLine("Query #2: Search on 'hotels', filter on 'Rating gt 4', sort by Rating in descending order...\n"); options = new SearchOptions() { Filter = "Rating gt 4", OrderBy = { "Rating desc" } }; options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Rating"); response = srchclient.Search <Hotel>("hotels", options); WriteDocuments(response); // Query 3 Console.WriteLine("Query #3: Limit search to specific fields (pool in Tags field)...\n"); options = new SearchOptions() { SearchFields = { "Tags" } }; options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Tags"); response = srchclient.Search <Hotel>("pool", options); WriteDocuments(response); // Query 4 - Use Facets to return a faceted navigation structure for a given query // Filters are typically used with facets to narrow results on OnClick events Console.WriteLine("Query #4: Facet on 'Category'...\n"); options = new SearchOptions() { Filter = "" }; options.Facets.Add("Category"); options.Select.Add("HotelId"); options.Select.Add("HotelName"); options.Select.Add("Category"); response = srchclient.Search <Hotel>("*", options); WriteDocuments(response); // Query 5 Console.WriteLine("Query #5: Look up a specific document...\n"); Response <Hotel> lookupResponse; lookupResponse = srchclient.GetDocument <Hotel>("3"); Console.WriteLine(lookupResponse.Value.HotelId); // Query 6 Console.WriteLine("Query #6: Call Autocomplete on HotelName...\n"); var autoresponse = srchclient.Autocomplete("sa", "sg"); WriteDocuments(autoresponse); }