示例#1
0
        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");
        }
示例#2
0
        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);
        }
示例#3
0
        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));
        }
示例#4
0
        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);
        }
示例#5
0
        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));
        }
示例#6
0
        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" });
        }
示例#7
0
        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"]);
        }
示例#8
0
        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);
        }
示例#9
0
        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);
        }
示例#10
0
        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");
        }
示例#11
0
        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);
        }
示例#12
0
        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);
        }
示例#13
0
        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" });
        }
示例#14
0
        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" });
        }
示例#15
0
        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);
        }
示例#16
0
        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);
        }
示例#17
0
        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);
        }
示例#18
0
        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>());
        }
示例#19
0
        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);
        }
示例#20
0
        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);
        }
示例#21
0
        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);
        }
示例#22
0
        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);
        }
示例#23
0
        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);
        }
示例#24
0
        public async Task SizeTrimsResults()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "po",
                    "sg",
                    new AutocompleteOptions
            {
                Mode = AutocompleteMode.OneTerm,
                Size = 2
            }),
                new[] { "point", "police" },
                new[] { "point", "police" });
        }
示例#25
0
        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");
        }
示例#26
0
        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);
        }
示例#27
0
        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" });
        }
示例#28
0
        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" });
        }
示例#29
0
        public async Task ExcludesFieldsNotInSuggester()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "luxu",
                    "sg",
                    new AutocompleteOptions
            {
                Mode         = AutocompleteMode.OneTerm,
                SearchFields = new[] { "hotelName" }
            }),
                Enumerable.Empty <string>(),
                Enumerable.Empty <string>());
        }
示例#30
0
        public async Task MultipleSelectedFields()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "mod",
                    "sg",
                    new AutocompleteOptions
            {
                Mode         = AutocompleteMode.OneTerm,
                SearchFields = new[] { "hotelName", "description" }
            }),
                new[] { "model", "modern" },
                new[] { "model", "modern" });
        }