protected virtual void EnsurePage(FilterOptionViewModel model, string page)
 {
     if (model.Page < 1)
     {
         if (!string.IsNullOrEmpty(page))
         {
             model.Page = int.Parse(page);
         }
         else
         {
             model.Page = 1;
         }
     }
 }
Ejemplo n.º 2
0
        public IEnumerable <ProductTileViewModel> QuickSearch(string query, int catalogId = 0)
        {
            var filterOptions = new FilterOptionViewModel
            {
                Q           = query,
                PageSize    = 5,
                Sort        = string.Empty,
                FacetGroups = new List <FacetGroupOption>(),
                Page        = 1,
                TrackData   = false
            };

            return(QuickSearch(filterOptions, catalogId));
        }
Ejemplo n.º 3
0
        public ContentSearchViewModel SearchPdf(FilterOptionViewModel filterOptions)
        {
            var model = new ContentSearchViewModel
            {
                FilterOption = filterOptions
            };

            if (!filterOptions.Q.IsNullOrEmpty())
            {
                var siteId = SiteDefinition.Current.Id;
                var query  = _findClient.UnifiedSearchFor(filterOptions.Q, _findClient.Settings.Languages.GetSupportedLanguage(ContentLanguage.PreferredCulture) ?? Language.None)
                             .UsingSynonyms()
                             .TermsFacetFor(x => x.SearchSection)
                             .FilterFacet("AllSections", x => x.SearchSection.Exists())
                             .Filter(x => x.MatchType(typeof(FoundationPdfFile)))
                             .Skip((filterOptions.Page - 1) * filterOptions.PageSize)
                             .Take(filterOptions.PageSize)
                             .ApplyBestBets();

                // obey DNT
                var doNotTrackHeader = System.Web.HttpContext.Current.Request.Headers.Get("DNT");
                if ((doNotTrackHeader == null || doNotTrackHeader.Equals("0")) && filterOptions.TrackData)
                {
                    query = query.Track();
                }

                if (!string.IsNullOrWhiteSpace(filterOptions.SectionFilter))
                {
                    query = query.FilterHits(x => x.SearchSection.Match(filterOptions.SectionFilter));
                }

                var hitSpec = new HitSpecification
                {
                    HighlightTitle   = true,
                    HighlightExcerpt = true
                };

                model.Hits = query.GetResult(hitSpec);
                filterOptions.TotalCount = model.Hits.TotalMatching;
            }

            return(model);
        }
Ejemplo n.º 4
0
        public virtual SearchViewModel <TContent> Create <TContent>(TContent currentContent,
                                                                    string selectedFacets,
                                                                    int catalogId,
                                                                    FilterOptionViewModel filterOption)
            where TContent : IContent
        {
            var model = new SearchViewModel <TContent>(currentContent);

            if (!filterOption.Q.IsNullOrEmpty() && (filterOption.Q.StartsWith("*") || filterOption.Q.StartsWith("?")))
            {
                model.CurrentContent   = currentContent;
                model.FilterOption     = filterOption;
                model.HasError         = true;
                model.ErrorMessage     = _localizationService.GetString("/Search/BadFirstCharacter");
                model.CategoriesFilter = new CategoriesFilterViewModel();
                return(model);
            }

            var results = _searchService.Search(currentContent, filterOption, selectedFacets, catalogId);

            filterOption.TotalCount  = results.TotalCount;
            filterOption.FacetGroups = results.FacetGroups.ToList();

            filterOption.Sorting = _searchService.GetSortOrder().Select(x => new SelectListItem
            {
                Text     = _localizationService.GetString("/Category/Sort/" + x.Name),
                Value    = x.Name.ToString(),
                Selected = string.Equals(x.Name.ToString(), filterOption.Sort)
            });

            model.CurrentContent    = currentContent;
            model.ProductViewModels = results?.ProductViewModels ?? new List <ProductTileViewModel>();
            model.FilterOption      = filterOption;
            model.CategoriesFilter  = GetCategoriesFilter(currentContent, filterOption.Q);
            model.DidYouMeans       = results.DidYouMeans;
            model.Query             = filterOption.Q;
            model.IsMobile          = _httpContextBase.GetOverriddenBrowser().IsMobileDevice;

            return(model);
        }
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            bindingContext.ModelName = "FilterOption";
            var      model       = new FilterOptionViewModel();
            var      contentLink = controllerContext.RequestContext.GetContentLink();
            IContent content     = null;

            if (!ContentReference.IsNullOrEmpty(contentLink))
            {
                content = _contentLoader.Get <IContent>(contentLink);
            }

            var query      = controllerContext.HttpContext.Request.QueryString["search"];
            var sort       = controllerContext.HttpContext.Request.QueryString["sort"];
            var facets     = controllerContext.HttpContext.Request.QueryString["facets"];
            var section    = controllerContext.HttpContext.Request.QueryString["t"];
            var page       = controllerContext.HttpContext.Request.QueryString["p"];
            var confidence = controllerContext.HttpContext.Request.QueryString["confidence"];

            SetupModel(model, query, sort, section, page, content, confidence);
            EnsureFacets(model, facets, content);
            return(model);
        }
