Beispiel #1
0
        protected virtual List <ContentSearchTabViewModel> GetSuggestedTabs(GetContentSearchParameter param, List <IContentTab> contentTabs)
        {
            List <ContentSearchTabViewModel> vm = null;

            if (param.ProductsTabActive && param.IsCorrectedSearchQuery)
            {
                vm = new List <ContentSearchTabViewModel>();
                foreach (var tab in contentTabs)
                {
                    var searchQuery   = param.SearchQuery;
                    var searchRequest = GetSearchRequestForContentTab(param, tab, searchQuery);
                    var result        = WebsiteSearchFacade.Search(searchRequest);

                    if (result.ResultsFound > 0)
                    {
                        vm.Add(new ContentSearchTabViewModel
                        {
                            Title    = tab.Title,
                            UrlTitle = tab.UrlTitle,
                            TabUrl   = GetTabUrl(param, tab, searchQuery),
                            Total    = result.ResultsFound
                        });
                    }
                }
            }
            return(vm);
        }
Beispiel #2
0
        protected virtual List <FacetViewModel> GetFacets(GetContentSearchParameter param, ContentSearchTabViewModel tab)
        {
            var facets = new List <FacetViewModel>();
            var labels = WebsiteSearchFacade.GetFacetOptions().ToDictionary(t => t.Item1, t => t.Item2);

            foreach (var f in tab.SearchResults.Facets)
            {
                if (f.Hits.Count == 0)
                {
                    continue;
                }

                var facet = new FacetViewModel
                {
                    Label = labels[f.Name],
                    Hits  = new List <FacetHitViewModel>()
                };

                foreach (var hit in f.Hits)
                {
                    facet.Hits.Add(GetFacetHiViewModel(hit, f.Name));
                }

                facets.Add(facet);
            }
            return(facets);
        }
Beispiel #3
0
        protected virtual List <FacetViewModel> GetSelectedFacets(GetContentSearchParameter param, ContentSearchTabViewModel tab)
        {
            var selectedFacets = new Dictionary <string, List <SearchResultFacetHit> >();
            int count          = 0;

            foreach (var f in tab.SearchResults.Facets)
            {
                var facets = GetFacetSelection(param, f.Name).ToList();
                count += facets.Count;
                selectedFacets.Add(f.Name, f.Hits.Where(i => facets.Contains(i.Value)).ToList());
            }

            if (count > 0)
            {
                var vm = new List <FacetViewModel>();
                foreach (var f in selectedFacets)
                {
                    var facet = new FacetViewModel()
                    {
                        Label = f.Key,
                        Hits  = new List <FacetHitViewModel>()
                    };

                    foreach (var hit in f.Value)
                    {
                        facet.Hits.Add(GetFacetHiViewModel(hit, f.Key));
                    }

                    vm.Add(facet);
                }
                return(vm);
            }
            return(null);
        }
Beispiel #4
0
        protected virtual string[] GetFacetSelection(GetContentSearchParameter param, string fieldName)
        {
            var prefix = GetFacetFieldCheckboxPrefix(fieldName);

            return(param.QueryKeys
                   .Where(key => key.StartsWith(prefix))
                   .Select(key => key.Substring(prefix.Length))
                   .ToArray());
        }
Beispiel #5
0
        protected virtual WebsiteSearchQuery GetSearchRequestForContentTab(GetContentSearchParameter param, IContentTab tab, string searchQuery)
        {
            bool isTabActive = param.PathInfo == tab.UrlTitle;
            // keywords
            var sq = searchQuery == "*" ? "*:*" : searchQuery;

            string[] keywords = sq.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


            // datatypes
            var tabTypes = tab.DataTypes.Split(',').ToList();

            Type[] dataTypes = tabTypes != null?tabTypes.Select(name => KnowTypes[name]).ToArray() : null;

            // page types
            var tabPageTypes = tab.PageTypes?.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            // facets
            var tabFacetsToQuery = !string.IsNullOrEmpty(tab.Facets) ? tab.Facets.Split(',') : Array.Empty <string>();
            var facets           = isTabActive ? tabFacetsToQuery.Select(fieldName => new WebsiteSearchQueryFacet
            {
                Name       = fieldName,
                Selections = GetFacetSelection(param, fieldName)
            }).ToArray() : null;

            // media folders
            var selectedMediaFolders = tab.MediaFolders != null?DataFacade.GetData <IMediaFileFolder>().Where(p => tab.MediaFolders.Contains(p.KeyPath))
                                       .Select(i => i.GetDataEntityToken()).ToArray() : null;

            var sortOptions = new List <SearchQuerySortOption>();

            if (isTabActive && !string.IsNullOrEmpty(param.SortDirection) && !string.IsNullOrEmpty(param.SortBy))
            {
                var isReverted = param.SortDirection == "desc" ? true : false;
                sortOptions.Add(new SearchQuerySortOption(param.SortBy, isReverted, SortTermsAs.String));
            }

            return(new WebsiteSearchQuery
            {
                Culture = param.Culture,
                Keywords = keywords,
                DataTypes = dataTypes,
                PageTypes = tabPageTypes,
                CurrentSiteOnly = param.CurrentSiteOnly,
                PageNumber = isTabActive ? param.CurrentPage - 1 : 0,
                Facets = facets,
                PageSize = param.PageSize,
                SortOptions = sortOptions,
                FilterByAncestorEntityTokens = selectedMediaFolders
            });
        }
