static SearchResultMapper()
        {
            var mapperConfiguration = new MapperConfiguration(configuration =>
            {
                configuration.CreateMap <IDictionary <string, IList <string> >, HitHighlights>()
                .ConvertUsing((searchHighlights, highlights) =>
                {
                    if (searchHighlights == null)
                    {
                        return(null);
                    }

                    highlights = new HitHighlights();
                    foreach (var key in searchHighlights.Keys)
                    {
                        highlights.Add(key, searchHighlights[key]);
                    }

                    return(highlights);
                });
                configuration.CreateMap <Azure.Search.Documents.Models.SearchResult <Accommodation>, SearchResult>();
                configuration.CreateMap <Azure.Search.Documents.Models.SearchSuggestion <Accommodation>, SuggestResult>();
            });

            mapperConfiguration.AssertConfigurationIsValid();
            Mapper = mapperConfiguration.CreateMapper();
        }
Exemplo n.º 2
0
        public void CanUseHitHighlighting()
        {
            const string Description = "description";
            const string Category    = "category";

            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClientForQuery();

                var searchParameters =
                    new SearchParameters()
                {
                    Filter           = "rating eq 5",
                    HighlightPreTag  = "<b>",
                    HighlightPostTag = "</b>"
                };

                // Try using the collection without initializing it to make sure it is lazily initialized.
                searchParameters.HighlightFields.Add(Description);
                searchParameters.HighlightFields.Add(Category);

                DocumentSearchResponse <Hotel> response =
                    client.Documents.Search <Hotel>("luxury hotel", searchParameters);

                AssertKeySequenceEqual(response, "1");

                HitHighlights highlights = response.Results[0].Highlights;
                Assert.NotNull(highlights);
                SearchAssert.SequenceEqual(new[] { Description, Category }, highlights.Keys);

                string categoryHighlight = highlights[Category].Single();
                Assert.Equal("<b>Luxury</b>", categoryHighlight);

                string[] expectedDescriptionHighlights =
                    new[]
                {
                    "Best <b>hotel</b> in town if you like <b>luxury</b> hotels.",
                    "We highly recommend this <b>hotel</b>."
                };

                SearchAssert.SequenceEqual(expectedDescriptionHighlights, highlights[Description]);
            });
        }
Exemplo n.º 3
0
        protected void TestCanUseHitHighlighting()
        {
            const string Description = "description";
            const string Category    = "category";

            SearchIndexClient client = GetClientForQuery();

            var searchParameters =
                new SearchParameters()
            {
                Filter           = "rating eq 5",
                HighlightPreTag  = "<b>",
                HighlightPostTag = "</b>",
                HighlightFields  = new[] { Description, Category }
            };

            DocumentSearchResult <Hotel> response =
                client.Documents.Search <Hotel>("luxury hotel", searchParameters);

            AssertKeySequenceEqual(response, "1");

            HitHighlights highlights = response.Results[0].Highlights;

            Assert.NotNull(highlights);
            Assert.Equal(2, highlights.Keys.Count);
            Assert.Contains(Description, highlights.Keys);
            Assert.Contains(Category, highlights.Keys);

            string categoryHighlight = highlights[Category].Single();

            Assert.Equal("<b>Luxury</b>", categoryHighlight);

            // Typed as IEnumerable so we get the right overload of Assert.Equals below.
            IEnumerable <string> expectedDescriptionHighlights =
                new[]
            {
                "Best <b>hotel</b> in town if you like <b>luxury</b> hotels.",
                "We highly recommend this <b>hotel</b>."
            };

            Assert.Equal(expectedDescriptionHighlights, highlights[Description]);
        }
Exemplo n.º 4
0
        static SearchResultMapper()
        {
            var mapperConfiguration = new MapperConfiguration(configuration =>
            {
                configuration.CreateMap <Microsoft.Azure.Search.Models.Document, Document>()
                .ConvertUsing(searchDocument =>
                {
                    var doc = new Document();
                    foreach (var key in searchDocument.Keys)
                    {
                        doc.Add(key, searchDocument[key]);
                    }

                    return(doc);
                });
                configuration.CreateMap <Microsoft.Azure.Search.Models.HitHighlights, HitHighlights>()
                .ConvertUsing(searchHighlights =>
                {
                    if (searchHighlights == null)
                    {
                        return(null);
                    }

                    var highlights = new HitHighlights();
                    foreach (var key in searchHighlights.Keys)
                    {
                        highlights.Add(key, searchHighlights[key]);
                    }

                    return(highlights);
                });
                configuration.CreateMap <Microsoft.Azure.Search.Models.SearchResult, SearchResult>();
                configuration.CreateMap <Microsoft.Azure.Search.Models.SuggestResult, SuggestResult>();
            });

            mapperConfiguration.AssertConfigurationIsValid();
            Mapper = mapperConfiguration.CreateMapper();
        }