public async Task SizeAndSkipForPaging() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchOptions options = new SearchOptions { Skip = 0, Size = 3, OrderBy = new[] { "hotelId" } }; Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>("*", options); await AssertKeysEqual( response, h => h.Document.HotelId, "1", "10", "2"); options.Skip = 3; response = await resources.GetQueryClient().SearchAsync <Hotel>("*", options); await AssertKeysEqual( response, h => h.Document.HotelId, "3", "4", "5"); }
public async Task PagingWithoutSize() { const int size = 167; await using SearchResources resources = await CreateLargeHotelsIndexAsync(size); Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>( "*", new SearchOptions { OrderBy = new[] { "hotelId asc" }, Select = new[] { "hotelId" } }); List <string> ids = new List <string>(); await foreach (Page <SearchResult <Hotel> > page in response.Value.GetResultsAsync().AsPages()) { Assert.LessOrEqual(page.Values.Count, 50); ids.AddRange(page.Values.Select(d => d.Document.HotelId)); } CollectionAssert.AreEquivalent( Enumerable.Range(1, size).Select(i => i.ToString()), ids); }
public async Task SelectedFields() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>( "secret", "sg", new SuggestOptions { Select = new[] { "hotelName", "rating", "address/city", "rooms/type" } }); var expected = new Hotel { HotelName = "Secret Point Motel", Rating = 4, Address = new HotelAddress() { City = "New York" }, Rooms = new[] { new HotelRoom() { Type = "Budget Room" }, new HotelRoom() { Type = "Budget Room" } } }; Assert.AreEqual(1, suggestions.Results.Count); Assert.AreEqual(expected, suggestions.Results.First().Document); }
public async Task SuggestStaticDocuments() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>( "more", "sg", new SuggestOptions { OrderBy = new[] { "hotelId" } }); IEnumerable <Hotel> expected = SearchResources.TestDocuments .Where(h => h.HotelId == "8" || h.HotelId == "10") .OrderBy(h => h.HotelId); Assert.Null(suggestions.Coverage); Assert.NotNull(suggestions.Results); CollectionAssert.AreEqual( expected.Select(h => h.HotelId), suggestions.Results.Select(r => r.Document.HotelId)); CollectionAssert.AreEqual( expected.Select(h => h.Description), suggestions.Results.Select(r => r.Text)); }
public async Task RangeFacets() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>( "*", new SearchOptions { Facets = new[] { "rooms/baseRate,values:5|8|10", "lastRenovationDate,values:2000-01-01T00:00:00Z" } }); await AssertKeysContains( response, h => h.Document.HotelId, SearchResources.TestDocuments.Select(h => h.HotelId).ToArray()); Assert.IsNotNull(response.Value.Facets); AssertFacetsEqual( GetFacetsForField(response.Value.Facets, "rooms/baseRate", 4), MakeRangeFacet(count: 1, from: null, to: 5.0), MakeRangeFacet(count: 1, from: 5.0, to: 8.0), MakeRangeFacet(count: 1, from: 8.0, to: 10.0), MakeRangeFacet(count: 0, from: 10.0, to: null)); AssertFacetsEqual( GetFacetsForField(response.Value.Facets, "lastRenovationDate", 2), MakeRangeFacet(count: 5, from: null, to: new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero)), MakeRangeFacet(count: 2, from: new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero), to: null)); }
public async Task OneTermOnByDefault() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); VerifyCompletions( await resources.GetQueryClient().AutocompleteAsync("po", "sg"), new[] { "point", "police", "polite", "pool", "popular" }, new[] { "point", "police", "polite", "pool", "popular" }); }
public async Task CanContinueWithoutSize() { const int size = 167; await using SearchResources resources = await CreateLargeHotelsIndexAsync(size); SearchIndexClient client = resources.GetQueryClient(); Response <SearchResults <SearchDocument> > response = await client.SearchAsync( "*", new SearchOptions { OrderBy = new[] { "hotelId asc" }, Select = new[] { "hotelId" } }); List <string> ids = new List <string>(); // Get the first page Page <SearchResult <SearchDocument> > page = await response.Value.GetResultsAsync().AsPages().FirstAsync(); Assert.LessOrEqual(page.Values.Count, 50); ids.AddRange(page.Values.Select(d => (string)d.Document["hotelId"])); Assert.NotNull(page.ContinuationToken); // Get the second page response = await client.SearchAsync(null, new SearchOptions(page.ContinuationToken)); page = await response.Value.GetResultsAsync().AsPages().FirstAsync(); Assert.LessOrEqual(page.Values.Count, 50); ids.AddRange(page.Values.Select(d => (string)d.Document["hotelId"])); Assert.NotNull(page.ContinuationToken); // Get the third page response = await client.SearchAsync(null, new SearchOptions(page.ContinuationToken)); page = await response.Value.GetResultsAsync().AsPages().FirstAsync(); Assert.LessOrEqual(page.Values.Count, 50); ids.AddRange(page.Values.Select(d => (string)d.Document["hotelId"])); Assert.NotNull(page.ContinuationToken); // Get the final page response = await client.SearchAsync(null, new SearchOptions(page.ContinuationToken)); page = await response.Value.GetResultsAsync().AsPages().FirstAsync(); Assert.LessOrEqual(page.Values.Count, 50); ids.AddRange(page.Values.Select(d => (string)d.Document["hotelId"])); Assert.IsNull(page.ContinuationToken); // Verify we saw everything CollectionAssert.AreEquivalent( Enumerable.Range(1, size).Select(i => i.ToString()), ids); }
public async Task ThrowsWhenNotFound() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await client.GetDocumentAsync("ThisDocumentDoesNotExist")); Assert.AreEqual(404, ex.Status); }
public async Task GetDocumentStatic() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); Response <Hotel> response = await client.GetDocumentAsync <Hotel>("3"); Assert.AreEqual(200, response.GetRawResponse().Status); Assert.AreEqual("3", response.Value.HotelId); }
public async Task GetDocumentDict() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); Response <SearchDocument> response = await client.GetDocumentAsync("3"); Assert.AreEqual(200, response.GetRawResponse().Status); Assert.AreEqual("3", response.Value["hotelId"]); }
public async Task FuzzyOffByDefault() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>("hitel", "sg"); Assert.NotNull(suggestions); Assert.NotNull(suggestions.Results); Assert.Zero(suggestions.Results.Count); }
public async Task TotalCount() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchOptions options = new SearchOptions { IncludeTotalCount = true }; Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>("*", options); Assert.AreEqual(SearchResources.TestDocuments.Length, response.Value.TotalCount); }
public async Task DefaultSearchModeIsAny() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>("Cheapest hotel"); await AssertKeysContains( response, h => h.Document.HotelId, "1", "2", "3"); }
public async Task MinimumCoverage() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>( "*", new SearchOptions { MinimumCoverage = 50 }); Assert.AreEqual(100, response.Value.Coverage); }
public async Task TwoTerms() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); VerifyCompletions( await resources.GetQueryClient().AutocompleteAsync( "po", "sg", new AutocompleteOptions { Mode = AutocompleteMode.TwoTerms }), new[] { "point motel", "police station", "polite staff", "pool a", "popular hotel" }, new[] { "point motel", "police station", "polite staff", "pool a", "popular hotel" }); }
public async Task ThrowsWhenNoSuggesterName() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await resources.GetQueryClient().AutocompleteAsync( "very po", suggesterName: string.Empty)); Assert.AreEqual(400, ex.Status); StringAssert.StartsWith( "Cannot find fields enabled for suggestions. Please provide a value for 'suggesterName' in the query.", ex.Message); }
public async Task ThrowsWhenRequestIsMalformed() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); SearchOptions invalidOptions = new SearchOptions { Filter = "This is not a valid filter." }; RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await client.SearchAsync("*", invalidOptions)); Assert.AreEqual(400, ex.Status); StringAssert.StartsWith("Invalid expression: Syntax error at position 7 in 'This is not a valid filter.'", ex.Message); }
public async Task FuzzyIsOffByDefault() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); VerifyCompletions( await resources.GetQueryClient().AutocompleteAsync( "pi", "sg", new AutocompleteOptions { Mode = AutocompleteMode.OneTerm }), Enumerable.Empty <string>(), Enumerable.Empty <string>()); }
public async Task OneTermWithContext() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); VerifyCompletions( await resources.GetQueryClient().AutocompleteAsync( "looking for very po", "sg", new AutocompleteOptions { Mode = AutocompleteMode.OneTermWithContext }), new[] { "very police", "very polite", "very popular" }, new[] { "looking for very police", "looking for very polite", "looking for very popular" }); }
public async Task MinimumCoverage() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>( "luxury", "sg", new SuggestOptions { MinimumCoverage = 50 }); Assert.AreEqual(100, suggestions.Coverage); }
public async Task ThrowsWhenGivenBadSuggesterName() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); string invalidName = "Invalid suggester"; RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await resources.GetQueryClient().SuggestAsync( "hotel", invalidName)); Assert.AreEqual(400, ex.Status); StringAssert.StartsWith( $"The specified suggester name '{invalidName}' does not exist in this index definition.", ex.Message); }
public async Task RegexSpecialChars() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); Response <SearchResults <Hotel> > response = await resources.GetQueryClient().SearchAsync <Hotel>( @"/\+\-\&\|\!\(\)\{\}\[\]\^\""\\~\*\?\:\\\//", new SearchOptions { QueryType = SearchQueryType.Full }); List <SearchResult <Hotel> > docs = await response.Value.GetResultsAsync().ToListAsync(); Assert.AreEqual(0, docs.Count); }
public async Task RecentlyIndexedDynamicDocument() { await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this); Hotel document = SearchResources.TestDocuments[0]; await resources.GetIndexClient().IndexDocumentsAsync( IndexDocumentsBatch.Upload(new[] { document.AsDocument() })); await resources.WaitForIndexingAsync(); Response <Hotel> response = await resources.GetQueryClient().GetDocumentAsync <Hotel>(document.HotelId); Assert.AreEqual(document.HotelId, response.Value.HotelId); }
public async Task ExcludeFieldsFromSuggest() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>( "luxury", "sg", new SuggestOptions { SearchFields = new[] { "hotelName" } }); Assert.AreEqual(0, suggestions.Results.Count); }
public async Task RegexSpecialCharsUnescapedThrows() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await client.SearchAsync( @"/.*/.*/", new SearchOptions { QueryType = SearchQueryType.Full })); Assert.AreEqual(400, ex.Status); StringAssert.StartsWith("Failed to parse query string at line 1, column 8.", ex.Message); }
public async Task ThrowsWhenMalformed() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchIndexClient client = resources.GetQueryClient(); RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await client.GetDocumentAsync( "3", new GetDocumentOptions() { SelectedFields = new[] { "ThisFieldDoesNotExist" } })); Assert.AreEqual(400, ex.Status); StringAssert.StartsWith("Invalid expression: Could not find a property named 'ThisFieldDoesNotExist' on type 'search.document'.", ex.Message); }
public async Task Filter() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); VerifyCompletions( await resources.GetQueryClient().AutocompleteAsync( "po", "sg", new AutocompleteOptions { Mode = AutocompleteMode.OneTerm, Filter = "search.in(hotelId, '6,7')" }), new[] { "polite" }, new[] { "polite" }); }
public async Task SizeTrimsResults() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>( "hotel", "sg", new SuggestOptions { OrderBy = new string[] { "hotelId" }, Size = 3 }); AssertContainsIds(suggestions, "1", "10", "2"); }
public async Task Fuzzy() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SuggestResults <Hotel> suggestions = await resources.GetQueryClient().SuggestAsync <Hotel>( "hitel", "sg", new SuggestOptions { UseFuzzyMatching = true }); Assert.NotNull(suggestions); Assert.NotNull(suggestions.Results); Assert.AreEqual(5, suggestions.Results.Count); }
public async Task StaticallyTypedDocuments() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); VerifyCompletions( await resources.GetQueryClient().AutocompleteAsync( "very po", "sg", new AutocompleteOptions { Mode = AutocompleteMode.OneTerm, UseFuzzyMatching = false }), new[] { "point", "police", "polite", "pool", "popular" }, new[] { "very point", "very police", "very polite", "very pool", "very popular" }); }