Beispiel #6
0
        public virtual ContentSearchViewModel GetContentSearchViewModel(GetContentSearchParameter param)
        {
            using (var conn = new DataConnection(param.Culture))
            {
                var contentTabs = DataFacade.GetData <IContentTab>().Where(c => !string.IsNullOrEmpty(c.DataTypes)).OrderBy(t => t.Order).ToList();
                if (contentTabs == null || contentTabs.Count == 0)
                {
                    return(null);
                }

                var vm = new ContentSearchViewModel();

                foreach (var tab in contentTabs)
                {
                    var isActive      = param.PathInfo == tab.UrlTitle;
                    var searchQuery   = GetSearchQuery(param);
                    var searchRequest = GetSearchRequestForContentTab(param, tab, searchQuery);
                    var result        = WebsiteSearchFacade.Search(searchRequest);

                    vm.Tabs.Add(new ContentSearchTabViewModel
                    {
                        Title         = tab.Title,
                        UrlTitle      = tab.UrlTitle,
                        TabUrl        = GetTabUrl(param, tab, searchQuery),
                        PagesCount    = (int)Math.Ceiling((decimal)result.ResultsFound / param.PageSize),
                        Total         = result.ResultsFound,
                        IsActive      = isActive,
                        SearchResults = result.Entries.Select(x => GetSearchResultsEntryViewModel(x)).ToList(),
                        DataTypes     = tab.DataTypes
                    });

                    if (isActive)
                    {
                        vm.SelectedFacets = GetSelectedFacets(param, result);
                        vm.Facets         = GetFacets(param, result);
                    }
                }

                var sortBys = DataFacade.GetData <ISortOption>().OrderBy(t => t.Order).ToList();
                vm.AvailableSortBys = sortBys;
                vm.SelectedSortBy   = sortBys.Find(o => o.FieldName == param.SortBy && (o.IsReverted && param.SortDirection == "desc" || !o.IsReverted && param.SortDirection == "asc")) ?? sortBys.First();

                vm.SuggestedTabs = GetSuggestedTabs(param, contentTabs);

                return(vm);
            }
        }
Beispiel #7
0
        protected virtual string GetTabUrl(GetContentSearchParameter param, IContentTab tab, string searchQuery)
        {
            var pagesConfiguration = SiteConfiguration.GetPagesConfiguration(param.Culture, WebsiteContext.WebsiteId);

            if (pagesConfiguration == null)
            {
                return(null);
            }

            var url = PageService.GetPageUrl(pagesConfiguration.SearchPageId, param.Culture);

            if (url == null)
            {
                return(null);
            }

            return($"{url}/{tab.UrlTitle}?{SearchRequestParams.Keywords}={HttpUtility.UrlEncode(searchQuery)}");
        }
Beispiel #8
0
        public virtual ContentSearchViewModel GetContentSearchViewModel(GetContentSearchParameter param)
        {
            var contentTabs = DataFacade.GetData <IContentTab>().Where(c => !string.IsNullOrEmpty(c.DataTypes)).OrderBy(t => t.Order).ToList();

            if (contentTabs == null || contentTabs.Count == 0)
            {
                return(null);
            }

            var vm = new ContentSearchViewModel();

            foreach (var tab in contentTabs)
            {
                var isActive      = param.PathInfo == tab.UrlTitle;
                var searchQuery   = GetSearchQuery(param);
                var searchRequest = GetSearchRequestForContentTab(param, tab, searchQuery);
                var result        = WebsiteSearchFacade.Search(searchRequest);

                vm.Tabs.Add(new ContentSearchTabViewModel
                {
                    Title         = tab.Title,
                    UrlTitle      = tab.UrlTitle,
                    TabUrl        = GetTabUrl(param, tab, searchQuery),
                    SearchResults = result,
                    PagesCount    = (int)Math.Ceiling((decimal)result.ResultsFound / param.PageSize),
                    Total         = result.ResultsFound,
                    IsActive      = isActive
                });
            }

            vm.SuggestedTabs = GetSuggestedTabs(param, contentTabs);

            if (vm.ActiveTab != null)
            {
                vm.SelectedFacets = GetSelectedFacets(param, vm.ActiveTab);
                vm.Facets         = GetFacets(param, vm.ActiveTab);
            }

            return(vm);
        }
Beispiel #9
0
 protected virtual string GetTabUrl(GetContentSearchParameter param, IContentTab tab, string searchQuery)
 {
     return($"{param.BaseUrl}/{tab.UrlTitle}?{SearchRequestParams.Keywords}={searchQuery}");
 }
Beispiel #10
0
 protected virtual string GetSearchQuery(GetContentSearchParameter param)
 {
     return(param.ProductsTabActive && param.IsCorrectedSearchQuery ?
            param.CorrectedSearchQuery :
            param.SearchQuery.Trim().ToLower());
 }