Ejemplo n.º 6
0
        public SearchViewModel <TContent> Create <TModel, TContent>(TContent currentContent, string selectedFacets, FilterOptionViewModel filterOption)
            where TContent : IContent
        {
            var model = new SearchViewModel <TContent>(currentContent);

            if (filterOption.Q != null && (filterOption.Q.StartsWith("*") || filterOption.Q.StartsWith("?")))
            {
                model.CurrentContent = currentContent;
                model.FilterOption   = filterOption;
                model.HasError       = true;
                model.ErrorMessage   = _localizationService.GetString("/Search/BadFirstCharacter");

                return(model);
            }

            model.ContentSearchResult = _searchService.SearchContent(filterOption);
            model.CurrentContent      = currentContent;
            model.FilterOption        = filterOption;
            model.Query    = filterOption.Q;
            model.IsMobile = _httpContextBase.GetOverriddenBrowser().IsMobileDevice;

            if (!model.ContentSearchResult.Hits.Any() && model.FilterOption.Q.IsNullOrEmpty())
            {
                model.DidYouMeans = _findClient.Statistics().GetDidYouMean(model.FilterOption.Q);
            }

            return(model);
        }
Ejemplo n.º 7
0
        public async Task <ActionResult> Index(SearchResultPage currentPage, FilterOptionViewModel filterOptions)
        {
            if (filterOptions == null)
            {
                return(Redirect(Url.ContentUrl(ContentReference.StartPage)));
            }

            if (string.IsNullOrEmpty(filterOptions.ViewSwitcher))
            {
                filterOptions.ViewSwitcher = "Grid";
            }

            var searchSettings = _settingsService.GetSiteSettings <SearchSettings>();
            var startPage      = _contentLoader.Get <HomePage>(ContentReference.StartPage);
            var viewModel      = _viewModelFactory.Create(currentPage,
                                                          HttpContext.Request.QueryString["facets"],
                                                          searchSettings?.SearchCatalog ?? 0,
                                                          filterOptions);

            if (viewModel == null)
            {
                return(View(viewModel));
            }

            if (!searchSettings?.ShowProductSearchResults ?? false)
            {
                viewModel.ProductViewModels = new List <ProductTileViewModel>();
            }
            else
            {
                var bestBestList    = viewModel.ProductViewModels.Where(x => x.IsBestBetProduct);
                var notBestBestList = viewModel.ProductViewModels.Where(x => !x.IsBestBetProduct);
                viewModel.ProductViewModels = bestBestList.Union(notBestBestList);

                if (filterOptions.Page <= 1 && HttpContext.Request.HttpMethod == "GET")
                {
                    var trackingResult =
                        await _recommendationService.TrackSearch(HttpContext, filterOptions.Q, filterOptions.PageSize,
                                                                 viewModel.ProductViewModels.Select(x => x.Code));

                    viewModel.Recommendations = trackingResult.GetSearchResultRecommendations(_referenceConverter);
                }
            }

            await _cmsTrackingService.SearchedKeyword(_httpContextBase, filterOptions.Q);

            if (searchSettings?.ShowContentSearchResults ?? true)
            {
                viewModel.ContentSearchResult = _searchService.SearchContent(new FilterOptionViewModel()
                {
                    Q                    = filterOptions.Q,
                    PageSize             = 5,
                    Page                 = filterOptions.SearchContent ? filterOptions.Page : 1,
                    SectionFilter        = filterOptions.SectionFilter,
                    IncludeImagesContent = searchSettings?.IncludeImagesInContentsSearchResults ?? true
                });
            }

            if (searchSettings?.ShowPdfSearchResults ?? true)
            {
                viewModel.PdfSearchResult = _searchService.SearchPdf(new FilterOptionViewModel()
                {
                    Q             = filterOptions.Q,
                    PageSize      = 5,
                    Page          = filterOptions.SearchPdf ? filterOptions.Page : 1,
                    SectionFilter = filterOptions.SectionFilter
                });
            }

            var productCount = viewModel.ProductViewModels?.Count() ?? 0;
            var contentCount = viewModel.ContentSearchResult?.Hits?.Count() ?? 0;
            var pdfCount     = viewModel.PdfSearchResult?.Hits?.Count() ?? 0;

            if (productCount + contentCount + pdfCount == 1)
            {
                if (productCount == 1)
                {
                    var product = viewModel.ProductViewModels.FirstOrDefault();
                    return(Redirect(product.Url));
                }
                if (contentCount == 1)
                {
                    var content = viewModel.ContentSearchResult.Hits.FirstOrDefault();
                    return(Redirect(content.Url));
                }
                if (pdfCount == 1)
                {
                    var content = viewModel.PdfSearchResult.Hits.FirstOrDefault();
                    return(Redirect(content.Url));
                }
            }

            viewModel.ShowProductSearchResults = searchSettings?.ShowProductSearchResults ?? true;
            viewModel.ShowContentSearchResults = searchSettings?.ShowContentSearchResults ?? true;
            viewModel.ShowPdfSearchResults     = searchSettings?.ShowPdfSearchResults ?? true;

            return(View(viewModel));
        }
