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 } }
public async Task Index() { await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); try { #region Snippet:Azure_Search_Tests_Samples_Readme_Index IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create( IndexDocumentsAction.Upload(new Hotel { Id = "783", Name = "Upload Inn" }), IndexDocumentsAction.Merge(new Hotel { Id = "12", Name = "Renovated Ranch" })); IndexDocumentsOptions options = new IndexDocumentsOptions { ThrowOnAnyError = true }; client.IndexDocuments(batch, options); #endregion Snippet:Azure_Search_Tests_Samples_Readme_Index } catch (RequestFailedException) { // Ignore the non-existent merge failure } }
public async Task QueryStatic() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient 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 }
public async Task Options() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); #region Snippet:Azure_Search_Tests_Samples_Readme_Options int stars = 4; SearchOptions options = new SearchOptions { // Filter to only Rating greater than or equal our preference #if SNIPPET Filter = SearchFilter.Create($"Rating ge {stars}"), #else Filter = SearchFilter.Create($"rating ge {stars}"), #endif Size = 5, // Take only 5 results #if SNIPPET OrderBy = { "Rating desc" } // Sort by Rating from high to low #else OrderBy = { "rating desc" } // Sort by rating from high to low #endif }; SearchResults <Hotel> response = client.Search <Hotel>("luxury", options); // ... #endregion Snippet:Azure_Search_Tests_Samples_Readme_Options }
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 async Task Troubleshooting() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); #region Snippet:Azure_Search_Tests_Samples_Readme_Troubleshooting try { //@@ return client.GetDocument<Hotel>("12345"); /*@@*/ await client.GetDocumentAsync <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."); } #endregion Snippet:Azure_Search_Tests_Samples_Readme_Troubleshooting }