예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #4
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");
        }
예제 #5
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);
        }
예제 #6
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" });
        }
예제 #7
0
        public async Task TwoTermsWithFuzzy()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "mod",
                    "sg",
                    new AutocompleteOptions
            {
                Mode             = AutocompleteMode.TwoTerms,
                UseFuzzyMatching = true
            }),
                new[] { "model suites", "modern architecture", "modern stay", "morel coverings", "motel" },
                new[] { "model suites", "modern architecture", "modern stay", "morel coverings", "motel" });
        }
예제 #8
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>());
        }
예제 #9
0
        public async Task OneTermWithContextWithFuzzy()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "very polit",
                    "sg",
                    new AutocompleteOptions
            {
                Mode             = AutocompleteMode.OneTermWithContext,
                UseFuzzyMatching = true
            }),
                new[] { "very polite", "very police" },
                new[] { "very polite", "very police" });
        }
예제 #10
0
        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);
        }
예제 #11
0
        public async Task OneTermWithFuzzy()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "mod",
                    "sg",
                    new AutocompleteOptions
            {
                Mode             = AutocompleteMode.OneTerm,
                UseFuzzyMatching = true
            }),
                new[] { "model", "modern", "morel", "motel" },
                new[] { "model", "modern", "morel", "motel" });
        }
예제 #12
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" });
        }
예제 #13
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" });
        }
예제 #14
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" });
        }
예제 #15
0
        public async Task FilterAndFuzzy()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "mod",
                    "sg",
                    new AutocompleteOptions
            {
                Mode             = AutocompleteMode.OneTerm,
                UseFuzzyMatching = true,
                Filter           = "hotelId ne '6' and (hotelName eq 'Modern Stay' or tags/any(t : t eq 'budget'))"
            }),
                new[] { "modern", "motel" },
                new[] { "modern", "motel" });
        }
예제 #16
0
        public async Task Filter()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Response <SearchResults <Hotel> > response =
                await resources.GetQueryClient().SearchAsync <Hotel>(
                    null,
                    new SearchOptions
            {
                Filter = "rating gt 3 and lastRenovationDate gt 2000-01-01T00:00:00Z"
            });

            await AssertKeysEqual(
                response,
                h => h.Document.HotelId,
                "1", "5");
        }
예제 #17
0
        public async Task Filter()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SuggestResults <Hotel> suggestions =
                await resources.GetQueryClient().SuggestAsync <Hotel>(
                    "hotel",
                    "sg",
                    new SuggestOptions
            {
                Filter = "rating gt 3 and lastRenovationDate gt 2000-01-01T00:00:00Z",
                // Use OrderBy so changes in ranking don't break the test
                OrderBy = new[] { "hotelId" }
            });

            AssertContainsIds(suggestions, "1", "5");
        }
예제 #18
0
        public async Task ThrowsWhenRequestIsMalformed()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            RequestFailedException ex = await CatchAsync <RequestFailedException>(
                async() => await resources.GetQueryClient().SuggestAsync(
                    "hotel",
                    "sg",
                    new SuggestOptions {
                OrderBy = new[] { "This is not a valid orderby." }
            }));

            Assert.AreEqual(400, ex.Status);
            StringAssert.StartsWith(
                "Invalid expression: Syntax error at position 7 in 'This is not a valid orderby.'",
                ex.Message);
        }
예제 #19
0
        public async Task VerifyRoundtrip <T>(
            Func <T, string> getKey,
            T document,
            T expected = default,
            GetDocumentOptions options = null)
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            await resources.GetIndexClient().IndexDocumentsAsync <T>(
                IndexDocumentsBatch.Upload <T>(new[] { document }));

            await resources.WaitForIndexingAsync();

            Response <T> response = await resources.GetQueryClient().GetDocumentAsync <T>(getKey(document), options);

            // Only validate expected properties
            AssertApproximate(expected ?? document, response.Value);
        }
예제 #20
0
        public async Task HitHighlighting()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            VerifyCompletions(
                await resources.GetQueryClient().AutocompleteAsync(
                    "po",
                    "sg",
                    new AutocompleteOptions
            {
                Mode             = AutocompleteMode.OneTerm,
                Filter           = "hotelName eq 'EconoStay' or hotelName eq 'Fancy Stay'",
                HighlightPreTag  = "<b>",
                HighlightPostTag = "</b>",
            }),
                new[] { "pool", "popular" },
                new[] { "<b>pool</b>", "<b>popular</b>" });
        }
예제 #21
0
        public async Task ThrowsWhenBadSuggesterName()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            string invalidName        = "Invalid suggester";
            RequestFailedException ex = await CatchAsync <RequestFailedException>(
                async() => await resources.GetQueryClient().AutocompleteAsync(
                    "very po",
                    invalidName,
                    new AutocompleteOptions {
                Mode = AutocompleteMode.OneTerm
            }));

            Assert.AreEqual(400, ex.Status);
            StringAssert.StartsWith(
                $"The specified suggester name '{invalidName}' does not exist in this index definition.",
                ex.Message);
        }
예제 #22
0
        public async Task SelectedFields()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Response <SearchResults <Hotel> > response =
                await resources.GetQueryClient().SearchAsync <Hotel>(
                    "fancy luxury secret",
                    new SearchOptions
            {
                SearchFields = new[] { "category", "hotelName" },
                Select       = new[] { "hotelName", "rating", "address/city", "rooms/type" }
            });

            Hotel[] expectedDocs =
                new[]
            {
                new Hotel {
                    HotelName = "Fancy Stay", Rating = 5
                },
                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"
                        }
                    }
                }
            };
            List <SearchResult <Hotel> > actualDocs =
                await response.Value.GetResultsAsync().ToListAsync();

            CollectionAssert.AreEqual(
                expectedDocs.OrderBy(h => h.HotelName),
                actualDocs.Select(d => d.Document).OrderBy(h => h.HotelName));
        }