Ejemplo n.º 8
0
 public ActionResult Facet(SearchResultPage currentPage, FilterOptionViewModel viewModel) => PartialView("_Facet", viewModel);
Ejemplo n.º 9
0
        private ITypeSearch <EntryContentBase> OrderBy(ITypeSearch <EntryContentBase> query, FilterOptionViewModel commerceFilterOptionViewModel)
        {
            if (string.IsNullOrEmpty(commerceFilterOptionViewModel.Sort) || commerceFilterOptionViewModel.Sort.Equals("Position"))
            {
                if (commerceFilterOptionViewModel.SortDirection.Equals("Asc"))
                {
                    query = query.OrderBy(x => x.SortOrder());
                    return(query);
                }
                query = query.OrderByDescending(x => x.SortOrder());
                return(query);
            }

            if (commerceFilterOptionViewModel.Sort.Equals("Price"))
            {
                if (commerceFilterOptionViewModel.SortDirection.Equals("Asc"))
                {
                    query = query.OrderBy(x => x.DefaultPrice());
                    return(query);
                }
                query = query.OrderByDescending(x => x.DefaultPrice());
                return(query);
            }

            if (commerceFilterOptionViewModel.Sort.Equals("Name"))
            {
                if (commerceFilterOptionViewModel.SortDirection.Equals("Asc"))
                {
                    query = query.OrderBy(x => x.DisplayName);
                    return(query);
                }
                query = query.OrderByDescending(x => x.DisplayName);
                return(query);
            }

            //if (CommerceFilterOptionViewModel.Sort.Equals("Recommended"))
            //{
            //    query = query.UsingPersonalization();
            //    return query;
            //}

            return(query);
        }
