Exemplo n.º 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");
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
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" });
        }
Exemplo n.º 6
0
        public async Task GetDocumentCount()
        {
            await using SearchResources search = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient client   = search.GetIndexClient();
            Response <long>   response = await client.GetDocumentCountAsync();

            Assert.AreEqual(200, response.GetRawResponse().Status);
            Assert.AreEqual(SearchResources.TestDocuments.Length, response.Value);
        }
Exemplo n.º 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"]);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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");
        }
Exemplo n.º 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);
        }
Exemplo n.º 13
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" });
        }
Exemplo n.º 14
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" });
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 17
0
        public async Task ClientRequestIdRountrips()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient client = resources.GetIndexClient();
            Guid            id       = Recording.Random.NewGuid();
            Response <long> response = await client.GetDocumentCountAsync(
                new SearchRequestOptions { ClientRequestId = id });

            // TODO: #10604 - C# generator doesn't properly support ClientRequestId yet
            // (Assertion is here to remind us to fix this when we do - just
            // change to AreEqual and re-record)
            Assert.AreNotEqual(id.ToString(), response.GetRawResponse().ClientRequestId);
        }
Exemplo n.º 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>());
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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);
        }
Exemplo n.º 21
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);
        }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
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);
        }
Exemplo n.º 24
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>());
        }
Exemplo n.º 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");
        }
Exemplo n.º 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);
        }
Exemplo n.º 27
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" });
        }
Exemplo n.º 28
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" });
        }
Exemplo n.º 29
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" });
        }
Exemplo n.º 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" });
        }