예제 #23
0
        public async Task HitHighlighting()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SuggestResults <Hotel> suggestions =
                await resources.GetQueryClient().SuggestAsync <Hotel>(
                    "hotel",
                    "sg",
                    new SuggestOptions
            {
                HighlightPreTag  = "<b>",
                HighlightPostTag = "</b>",
                Filter           = "category eq 'Luxury'",
                Size             = 1
            });

            AssertContainsIds(suggestions, "1");
            StringAssert.StartsWith("Best <b>hotel</b> in town", suggestions.Results.First().Text);
        }
예제 #24
0
        public async Task IndexSharesPipeline()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            TestPipelinePolicy custom = new TestPipelinePolicy();

            Assert.AreEqual(0, custom.RequestCount);

            SearchClientOptions options = new SearchClientOptions(ServiceVersion);

            options.AddPolicy(custom, HttpPipelinePosition.PerCall);
            SearchServiceClient client = resources.GetServiceClient(options);

            SearchIndexClient index = client.GetSearchIndexClient(resources.IndexName);

            _ = await index.GetDocumentCountAsync();

            Assert.AreEqual(1, custom.RequestCount);
        }
예제 #25
0
        public async Task SearchModeAll()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Response <SearchResults <Hotel> > response =
                await resources.GetQueryClient().SearchAsync <Hotel>(
                    "Cheapest hotel",
                    new SearchOptions
            {
                // Set explicitly at least once for test coverage
                QueryType  = SearchQueryType.Simple,
                SearchMode = SearchMode.All
            });

            await AssertKeysEqual(
                response,
                h => h.Document.HotelId,
                "2");
        }
예제 #26
0
        public async Task OrderByProgressivelyBreaksTies()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SuggestResults <Hotel> suggestions =
                await resources.GetQueryClient().SuggestAsync <Hotel>(
                    "hotel",
                    "sg",
                    new SuggestOptions
            {
                OrderBy = new string[]
                {
                    "rating desc",
                    "lastRenovationDate asc",
                    "geo.distance(location, geography'POINT(-122.0 49.0)')"
                }
            });

            AssertContainsIds(suggestions, "1", "9", "4", "3", "5");
        }
예제 #27
0
        public async Task NoOrderBySortsByScore()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Response <SearchResults <Hotel> > response =
                await resources.GetQueryClient().SearchAsync <Hotel>(
                    "cheapest hotel in town",
                    new SearchOptions {
                Filter = "rating lt 4"
            });

            await AssertKeysEqual(
                response,
                h => h.Document.HotelId,
                "2", "10");

            List <SearchResult <Hotel> > docs = await response.Value.GetResultsAsync().ToListAsync();

            Assert.Greater(docs[0].Score, docs[1].Score);
        }
예제 #28
0
        public async Task Structs()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SimpleStructHotel document = new SimpleStructHotel
            {
                HotelId   = "4",
                HotelName = "Value Inn"
            };

            await resources.GetIndexClient().IndexDocumentsAsync(
                IndexDocumentsBatch.Upload(new[] { document }));

            await resources.WaitForIndexingAsync();

            SearchIndexClient            client   = resources.GetQueryClient();
            Response <SimpleStructHotel> response = await client.GetDocumentAsync <SimpleStructHotel>(document.HotelId);

            Assert.AreEqual(document, response.Value);
        }
예제 #29
0
        public async Task HitHighlighting()
        {
            const string Description = "description";
            const string Category    = "category";

            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Response <SearchResults <Hotel> > response =
                await resources.GetQueryClient().SearchAsync <Hotel>(
                    "luxury hotel",
                    new SearchOptions
            {
                Filter           = "rating eq 5",
                HighlightPreTag  = "<b>",
                HighlightPostTag = "</b>",
                HighlightFields  = new[] { Description, Category }
            });

            await AssertKeysEqual(
                response,
                h => h.Document.HotelId,
                "1");

            SearchResult <Hotel> doc = await response.Value.GetResultsAsync().FirstAsync();

            Assert.IsNotNull(doc.Highlights);
            Assert.AreEqual(2, doc.Highlights.Count);
            CollectionAssert.Contains(doc.Highlights.Keys, Description);
            CollectionAssert.Contains(doc.Highlights.Keys, Category);

            Assert.AreEqual(
                "<b>Luxury</b>",
                doc.Highlights[Category].Single());
            CollectionAssert.AreEqual(
                new[]
            {
                "Best <b>hotel</b> in town if you like <b>luxury</b> <b>hotels</b>.",
                "We highly recommend this <b>hotel</b>."
            },
                doc.Highlights[Description]);
        }
예제 #30
0
        public async Task StaticDocuments()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient     client   = resources.GetQueryClient();
            SearchResults <Hotel> response = await client.SearchAsync <Hotel>("*");

            Assert.IsNull(response.TotalCount);
            Assert.IsNull(response.Coverage);
            Assert.IsNull(response.Facets);

            List <SearchResult <Hotel> > docs = await response.GetResultsAsync().ToListAsync();

            Assert.AreEqual(SearchResources.TestDocuments.Length, docs.Count);
            for (int i = 0; i < docs.Count; i++)
            {
                Assert.AreEqual(1, docs[i].Score);
                Assert.IsNull(docs[i].Highlights);
                Assert.AreEqual(SearchResources.TestDocuments[i], docs[i].Document);
            }
        }