Esempio n. 1
0
        public ViewResult Index(FindPage currentPage, string q)
        {
            var model = new FindSearchContentModel(currentPage)
            {
                PublicProxyPath = _findUIConfiguration.AbsolutePublicProxyPath()
            };

            if (!string.IsNullOrWhiteSpace(model.Query))
            {
                var query =
                    _searchClient.UnifiedSearchFor(model.Query, _searchClient.Settings.Languages.GetSupportedLanguage(ContentLanguage.PreferredCulture) ??
                                                   Language.None)
                    .UsingSynonyms()
                    //Include a facet whose value we can use to show the total number of hits
                    //regardless of section. The filter here is irrelevant but should match *everything*.
                    .TermsFacetFor(x => x.SearchSection)
                    .FilterFacet("AllSections", x => x.SearchSection.Exists())
                    //Fetch the specific paging page.
                    .Skip((model.PagingPage - 1) * model.CurrentPage.PageSize)
                    .Take(model.CurrentPage.PageSize)
                    //Range facet for date
                    //.RangeFacetFor(x => x.SearchUpdateDate, model.PublishedDateRange.ToArray())
                    //Allow editors (from the Find/Optimizations view) to push specific hits to the top
                    //for certain search phrases.
                    .ApplyBestBets();

                // obey DNT
                var doNotTrackHeader = System.Web.HttpContext.Current.Request.Headers.Get("DNT");
                // Should Not track when value equals 1
                if (doNotTrackHeader == null || doNotTrackHeader.Equals("0"))
                {
                    query = query.Track();
                }

                //If a section filter exists (in the query string) we apply
                //a filter to only show hits from a given section.
                if (!string.IsNullOrWhiteSpace(model.SectionFilter))
                {
                    query = query.FilterHits(x => x.SearchSection.Match(model.SectionFilter));
                }

                //We can (optionally) supply a hit specification as argument to the GetResult
                //method to control what each hit should contain. Here we create a
                //hit specification based on values entered by an editor on the search page.
                var hitSpec = new HitSpecification
                {
                    HighlightTitle   = model.CurrentPage.HighlightTitles,
                    HighlightExcerpt = model.CurrentPage.HighlightExcerpts
                };

                //Execute the query and populate the Result property which the markup (aspx)
                //will render.
                model.Hits = query.GetResult(hitSpec);
            }

            return(View(model));
        }
        private ITypeSearch <ISearchContent> BuildQuery(FindSearchContentModel model, string selectedAnalyzer)
        {
            var language = searchClient.Settings.Languages.GetSupportedLanguage(selectedAnalyzer);

            var queryFor = language != null?
                           searchClient.UnifiedSearch(language) :
                               searchClient.UnifiedSearch();

            var querySearch = string.IsNullOrEmpty(selectedAnalyzer) || language == null
                    ? queryFor.For(model.Query)
                    : queryFor.For(model.Query, x => x.Analyzer = language.Analyzer);

            if (model.UseAndForMultipleSearchTerms)
            {
                querySearch = querySearch.WithAndAsDefaultOperator();
            }

            var query = querySearch
                        .UsingAutoBoost(TimeSpan.FromDays(30))
                        //Include a facet whose value is used to show the total number of hits regardless of section.
                        //The filter here is irrelevant but should match *everything*.
                        .TermsFacetFor(x => x.SearchSection)
                        .FilterFacet("AllSections", x => x.SearchSection.Exists())
                        .OrderBy(x => x.SearchTitle)
                        //Fetch the specific paging page.
                        .Skip((model.PagingPage - 1) * model.CurrentPage.PageSize)
                        .Take(model.CurrentPage.PageSize)
                        //Allow editors (from the Find/Optimizations view) to push specific hits to the top
                        //for certain search phrases.
                        .ApplyBestBets();

            // obey DNT
            var doNotTrackHeader = System.Web.HttpContext.Current.Request.Headers.Get("DNT");

            // Should not track when value equals 1
            if (doNotTrackHeader == null || doNotTrackHeader.Equals("0"))
            {
                query = query.Track();
            }

            //If a section filter exists (in the query string) we apply
            //a filter to only show hits from a given section.
            if (!string.IsNullOrWhiteSpace(model.SectionFilter))
            {
                query = query.FilterHits(x => x.SearchSection.Match(model.SectionFilter));
            }

            return(query);
        }