Ejemplo n.º 10
0
        private ProductSearchResults GetSearchResults(IContent currentContent,
                                                      FilterOptionViewModel filterOptions,
                                                      string selectedfacets,
                                                      IEnumerable <Filter> filters = null,
                                                      int catalogId = 0)
        {
            //If contact belong organization, only find product that belong the categories that has owner is this organization
            var contact        = PrincipalInfo.CurrentPrincipal.GetCustomerContact();
            var organizationId = contact?.ContactOrganization?.PrimaryKeyId ?? Guid.Empty;

            EPiServer.Commerce.Catalog.ContentTypes.CatalogContent catalogOrganization = null;
            if (organizationId != Guid.Empty)
            {
                //get category that has owner id = organizationId
                catalogOrganization = _contentRepository
                                      .GetChildren <EPiServer.Commerce.Catalog.ContentTypes.CatalogContent>(_referenceConverter.GetRootLink())
                                      .FirstOrDefault(x => !string.IsNullOrEmpty(x.Owner) && x.Owner.Equals(organizationId.ToString(), StringComparison.OrdinalIgnoreCase));
            }

            var pageSize = filterOptions.PageSize > 0 ? filterOptions.PageSize : DefaultPageSize;
            var market   = _currentMarket.GetCurrentMarket();

            var query = _findClient.Search <EntryContentBase>();

            query = ApplyTermFilter(query, filterOptions.Q, filterOptions.TrackData);
            query = query.Filter(x => x.Language.Name.Match(_languageResolver.GetPreferredCulture().Name));

            if (organizationId != Guid.Empty && catalogOrganization != null)
            {
                query = query.Filter(x => x.Outline().PrefixCaseInsensitive(catalogOrganization.Name));
            }

            var nodeContent = currentContent as NodeContent;

            if (nodeContent != null)
            {
                var outline = GetOutline(nodeContent.Code);
                query = query.FilterOutline(new[] { outline });
            }

            query = query.FilterMarket(market);
            var facetQuery = query;

            query = FilterSelected(query, filterOptions.FacetGroups);
            query = ApplyFilters(query, filters);
            query = OrderBy(query, filterOptions);
            //Exclude products from search
            //query = query.Filter(x => (x as ProductContent).ExcludeFromSearch.Match(false));

            if (catalogId != 0)
            {
                query = query.Filter(x => x.CatalogId.Match(catalogId));
            }

            query = query.ApplyBestBets()
                    .Skip((filterOptions.Page - 1) * pageSize)
                    .Take(pageSize)
                    .StaticallyCacheFor(TimeSpan.FromMinutes(1));

            var result = query.GetContentResult();

            return(new ProductSearchResults
            {
                ProductViewModels = CreateProductViewModels(result, currentContent, filterOptions.Q),
                FacetGroups = GetFacetResults(filterOptions.FacetGroups, facetQuery, selectedfacets),
                TotalCount = result.TotalMatching,
                DidYouMeans = string.IsNullOrEmpty(filterOptions.Q) ? null : result.TotalMatching != 0 ? null : _findClient.Statistics().GetDidYouMean(filterOptions.Q),
                Query = filterOptions.Q,
            });
        }
Ejemplo n.º 11
0
 public IEnumerable <ProductTileViewModel> QuickSearch(FilterOptionViewModel filterOptions,
                                                       int catalogId = 0)
 => string.IsNullOrEmpty(filterOptions.Q) ? Enumerable.Empty <ProductTileViewModel>() : GetSearchResults(null, filterOptions, "", null, catalogId).ProductViewModels;
Ejemplo n.º 12
0
 public ProductSearchResults SearchWithFilters(IContent currentContent,
                                               FilterOptionViewModel filterOptions,
                                               IEnumerable <Filter> filters,
                                               int catalogId = 0) => filterOptions == null?CreateEmptyResult() : GetSearchResults(currentContent, filterOptions, "", filters, catalogId);
Ejemplo n.º 13
0
 public ProductSearchResults Search(IContent currentContent,
                                    FilterOptionViewModel filterOptions,
                                    string selectedFacets,
                                    int catalogId = 0) => filterOptions == null?CreateEmptyResult() : GetSearchResults(currentContent, filterOptions, selectedFacets, null, catalogId);