public async Task<ActionResult> SearchCategories(CatalogSearchCriteria searchCriteria)
 {
     var retVal = await _catalogSearchService.SearchCategoriesAsync(searchCriteria);
     return Json(new
     {
         Categories = retVal,
         MetaData = retVal.GetMetaData()
     });
 }
 public async Task<ActionResult> SearchProducts(CatalogSearchCriteria searchCriteria)
 {
     var retVal = await _catalogSearchService.SearchProductsAsync(searchCriteria);
     return Json(new
     {
          Products = retVal.Products,
          Aggregations = retVal.Aggregations,
          MetaData = retVal.Products.GetMetaData()
     });
 }
Esempio n. 3
0
 public async Task<ActionResult> Index()
 {
     //Load categories for main page (may be removed if it not necessary)
     var catalogSearchCriteria = new CatalogSearchCriteria
     {
         CatalogId = _workContext.CurrentStore.Catalog,
         ResponseGroup = CatalogSearchResponseGroup.WithCategories
     };
     _workContext.CurrentCatalogSearchResult = await _catalogSearchService.SearchAsync(catalogSearchCriteria);
     return View("index", _workContext);
 }
        public static CatalogSearchCriteria Parse(NameValueCollection queryString)
        {
            var retVal = new CatalogSearchCriteria();
            retVal.Keyword = queryString.Get("q");
            retVal.PageNumber = Convert.ToInt32(queryString.Get("page") ?? "1");
            //TODO move this code to Parse or Converter method
            // tags=name1:value1,value2,value3;name2:value1,value2,value3
            retVal.SortBy = queryString.Get("sort_by");
            retVal.Terms = (queryString.GetValues("terms") ?? new string[0])
                .SelectMany(s => s.Split(';'))
                .Select(s => s.Split(':'))
                .Where(a => a.Length == 2)
                .SelectMany(a => a[1].Split(',').Select(v => new Term { Name = a[0], Value = v }))
                .ToArray();

            return retVal;
        }
 public CatalogSearchCriteria Clone()
 {
     var retVal = new CatalogSearchCriteria(Language, Currency);
     retVal.CatalogId = CatalogId;
     retVal.CategoryId = CategoryId;
     retVal.ResponseGroup = ResponseGroup;
     retVal.Currency = Currency;
     retVal.Language = Language;
     retVal.Keyword = Keyword;
     retVal.SortBy = SortBy;
     retVal.SearchInChildren = SearchInChildren;
     retVal.PageNumber = PageNumber;
     retVal.PageSize = PageSize;
     if (Terms != null)
     {
         retVal.Terms = Terms.Select(x => new Term { Name = x.Name, Value = x.Value }).ToArray();
     }
     return retVal;
 }
        public static CatalogSearchCriteria Parse(NameValueCollection queryString)
        {
            var retVal = new CatalogSearchCriteria();

            retVal.Keyword    = queryString.Get("q");
            retVal.PageNumber = Convert.ToInt32(queryString.Get("page") ?? "1");
            //TODO move this code to Parse or Converter method
            // tags=name1:value1,value2,value3;name2:value1,value2,value3
            retVal.SortBy = queryString.Get("sort_by");
            retVal.Terms  = (queryString.GetValues("terms") ?? new string[0])
                            .SelectMany(s => s.Split(';'))
                            .Select(s => s.Split(':'))
                            .Where(a => a.Length == 2)
                            .SelectMany(a => a[1].Split(',').Select(v => new Term {
                Name = a[0], Value = v
            }))
                            .ToArray();

            return(retVal);
        }
