public Query Get(ProductSearchQuery searchQuery) { var booleanQuery = new BooleanQuery { ProductSearchPublishedDefinition.PublishedOnly }; if (!searchQuery.Options.Any() && !searchQuery.Specifications.Any() && Math.Abs(searchQuery.PriceFrom - 0) < 0.01 && !searchQuery.PriceTo.HasValue && !searchQuery.CategoryId.HasValue && string.IsNullOrWhiteSpace(searchQuery.SearchTerm) && !searchQuery.BrandId.HasValue) { return booleanQuery; } if (searchQuery.Options.Any()) booleanQuery.Add(GetOptionsQuery(searchQuery.Options), Occur.MUST); if (searchQuery.Specifications.Any()) booleanQuery.Add(GetSpecificationsQuery(searchQuery.Specifications), Occur.MUST); if (searchQuery.CategoryId.HasValue) booleanQuery.Add(GetCategoriesQuery(searchQuery.CategoryId.Value), Occur.MUST); if (searchQuery.PriceFrom > 0 || searchQuery.PriceTo.HasValue) booleanQuery.Add(GetPriceRangeQuery(searchQuery), Occur.MUST); if (!String.IsNullOrWhiteSpace(searchQuery.SearchTerm)) { IndexDefinition indexDefinition = IndexingHelper.Get<ProductSearchIndex>(); Analyzer analyser = indexDefinition.GetAnalyser(); var parser = new MultiFieldQueryParser(Version.LUCENE_30, indexDefinition.SearchableFieldNames, analyser); Query query = searchQuery.SearchTerm.SafeGetSearchQuery(parser, analyser); booleanQuery.Add(query, Occur.MUST); } if (searchQuery.BrandId.HasValue) booleanQuery.Add(GetBrandQuery(searchQuery.BrandId.Value), Occur.MUST); return booleanQuery; }
public void SetBrandSearchViewData(ProductSearchQuery query, ViewDataDictionary viewData) { ProductOptionSearchData productOptionSearchData = _productOptionManager.GetSearchData(query); viewData["product-options"] = productOptionSearchData.AttributeOptions; viewData["product-specifications"] = productOptionSearchData.SpecificationOptions; viewData["max-price"] = _productSearchIndexService.GetMaxPrice(query); }
public List<OptionInfo> GetOptions(ProductSearchQuery query) { var clone = query.Clone() as ProductSearchQuery; IndexSearcher indexSearcher = _productSearcher.IndexSearcher; var valueCollector = new ValueCollector(indexSearcher, FieldDefinition.GetFieldName<ProductSearchOptionsDefinition>()); indexSearcher.Search(GetQuery(clone), valueCollector); return GetOptionInfo(valueCollector); }
public void SetProductSearchViewData(ProductSearchQuery query, ViewDataDictionary viewData) { ProductOptionSearchData productOptionSearchData = _productOptionManager.GetSearchData(query); viewData["product-options"] = productOptionSearchData.AttributeOptions; viewData["product-specifications"] = productOptionSearchData.SpecificationOptions; viewData["product-brands"] = _availableBrandsService.GetAvailableBrands(query); viewData["categories"] = _categoryService.GetCategoriesForSearch(query); viewData["max-price"] = _productSearchIndexService.GetMaxPrice(query); }
public bool CategoryHasBeenSorted(ProductSearchQuery query) { if (!query.CategoryId.HasValue) return false; return _session.QueryOver<CategoryProductDisplayOrder>() .Where(order => order.Category.Id == query.CategoryId.Value) .Cacheable() .Any(); }
public Sort GetSort(ProductSearchQuery query) { switch (_getProductSearchSortByValue.Get(query)) { case ProductSearchSort.MostPopular: return new Sort(new[] { new SortField(FieldDefinition.GetFieldName<ProductSearchNumberBoughtDefinition>(), SortField.INT, true) }); case ProductSearchSort.Latest: return new Sort(new[] { new SortField(FieldDefinition.GetFieldName<ProductSearchCreatedOnDefinition>(), SortField.STRING, true) }); case ProductSearchSort.NameAToZ: return new Sort(new[] { new SortField(FieldDefinition.GetFieldName<ProductSearchNameSortDefinition>(), SortField.STRING) }); case ProductSearchSort.NameZToA: return new Sort(new[] { new SortField(FieldDefinition.GetFieldName<ProductSearchNameSortDefinition>(), SortField.STRING, true) }); case ProductSearchSort.PriceLowToHigh: return new Sort(new[] { new SortField(FieldDefinition.GetFieldName<ProductSearchPriceDefinition>(), SortField.DOUBLE) }); case ProductSearchSort.PriceHighToLow: return new Sort(new[] { new SortField(FieldDefinition.GetFieldName<ProductSearchPriceDefinition>(), SortField.DOUBLE, true) }); case ProductSearchSort.DisplayOrder: if (query.CategoryId != null) return new Sort(new[] { new SortField(ProductSearchIndex.GetCategoryFieldName(query.CategoryId.Value), SortField.INT, false) }); return Sort.RELEVANCE; default: return Sort.RELEVANCE; } }
private object GetPerPageOptions(ProductSearchQuery query) { IEnumerable<int> options = _ecommerceSettings .SearchProductsPerPage.Split(',') .Where(s => { int result; return int.TryParse(s, out result); }).Select(s => Convert.ToInt32(s)); return options.BuildSelectItemList(i => string.Format("{0} products per page", i), i => i.ToString(), i => i == query.PageSize, emptyItem: null); }
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext.IsChildAction) { return base.BindModel(controllerContext, bindingContext); } var model = new ProductSearchQuery { Specifications = (controllerContext.HttpContext.Request["Specifications"] ?? string.Empty).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => Convert.ToInt32((string) s)) .ToList(), Options = (controllerContext.HttpContext.Request["Options"] ?? string.Empty) .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .ToList(), PageSize = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["PageSize"]) ? Convert.ToInt32(controllerContext.HttpContext.Request["PageSize"]) : MrCMSApplication.Get<EcommerceSettings>() .ProductPerPageOptions.FirstOrDefault(), Page = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["Page"]) ? Convert.ToInt32(controllerContext.HttpContext.Request["Page"]) : 1, CategoryId = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["CategoryId"]) ? Convert.ToInt32(controllerContext.HttpContext.Request["CategoryId"]) : (int?)null, PriceFrom = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["PriceFrom"]) ? Convert.ToDouble(controllerContext.HttpContext.Request["PriceFrom"]) : 0, PriceTo = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["PriceTo"]) ? Convert.ToDouble(controllerContext.HttpContext.Request["PriceTo"]) : (double?)null, BrandId = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["BrandId"]) ? Convert.ToInt32(controllerContext.HttpContext.Request["BrandId"]) : (int?)null, SearchTerm = controllerContext.HttpContext.Request["SearchTerm"] }; model.SortBy = !string.IsNullOrWhiteSpace(controllerContext.HttpContext.Request["SortBy"]) ? (ProductSearchSort)Convert.ToInt32(controllerContext.HttpContext.Request["SortBy"]) : model.SortBy; return model; }
public List<SelectListItem> GetAvailableBrands(ProductSearchQuery query) { BrandInfo info = null; List<int> brands = _productSearchIndexService.GetBrands(query); IList<BrandInfo> items = _session.QueryOver<Brand>().Where(item => item.Id.IsIn(brands)).SelectList(builder => { builder.Select(x => x.Id).WithAlias(() => info.Id); builder.Select(x => x.Name).WithAlias(() => info.Name); return builder; }).TransformUsing(Transformers.AliasToBean<BrandInfo>()) .List<BrandInfo>(); return items.BuildSelectItemList(brand => brand.Name, brand => brand.Id.ToString(), brand => brand.Id == query.BrandId, "All Brands"); }
public ProductSearchSort Get(ProductSearchQuery query) { if (query.SortBy.HasValue) return query.SortBy.Value; if (!string.IsNullOrWhiteSpace(query.SearchTerm)) return ProductSearchSort.Relevance; if (query.CategoryId.HasValue) { var category = _session.Get<Category>(query.CategoryId); if (category != null) if (category.DefaultProductSearchSort.HasValue) return category.DefaultProductSearchSort.Value; else if (CategoryHasBeenSorted(query)) return ProductSearchSort.DisplayOrder; } return _ecommerceSettings.DefaultProductSearchSort; }
private List<SelectListItem> GetSortByOptions(ProductSearchQuery query) { var productSearchSorts = new List<ProductSearchSort> { ProductSearchSort.MostPopular, ProductSearchSort.Latest, ProductSearchSort.NameAToZ, ProductSearchSort.NameZToA, ProductSearchSort.PriceLowToHigh, ProductSearchSort.PriceHighToLow, }; if (query.CategoryId.HasValue && _getProductSearchSortByValue.CategoryHasBeenSorted(query)) productSearchSorts.Insert(0, ProductSearchSort.DisplayOrder); if (!string.IsNullOrWhiteSpace(query.SearchTerm)) productSearchSorts.Insert(0, ProductSearchSort.Relevance); ProductSearchSort productSearchSort = _getProductSearchSortByValue.Get(query); return productSearchSorts.BuildSelectItemList(sort => sort.GetDescription(), sort => Convert.ToInt32(sort).ToString(), sort => sort == productSearchSort, emptyItem: null); }
public double GetMaxPrice(ProductSearchQuery query) { var clone = query.Clone() as ProductSearchQuery; clone.PriceTo = null; TopDocs search = _productSearcher.IndexSearcher.Search(GetQuery(clone), int.MaxValue); List<Document> documents = search.ScoreDocs.Select(doc => _productSearcher.IndexSearcher.Doc(doc.Doc)).ToList(); decimal max = documents.Count > 0 ? documents.Select( document => document.GetValue<decimal>(FieldDefinition.GetFieldName<ProductSearchPriceDefinition>())) .Max() : 0; if (documents.Any()) max = documents.Select( document => document.GetValue<decimal>(FieldDefinition.GetFieldName<ProductSearchPriceDefinition>())) .Max(); return Convert.ToDouble(Math.Ceiling(max / 5.0m) * 5m); }
public CategorySearchModel GetCategoriesForSearch(ProductSearchQuery query) { List<int> availableCategories = _productSearchIndexService.GetCategories(query); if (!query.CategoryId.HasValue) return GetRootCategoryModel(availableCategories); var category = _session.Get<Category>(query.CategoryId); List<Category> categories = _session.QueryOver<Category>() .Where(cat => cat.Parent.Id == category.Id && cat.Id.IsIn(availableCategories)) .Cacheable() .List().ToList(); List<Category> hierarchy = category.ActivePages.OfType<Category>().Where(cat => availableCategories.Contains(cat.Id)).ToList(); hierarchy.Reverse(); return new CategorySearchModel { Children = categories, Hierarchy = hierarchy }; }
public void SetViewData(ProductSearchQuery productSearchQuery, ViewDataDictionary viewData) { viewData["sort-by-options"] = GetSortByOptions(productSearchQuery); viewData["per-page-options"] = GetPerPageOptions(productSearchQuery); }
private void RemoveHiddenSearchSpecifications(ProductSearchQuery query, List<ProductSpecificationAttribute> productSpecificationAttributes) { EcommerceSearchablePage category = query.CategoryId.HasValue ? (EcommerceSearchablePage) _session.Get<Category>(query.CategoryId.Value) : _uniquePageService.GetUniquePage<ProductSearch>(); if (category != null) { productSpecificationAttributes.RemoveAll( attribute => category.HiddenSearchSpecifications.Contains(attribute)); } }
public ProductOptionSearchData GetSearchData(ProductSearchQuery query) { OptionSearchData values = _productSearchIndexService.GetOptionSearchData(query); return new ProductOptionSearchData { AttributeOptions = GetSearchAttributeOptions(values.Options), SpecificationOptions = GetSearchSpecificationAttributes(query, values.Specifications) }; }
private List<ProductOptionModel<int>> GetSearchSpecificationAttributes(ProductSearchQuery query, List<int> values) { ProductSpecificationAttribute attributeAlias = null; IList<ProductSpecificationAttributeOption> productSpecificationAttributeOptions = _session.QueryOver<ProductSpecificationAttributeOption>() .JoinAlias(option => option.ProductSpecificationAttribute, () => attributeAlias) .Where(option => option.Id.IsIn(values) && !attributeAlias.HideInSearch) .Fetch(option => option.ProductSpecificationAttribute).Eager .Cacheable() .List(); List<ProductSpecificationAttribute> productSpecificationAttributes = productSpecificationAttributeOptions.Select(value => value.ProductSpecificationAttribute) .OrderBy(x => x.DisplayOrder) .Distinct() .ToList(); RemoveHiddenSearchSpecifications(query, productSpecificationAttributes); return productSpecificationAttributes.Select(attribute => new ProductOptionModel<int> { Name = attribute.Name, Id = attribute.Id, Values = productSpecificationAttributeOptions .Where( value => value .ProductSpecificationAttribute == attribute) .OrderBy(x => x.DisplayOrder) .Distinct() .Select( value => new ProductValueModel<int> { Name = value.Name, Id = value.Id }).ToList() }).ToList(); }
public List<int> GetCategories(ProductSearchQuery query) { var clone = query.Clone() as ProductSearchQuery; clone.CategoryId = null; Query searchQuery = GetQuery(clone); return _getProductCategories.Get(searchQuery); }
public Query GetQuery(ProductSearchQuery searchQuery) { return _getProductSearchLuceneQuery.Get(searchQuery); }
private Query GetPriceRangeQuery(ProductSearchQuery searchQuery) { var booleanQuery = new BooleanQuery { { NumericRangeQuery.NewDoubleRange( FieldDefinition.GetFieldName<ProductSearchPriceDefinition>(), searchQuery.PriceFrom, searchQuery.PriceTo, true, searchQuery.PriceTo.HasValue), Occur.MUST } }; return booleanQuery; }
public CachingInfo GetCachingInfo(ProductSearchQuery query, string suffix = null) { return new CachingInfo(_ecommerceSearchCacheSettings.SearchCache, GetCacheKey(query) + suffix, TimeSpan.FromSeconds(_ecommerceSearchCacheSettings.SearchCacheLength), _ecommerceSearchCacheSettings.SearchCacheExpiryType); }
public List<int> GetBrands(ProductSearchQuery query) { var clone = query.Clone() as ProductSearchQuery; clone.BrandId = null; IndexSearcher indexSearcher = _productSearcher.IndexSearcher; string name = FieldDefinition.GetFieldName<ProductSearchBrandDefinition>(); var valueCollector = new ValueCollector(indexSearcher, name); indexSearcher.Search(GetQuery(clone), valueCollector); return valueCollector.Values[name].Where(x => !string.IsNullOrEmpty(x)) .Select(s => Convert.ToInt32(s)) .Distinct() .ToList(); }
private Sort GetSort(ProductSearchQuery query) { return _getProductSearchQueryObjects.GetSort(query); }
public OptionSearchData GetOptionSearchData(ProductSearchQuery query) { ValueCollector optionValueCollector = GetOptionValueCollector(query); return new OptionSearchData { Options = GetOptionInfo(optionValueCollector), Specifications = GetSpecifications(optionValueCollector) }; }
private Query GetQuery(ProductSearchQuery query) { return _getProductSearchQueryObjects.GetQuery(query); }
private ValueCollector GetOptionValueCollector(ProductSearchQuery query) { var clone = query.Clone() as ProductSearchQuery; //clone.Options = new List<string>(); IndexSearcher indexSearcher = _productSearcher.IndexSearcher; var valueCollector = new ValueCollector(indexSearcher, FieldDefinition.GetFieldName<ProductSearchSpecificationsDefinition>(), FieldDefinition.GetFieldName<ProductSearchOptionsDefinition>()); indexSearcher.Search(GetQuery(clone), valueCollector); return valueCollector; }
public IPagedList<Product> SearchProducts(ProductSearchQuery query) { IPagedList<Product> searchProducts = _productSearcher.Search(GetQuery(query), query.Page, query.PageSize, sort: GetSort(query)); return searchProducts; }
private string GetCacheKey(ProductSearchQuery query) { return _ecommerceSearchCacheSettings.SearchCachePerUser ? JsonConvert.SerializeObject(new { CurrentRequestData.UserGuid, query }) : JsonConvert.SerializeObject(query); }