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);
        }
Exemple #2
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");
        }
        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));
        }
Exemple #4
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));
        }
Exemple #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" });
        }
        public async Task GetDocumentCount()
        {
            await using SearchResources search = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient    client   = search.GetSearchClient();
            Response <long> response = await client.GetDocumentCountAsync();

            Assert.AreEqual(200, response.GetRawResponse().Status);
            Assert.AreEqual(SearchResources.TestDocuments.Length, response.Value);
        }
        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 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 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);
        }
Exemple #11
0
        public async Task ClientRequestIdRountrips()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

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

            Assert.AreEqual(id.ToString(), response.GetRawResponse().ClientRequestId);
        }
Exemple #12
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");
        }
Exemple #13
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);
        }
Exemple #14
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);
        }
Exemple #15
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);
        }
Exemple #16
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);
        }
Exemple #17
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" });
        }
        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);
        }
Exemple #19
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>());
        }
        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);
        }
Exemple #21
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" });
        }
Exemple #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);
        }
        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);
        }
Exemple #24
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);
        }
        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 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 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 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);
        }
Exemple #29
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" });
        }
Exemple #30
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" });
        }