Esempio n. 3
0
        private ITypeSearch <ISearchContent> BuildQuery(FindSearchContentModel model)
        {
            string userId = this.userRepository.GetUserId(this.User);
            Dictionary <string, int> cats = this.pageRatingRepository.GetFavoriteCategoryNamesForUser(userId);

            var queryFor = this.searchClient.UnifiedSearch().For(model.Query);

            if (model.UseAndForMultipleSearchTerms)
            {
                queryFor = queryFor.WithAndAsDefaultOperator();
            }

            var query = queryFor
                        .UsingSynonyms()
                        .UsingAutoBoost(TimeSpan.FromDays(30))
                        .AddCategoryBoost(cats)
                        //Include a facet whose value is used to show the total number of hits regardless of section.
                        //The filter here is irrelevant but should match *everything*.
                        .TermsFacetFor(x => x.SearchSection)
                        .FilterFacet("AllSections", x => x.SearchSection.Exists())
                        //Fetch the specific paging page.
                        .Skip((model.PagingPage - 1) * model.CurrentPage.PageSize)
                        .Take(model.CurrentPage.PageSize)
                        //Allow editors (from the Find/Optimizations view) to push specific hits to the top
                        //for certain search phrases.
                        .ApplyBestBets();

            // obey DNT
            var doNotTrackHeader = System.Web.HttpContext.Current.Request.Headers.Get("DNT");

            // Should not track when value equals 1
            if (doNotTrackHeader == null || doNotTrackHeader.Equals("0"))
            {
                query = query.Track();
            }

            //If a section filter exists (in the query string) we apply
            //a filter to only show hits from a given section.
            if (!string.IsNullOrWhiteSpace(model.SectionFilter))
            {
                query = query.FilterHits(x => x.SearchSection.Match(model.SectionFilter));
            }
            return(query);
        }
        public ViewResult Index(FindSearchPage currentPage, string q, string selectedAnalyzer)
        {
            var model = new FindSearchContentModel(currentPage)
            {
                PublicProxyPath = findUIConfiguration.AbsolutePublicProxyPath()
            };

            var parameters = Request.Url != null ? Request.Url.Query : string.Empty;

            model.OtherLanguageUrl = model.OtherLanguageUrl + parameters;

            //detect if serviceUrl and/or defaultIndex is configured.
            model.IsConfigured = SearchIndexIsConfigured(EPiServer.Find.Configuration.GetConfiguration());

            if (model.IsConfigured && !string.IsNullOrWhiteSpace(model.Query))
            {
                var query = BuildQuery(model, selectedAnalyzer);

                //Create a hit specification to determine display based on values entered by an editor on the search page.
                var hitSpec = new HitSpecification
                {
                    HighlightTitle   = model.CurrentPage.HighlightTitles,
                    HighlightExcerpt = model.CurrentPage.HighlightExcerpts,
                    // When HighlightExcerpt = true then minimum of ExcerptLength = 36
                    ExcerptLength = model.CurrentPage.HighlightExcerpts && model.ExcerptLength < 36 ? 36 : model.ExcerptLength
                };

                try
                {
                    model.Hits = query.GetResult(hitSpec);
                }
                catch (WebException wex)
                {
                    model.IsConfigured = wex.Status != WebExceptionStatus.NameResolutionFailure;
                }
            }

            model.Analyzers = CreateAnalyzers(selectedAnalyzer);

            RequireClientResources();

            return(View(model));
        }
        public ViewResult Index(FindSearchPage currentPage, string q)
        {
            var model = new FindSearchContentModel(currentPage)
            {
                PublicProxyPath = findUIConfiguration.AbsolutePublicProxyPath()
            };

            //detect if serviceUrl and/or defaultIndex is configured.

            var findConfiguration = EPiServer.Find.Configuration.GetConfiguration();

            model.IsConfigured = !findConfiguration.ServiceUrl.IsNullOrEmpty() &&
                                 !findConfiguration.DefaultIndex.IsNullOrEmpty();

            if (model.IsConfigured && !string.IsNullOrWhiteSpace(model.Query))
            {
                var query = BuildQuery(model);

                //Create a hit specification to determine display based on values entered by an editor on the search page.
                var hitSpec = new HitSpecification
                {
                    HighlightTitle   = model.CurrentPage.HighlightTitles,
                    HighlightExcerpt = model.CurrentPage.HighlightExcerpts,
                    ExcerptLength    = model.ExcerptLength
                };

                try
                {
                    model.Hits = query.GetResult(hitSpec);
                }
                catch (WebException wex)
                {
                    model.IsConfigured = wex.Status != WebExceptionStatus.NameResolutionFailure;
                }
            }

            RequireClientResources();

            return(View(model));
        }