public void CanContinueSearchForDynamicDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); IEnumerable <string> hotelIds = IndexDocuments(client, 2001); var searchParameters = new SearchParameters() { Top = 3000, OrderBy = new[] { "hotelId asc" }, Select = new[] { "hotelId" } }; IEnumerable <string> expectedIds = Data.TestDocuments.Select(d => d.HotelId).Concat(hotelIds).OrderBy(id => id); DocumentSearchResponse response = client.Documents.Search("*", searchParameters); AssertKeySequenceEqual(response, expectedIds.Take(1000).ToArray()); Assert.NotNull(response.ContinuationToken); response = client.Documents.ContinueSearch(response.ContinuationToken); AssertKeySequenceEqual(response, expectedIds.Skip(1000).Take(1000).ToArray()); Assert.NotNull(response.ContinuationToken); response = client.Documents.ContinueSearch(response.ContinuationToken); AssertKeySequenceEqual(response, expectedIds.Last()); Assert.Null(response.ContinuationToken); }); }
public void CanSearchStaticallyTypedDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Null(response.ContinuationToken); Assert.Null(response.Count); Assert.Null(response.Coverage); Assert.Null(response.Facets); Assert.NotNull(response.Results); Assert.Equal(Data.TestDocuments.Length, response.Results.Count); for (int i = 0; i < response.Results.Count; i++) { Assert.Equal(1, response.Results[i].Score); Assert.Null(response.Results[i].Highlights); } SearchAssert.SequenceEqual(response.Results.Select(r => r.Document), Data.TestDocuments); }); }
} // DeleteCustomer /// <summary> /// Search for a csutomer /// </summary> public List <Model.Search.SearchCustomerModel> CustomerSearch(Interface.GlobalEnum.IndexerIndexName indexName, string customerName) { // only check once per run if (!doesIndexExistsCheck.Contains(indexName.ToString().ToLower())) { CreateIndexIfNotExists(indexName, Interface.GlobalEnum.IndexerRepositoryIndexType.SystemDefined); doesIndexExistsCheck.Add(indexName.ToString().ToLower()); } SearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName.ToString().ToLower()); SearchParameters searchParameters = new SearchParameters(); List <Model.Search.SearchCustomerModel> resultList = new List <Model.Search.SearchCustomerModel>(); DocumentSearchResponse <Model.Search.SearchCustomerModel> response = indexClient.Documents.Search <Model.Search.SearchCustomerModel>(customerName, searchParameters); foreach (SearchResult <Model.Search.SearchCustomerModel> item in response) { Model.Search.SearchCustomerModel searchDocument = new Model.Search.SearchCustomerModel(); searchDocument.CustomerId = item.Document.CustomerId; searchDocument.CustomerName = item.Document.CustomerName; resultList.Add(searchDocument); } return(resultList); }
public void CanSearchWithSelectedFields() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); var searchParameters = new SearchParameters() { SearchFields = new[] { "category", "hotelName" }, Select = new[] { "hotelName", "baseRate" } }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("fancy luxury", searchParameters); var expectedDoc = new Hotel() { HotelName = "Fancy Stay", BaseRate = 199.0 }; Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Results); Assert.Equal(1, response.Results.Count); Assert.Equal(expectedDoc, response.Results.First().Document); }); }
public void TestConstructor() { var documentTypeId = Guid.NewGuid(); var id = 1; var key = new DocumentKey(documentTypeId, id); var ecaDocument = new ECADocument(); ecaDocument.SetKey(key); var response = new DocumentSearchResponse <ECADocument>(); response.Count = 1; response.Coverage = 2; var searchResult = new SearchResult <ECADocument>(); searchResult.Document = ecaDocument; searchResult.Highlights = new HitHighlights(); searchResult.Score = 3; response.Results.Add(searchResult); var model = new DocumentSearchResponseViewModel(response); Assert.AreEqual(response.Count, model.Count); Assert.AreEqual(response.Coverage, model.Coverage); Assert.AreEqual(1, response.Results.Count); var firstResult = response.Results.First(); Assert.AreEqual(searchResult.Score, firstResult.Score); Assert.IsTrue(Object.ReferenceEquals(ecaDocument, searchResult.Document)); Assert.IsTrue(Object.ReferenceEquals(searchResult.Highlights, firstResult.Highlights)); }
protected void TestDefaultSearchModeIsAny() { SearchIndexClient client = GetClientForQuery(); DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("Cheapest hotel"); AssertContainsKeys(response, "1", "2", "3"); }
private void AssertKeySequenceEqual(DocumentSearchResponse response, params string[] expectedKeys) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Results); IEnumerable <string> actualKeys = response.Results.Select(r => (string)r.Document["hotelId"]); SearchAssert.SequenceEqual(expectedKeys, actualKeys); }
public void DefaultSearchModeIsAny() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("Cheapest hotel"); AssertContainsKeys(response, "1", "2", "3"); }); }
private void AssertContainsKeys(DocumentSearchResponse response, params string[] expectedKeys) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Results); IEnumerable <string> actualKeys = response.Results.Select(r => (string)r.Document["hotelId"]); foreach (string expectedKey in expectedKeys) { Assert.Contains(expectedKey, actualKeys); } }
protected void TestCanSearchWithMinimumCoverage() { SearchIndexClient client = GetClientForQuery(); var parameters = new SearchParameters() { MinimumCoverage = 50 }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*", parameters); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(100, response.Coverage); }
/// <summary> /// Creates a new instance. /// </summary> /// <param name="response">The azure search document search response.</param> public DocumentSearchResponseViewModel(DocumentSearchResponse <ECADocument> response) { Contract.Requires(response != null, "The response must not be null."); this.Count = response.Count; this.Coverage = response.Coverage; this.Results = new List <SearchResultViewModel>(); var facetResults = response.Facets; foreach (var result in response.Results) { this.Results.Add(new SearchResultViewModel(result)); } }
protected void TestCanSearchWithSearchModeAll() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { SearchMode = SearchMode.All }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("Cheapest hotel", searchParameters); AssertKeySequenceEqual(response, "2"); }
public ICollection <Base> Search(string query) { var resultSearch = new Collection <Base>(); var searchParameters = new SearchParameters(); DocumentSearchResponse <Base> response = indexClient.Documents.Search <Base>(query, searchParameters); foreach (var result in response) { resultSearch.Add(result.Document); } return(resultSearch); }
protected void TestSearchWithoutOrderBySortsByScore() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { Filter = "baseRate gt 190" }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("surprisingly expensive hotel", searchParameters); AssertKeySequenceEqual(response, "6", "1"); Assert.True(response.Results[0].Score > response.Results[1].Score); }
public void CanSearchWithRangeFacets() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); var searchParameters = new SearchParameters() { Facets = new[] { "baseRate,values:80|150|200", "lastRenovationDate,values:2000-01-01T00:00:00Z" } }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*", searchParameters); AssertContainsKeys(response, Data.TestDocuments.Select(d => d.HotelId).ToArray()); Assert.NotNull(response.Facets); RangeFacetResult <double>[] baseRateFacets = GetRangeFacetsForField <double>(response.Facets, "baseRate", 4); Assert.False(baseRateFacets[0].From.HasValue); Assert.Equal(80.0, baseRateFacets[0].To); Assert.Equal(80.0, baseRateFacets[1].From); Assert.Equal(150.0, baseRateFacets[1].To); Assert.Equal(150.0, baseRateFacets[2].From); Assert.Equal(200.0, baseRateFacets[2].To); Assert.Equal(200.0, baseRateFacets[3].From); Assert.False(baseRateFacets[3].To.HasValue); Assert.Equal(1, baseRateFacets[0].Count); Assert.Equal(3, baseRateFacets[1].Count); Assert.Equal(1, baseRateFacets[2].Count); Assert.Equal(1, baseRateFacets[3].Count); RangeFacetResult <DateTimeOffset>[] lastRenovationDateFacets = GetRangeFacetsForField <DateTimeOffset>(response.Facets, "lastRenovationDate", 2); Assert.False(lastRenovationDateFacets[0].From.HasValue); Assert.Equal(new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero), lastRenovationDateFacets[0].To); Assert.Equal(new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero), lastRenovationDateFacets[1].From); Assert.False(lastRenovationDateFacets[1].To.HasValue); Assert.Equal(3, lastRenovationDateFacets[0].Count); Assert.Equal(2, lastRenovationDateFacets[1].Count); }); }
protected void TestCanGetResultCountInSearch() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { IncludeTotalResultCount = true }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*", searchParameters); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Results); Assert.Equal(Data.TestDocuments.Length, response.Count); }
public void CanSearchWithSearchModeAll() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); var searchParameters = new SearchParameters() { SearchMode = SearchMode.All }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("Cheapest hotel", searchParameters); AssertKeySequenceEqual(response, "2"); }); }
protected void TestCanFilter() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { Filter = "rating gt 3 and lastRenovationDate gt 2000-01-01T00:00:00Z" }; // Also test that searchText can be null. DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>(null, searchParameters); AssertKeySequenceEqual(response, "1", "5"); }
public void CanSearchWithMinimumCoverage() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); var parameters = new SearchParameters() { MinimumCoverage = 50 }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*", parameters); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(100, response.Coverage); }); }
private void Search() { IsBusy = true; // a search string is required. if it's empty, set it to "all" if (string.IsNullOrWhiteSpace(SearchString)) { SearchString = "*"; } // create our search client var client = new AzureSearchClient(); // after we load the search results & set the facet collections, two way binding will clear the currently selected facet. // we'll store these temporarily and reapply them later var sourceFacet = SourceFacet; var retweetsFacet = RetweetsFacet; var followingFacet = FollowingFacet; var followersFacet = FollowersFacet; DocumentSearchResponse <MarchMadnessTweet> searchResult = null; var executionTime = string.Empty; Task.Run(async delegate { var start = DateTime.Now; searchResult = await client.Search <MarchMadnessTweet>(SearchString, SearchField, SourceFacet, RetweetsFacet, FollowingFacet, FollowersFacet); var end = DateTime.Now; executionTime = (end.Subtract(start)).TotalMilliseconds.ToString("#####.##") + " milliseconds"; }).Wait(); SearchExecutionTime = executionTime; Tweets = searchResult.Results.ToObservableCollection(); SourceFacets = searchResult.Facets["source"].Where(f => f.Count > 0).ToObservableCollection(); RetweetsFacets = searchResult.Facets["retweets"].Where(f => f.Count > 0).ToObservableCollection(); FollowingFacets = searchResult.Facets["following"].Where(f => f.Count > 0).ToObservableCollection(); FollowersFacets = searchResult.Facets["followers"].Where(f => f.Count > 0).ToObservableCollection(); SourceFacet = sourceFacet; RetweetsFacet = retweetsFacet; FollowingFacet = followingFacet; FollowersFacet = followersFacet; IsBusy = false; }
protected void TestCanUseTopAndSkipForClientSidePaging() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { Top = 3, Skip = 0, OrderBy = new[] { "hotelId" } }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*", searchParameters); AssertKeySequenceEqual(response, "1", "2", "3"); searchParameters.Skip = 3; response = client.Documents.Search <Hotel>("*", searchParameters); AssertKeySequenceEqual(response, "4", "5", "6"); }
protected void TestSearchWithScoringProfileBoostsScore() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { ScoringProfile = "nearest", ScoringParameters = new[] { "myloc:-122,49" }, Filter = "rating eq 5 or rating eq 1" }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("hotel", searchParameters); AssertKeySequenceEqual(response, "2", "1"); }
private static void SearchDocuments(SearchIndexClient indexClient, string searchText, string filter = null) { // Execute search based on search text and optional filter var sp = new SearchParameters(); if (!String.IsNullOrEmpty(filter)) { sp.Filter = filter; } DocumentSearchResponse <Hotel> response = indexClient.Documents.Search <Hotel>(searchText, sp); foreach (SearchResult <Hotel> result in response) { Console.WriteLine(result.Document); } }
protected void TestCanSearchWithDateTimeInStaticModel() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = new Index() { Name = TestUtilities.GenerateName(), Fields = new[] { new Field("ISBN", DataType.String) { IsKey = true }, new Field("Title", DataType.String) { IsSearchable = true }, new Field("Author", DataType.String), new Field("PublishDate", DataType.DateTimeOffset) } }; IndexDefinitionResponse createIndexResponse = serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name); var doc1 = new Book() { ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien" }; var doc2 = new Book() { ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18) }; var batch = IndexBatch.Create(IndexAction.Create(doc1), IndexAction.Create(doc2)); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); DocumentSearchResponse <Book> response = indexClient.Documents.Search <Book>("War and Peace"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(1, response.Results.Count); Assert.Equal(doc2, response.Results[0].Document); }
public ActionResult Search(string q = "", bool h = false, bool f = false, bool s = false, string fi = "", string sp = "") { //Create an object array to return object[] searchresults = new object[2]; //Get the JsonResult JsonResult resultsdata = new JsonResult(); resultsdata.JsonRequestBehavior = JsonRequestBehavior.AllowGet; resultsdata.Data = _AzureSearch.Search(q + "*", h, f, s, fi, sp); //Get the Facets section. This value is not accessible via the JsonResult object so we need to create a DocumentSearchResponse to retrieve it. DocumentSearchResponse dsrfacets = (DocumentSearchResponse)resultsdata.Data; searchresults[0] = resultsdata.Data; searchresults[1] = dsrfacets.Facets; //Return the array return(Json(searchresults)); }
public void CanUseHitHighlighting() { const string Description = "description"; const string Category = "category"; Run(() => { SearchIndexClient client = Data.GetSearchIndexClientForQuery(); var searchParameters = new SearchParameters() { Filter = "rating eq 5", HighlightPreTag = "<b>", HighlightPostTag = "</b>" }; // Try using the collection without initializing it to make sure it is lazily initialized. searchParameters.HighlightFields.Add(Description); searchParameters.HighlightFields.Add(Category); DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("luxury hotel", searchParameters); AssertKeySequenceEqual(response, "1"); HitHighlights highlights = response.Results[0].Highlights; Assert.NotNull(highlights); SearchAssert.SequenceEqual(new[] { Description, Category }, highlights.Keys); string categoryHighlight = highlights[Category].Single(); Assert.Equal("<b>Luxury</b>", categoryHighlight); string[] expectedDescriptionHighlights = new[] { "Best <b>hotel</b> in town if you like <b>luxury</b> hotels.", "We highly recommend this <b>hotel</b>." }; SearchAssert.SequenceEqual(expectedDescriptionHighlights, highlights[Description]); }); }
public static void SearchDocuments(SearchIndexClient indexClient, string searchText) { // Search using the supplied searchText and output documents that match try { var sp = new SearchParameters(); DocumentSearchResponse <OCRTextIndex> response = indexClient.Documents.Search <OCRTextIndex>(searchText, sp); foreach (SearchResult <OCRTextIndex> result in response) { Console.WriteLine("File ID: {0}", result.Document.fileId); Console.WriteLine("File Name: {0}", result.Document.fileName); Console.WriteLine("Extracted Text: {0}", result.Document.ocrText); } } catch (Exception e) { Console.WriteLine("Failed search: {0}", e.Message.ToString()); } }
protected void TestOrderByProgressivelyBreaksTies() { SearchIndexClient client = GetClientForQuery(); var searchParameters = new SearchParameters() { OrderBy = new string[] { "rating desc", "lastRenovationDate asc", "geo.distance(location, geography'POINT(-122.0 49.0)')" } }; DocumentSearchResponse <Hotel> response = client.Documents.Search <Hotel>("*", searchParameters); AssertKeySequenceEqual(response, "1", "4", "3", "5", "2", "6"); }
protected void TestCanSearchDynamicDocuments() { SearchIndexClient client = GetClientForQuery(); DocumentSearchResponse response = client.Documents.Search("*"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Null(response.ContinuationToken); Assert.Null(response.Count); Assert.Null(response.Coverage); Assert.Null(response.Facets); Assert.NotNull(response.Results); Assert.Equal(Data.TestDocuments.Length, response.Results.Count); for (int i = 0; i < response.Results.Count; i++) { Assert.Equal(1, response.Results[i].Score); Assert.Null(response.Results[i].Highlights); SearchAssert.DocumentsEqual(Data.TestDocuments[i].AsDocument(), response.Results[i].Document); } }
protected void TestCanContinueSearchForDynamicDocuments() { SearchIndexClient client = GetClient(); IEnumerable <string> hotelIds = IndexDocuments(client, 2001); var searchParameters = new SearchParameters() { Top = 3000, OrderBy = new[] { "hotelId asc" }, Select = new[] { "hotelId" } }; IEnumerable <string> expectedIds = Data.TestDocuments.Select(d => d.HotelId).Concat(hotelIds).OrderBy(id => id); DocumentSearchResponse response = client.Documents.Search("*", searchParameters); AssertKeySequenceEqual(response, expectedIds.Take(1000).ToArray()); Assert.NotNull(response.ContinuationToken); // Test that ContinueSearch still works even if you toggle GET/POST. client.UseHttpGetForQueries = !client.UseHttpGetForQueries; response = client.Documents.ContinueSearch(response.ContinuationToken); AssertKeySequenceEqual(response, expectedIds.Skip(1000).Take(1000).ToArray()); Assert.NotNull(response.ContinuationToken); // Toggle GET/POST again. client.UseHttpGetForQueries = !client.UseHttpGetForQueries; response = client.Documents.ContinueSearch(response.ContinuationToken); AssertKeySequenceEqual(response, expectedIds.Last()); Assert.Null(response.ContinuationToken); }