Esempio n. 7
0
        public CatalogSearchCriteria Clone()
        {
            var retVal = new CatalogSearchCriteria(Language, Currency);

            retVal.CatalogId        = CatalogId;
            retVal.CategoryId       = CategoryId;
            retVal.ResponseGroup    = ResponseGroup;
            retVal.Currency         = Currency;
            retVal.Language         = Language;
            retVal.Keyword          = Keyword;
            retVal.SortBy           = SortBy;
            retVal.SearchInChildren = SearchInChildren;
            retVal.PageNumber       = PageNumber;
            retVal.PageSize         = PageSize;
            if (Terms != null)
            {
                retVal.Terms = Terms.Select(x => new Term {
                    Name = x.Name, Value = x.Value
                }).ToArray();
            }
            return(retVal);
        }
        /// <summary>
        /// Async search categories by given criteria 
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public async Task<IPagedList<Category>> SearchCategoriesAsync(CatalogSearchCriteria criteria)
        {
            var workContext = _workContextFactory();
            criteria = criteria.Clone();
            //exclude products
            criteria.ResponseGroup = criteria.ResponseGroup & (~CatalogSearchResponseGroup.WithProducts);
            //include categories
            criteria.ResponseGroup = criteria.ResponseGroup | CatalogSearchResponseGroup.WithCategories;
            var searchCriteria = criteria.ToServiceModel(workContext);
            var result = await _catalogModuleApi.CatalogModuleSearchSearchAsync(searchCriteria);

            //API temporary does not support paginating request to categories (that's uses PagedList with superset instead StaticPagedList)
            return new PagedList<Category>(result.Categories.Select(x => x.ToWebModel(workContext.CurrentLanguage, workContext.CurrentStore)), criteria.PageNumber, criteria.PageSize);
        }
        /// <summary>
        /// Search products by given criteria 
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public CatalogSearchResult SearchProducts(CatalogSearchCriteria criteria)
        {
            var workContext = _workContextFactory();
            criteria = criteria.Clone();
            //exclude categories
            criteria.ResponseGroup = criteria.ResponseGroup & (~CatalogSearchResponseGroup.WithCategories);
            //include products
            criteria.ResponseGroup = criteria.ResponseGroup | CatalogSearchResponseGroup.WithProducts;

            var searchCriteria = criteria.ToServiceModel(workContext);

            var result = _searchApi.SearchModuleSearch(searchCriteria);
            var products = result.Products.Select(x => x.ToWebModel(workContext.CurrentLanguage, workContext.CurrentCurrency, workContext.CurrentStore)).ToList();

            //Unable to make parallel call because its synchronous method (in future this information pricing and inventory will be getting from search index) and this lines can be removed
            _pricingService.EvaluateProductPrices(products);
            LoadProductsInventories(products);

            return new CatalogSearchResult
            {
                Products = new StaticPagedList<Product>(products, criteria.PageNumber, criteria.PageSize, result.ProductsTotalCount.Value),
                Aggregations = !result.Aggregations.IsNullOrEmpty() ? result.Aggregations.Select(x => x.ToWebModel(workContext.CurrentLanguage.CultureName)).ToArray() : new Aggregation[] { }
            };
        }
        /// <summary>
        /// Async search products by given criteria 
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public async Task<CatalogSearchResult> SearchProductsAsync(CatalogSearchCriteria criteria)
        {
            criteria = criteria.Clone();
            //exclude categories
            criteria.ResponseGroup = criteria.ResponseGroup & (~CatalogSearchResponseGroup.WithCategories);
            //include products
            criteria.ResponseGroup = criteria.ResponseGroup | CatalogSearchResponseGroup.WithProducts;

            var workContext = _workContextFactory();
            var searchCriteria = criteria.ToServiceModel(workContext);
            var result = await _searchApi.SearchModuleSearchAsync(searchCriteria);
            var products = result.Products.Select(x => x.ToWebModel(workContext.CurrentLanguage, workContext.CurrentCurrency, workContext.CurrentStore)).ToList();

            if (!products.IsNullOrEmpty())
            {
                var taskList = new List<Task>();
                taskList.Add(LoadProductsInventoriesAsync(products));
                taskList.Add(_pricingService.EvaluateProductPricesAsync(products));
                await Task.WhenAll(taskList.ToArray());
            }

            return new CatalogSearchResult
            {
                Products = new StaticPagedList<Product>(products, criteria.PageNumber, criteria.PageSize, result.ProductsTotalCount.Value),
                Aggregations = !result.Aggregations.IsNullOrEmpty() ? result.Aggregations.Select(x => x.ToWebModel(workContext.CurrentLanguage.CultureName)).ToArray() : new Aggregation[] { }
            };
        }
        public async Task<CatalogSearchResult> SearchAsync(CatalogSearchCriteria criteria)
        {
            var retVal = new CatalogSearchResult();

            string sort = "manual";
            string sortOrder = "asc";

            if (!string.IsNullOrEmpty(criteria.SortBy))
            {
                var splittedSortBy = criteria.SortBy.Split('-');
                if (splittedSortBy.Length > 1)
                {
                    sort = splittedSortBy[0].Equals("title", StringComparison.OrdinalIgnoreCase) ? "name" : splittedSortBy[0];
                    sortOrder = splittedSortBy[1].IndexOf("descending", StringComparison.OrdinalIgnoreCase) >= 0 ? "desc" : "asc";
                }
            }

            var result = await _searchApi.SearchModuleSearchAsync(
                criteriaStoreId: _workContext.CurrentStore.Id,
                criteriaKeyword: criteria.Keyword,
                criteriaResponseGroup: criteria.ResponseGroup.ToString(),
                criteriaSearchInChildren: true,
                criteriaCategoryId: criteria.CategoryId,
                criteriaCatalogId: criteria.CatalogId,
                criteriaCurrency: _workContext.CurrentCurrency.Code,
                criteriaHideDirectLinkedCategories: true,
                criteriaTerms: criteria.Terms.ToStrings(),
                criteriaPricelistIds: _workContext.CurrentPriceListIds.ToList(),
                criteriaSkip: criteria.PageSize * (criteria.PageNumber - 1),
                criteriaTake: criteria.PageSize,
                criteriaSort: sort,
                criteriaSortOrder: sortOrder);

            if (criteria.CategoryId != null)
            {
                var category = await _catalogModuleApi.CatalogModuleCategoriesGetAsync(criteria.CategoryId);
                if (category != null)
                {
                    retVal.Category = category.ToWebModel();
                }
            }

            if (result != null)
            {
                if (result.Products != null && result.Products.Any())
                {
                    var products = result.Products.Select(x => x.ToWebModel(_workContext.CurrentLanguage, _workContext.CurrentCurrency)).ToArray();
                    retVal.Products = new StorefrontPagedList<Product>(products, criteria.PageNumber, criteria.PageSize, result.ProductsTotalCount.Value, page => _workContext.RequestUrl.SetQueryParameter("page", page.ToString()).ToString());

                    LoadProductsPrices(retVal.Products.ToArray());
                    LoadProductsInventories(retVal.Products.ToArray());
                }

                if (result.Categories != null && result.Categories.Any())
                {
                    retVal.Categories = result.Categories.Select(x => x.ToWebModel());
                }

                if (result.Aggregations != null)
                {
                    retVal.Aggregations = result.Aggregations.Select(x => x.ToWebModel()).ToArray();
                }
            }

            return retVal;
        }