protected virtual SearchAvailableProductsBaseRequest CreateSearchRequest(SearchCriteria criteria)
        {
            var request = ProductRequestFactory.CreateProductRequest(criteria);

            request.Query.IncludeTotalCount = true;
            request.Query.MaximumItems      = criteria.NumberOfItemsPerPage;
            request.Query.StartingIndex     = (criteria.Page - 1) * criteria.NumberOfItemsPerPage;
            request.CultureName             = criteria.CultureInfo.Name;
            request.SearchTerms             = criteria.Keywords;
            request.ScopeId              = criteria.Scope;
            request.IncludeFacets        = criteria.IncludeFacets;
            request.Facets               = GetFacetFieldNameToQuery(criteria);
            request.FacetPredicates      = BuildFacetPredicates(criteria);
            request.InventoryLocationIds = criteria.InventoryLocationIds;
            request.AutoCorrect          = criteria.AutoCorrect;

            var sortDefinitions = BuildQuerySortings(criteria);

            if (sortDefinitions != null)
            {
                request.Query.Sortings.Add(sortDefinitions);
            }

            return(request);
        }
        public virtual Task <ProductSearchResult> GetCategoryFacetCountsAsync(SearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }
            if (criteria.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(criteria.CultureInfo)), nameof(criteria));
            }
            if (string.IsNullOrWhiteSpace(criteria.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(criteria.Scope)), nameof(criteria));
            }

            var request = ProductRequestFactory.CreateProductRequest(criteria.Scope);

            request.Query.IncludeTotalCount = true;
            request.Query.MaximumItems      = 0;
            request.Query.StartingIndex     = 0;
            request.CultureName             = criteria.CultureInfo.Name;
            request.SearchTerms             = criteria.Keywords;
            request.ScopeId       = criteria.Scope;
            request.IncludeFacets = criteria.IncludeFacets;

            var facetsForCounts = FacetConfigContext.GetFacetSettings()
                                  .Where(fs => fs.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix))
                                  .Select(f => f.FieldName.Replace("_Facet", ""));
            var facets = GetFacetFieldNameToQuery(criteria);

            facets.AddRange(facetsForCounts);
            request.Facets = facets;
            if (criteria.SelectedFacets != null)
            {
                request.FacetPredicates = criteria.SelectedFacets
                                          .Where(sf => !sf.Name.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix))
                                          .Select(FacetPredicateFactory.CreateFacetPredicate)
                                          .Where(fp => fp != null).ToList();
            }
            request.InventoryLocationIds = criteria.InventoryLocationIds;
            request.AutoCorrect          = criteria.AutoCorrect;
            request.AvailabilityDate     = criteria.AvailabilityDate;

            return(ExecuteProductSearchRequestAsync(request));
        }