public async Task <IList <AggregationRequest> > GetAggregationRequestsAsync(ProductIndexedSearchCriteria criteria, FiltersContainer allFilters) { var result = new List <AggregationRequest>(); var browseFilters = await _browseFilterService.GetBrowseFiltersAsync(criteria); if (browseFilters != null) { foreach (var filter in browseFilters) { var existingFilters = allFilters.GetFiltersExceptSpecified(filter.Key); var attributeFilter = filter as AttributeFilter; var priceRangeFilter = filter as PriceRangeFilter; var rangeFilter = filter as RangeFilter; AggregationRequest aggregationRequest = null; IList <AggregationRequest> aggregationRequests = null; if (attributeFilter != null) { aggregationRequest = GetAttributeFilterAggregationRequest(attributeFilter, existingFilters); } else if (rangeFilter != null) { aggregationRequests = GetRangeFilterAggregationRequests(rangeFilter, existingFilters); } else if (priceRangeFilter != null && priceRangeFilter.Currency.EqualsInvariant(criteria.Currency)) { aggregationRequests = GetPriceRangeFilterAggregationRequests(priceRangeFilter, criteria, existingFilters); } if (aggregationRequest != null) { result.Add(aggregationRequest); } if (aggregationRequests != null) { result.AddRange(aggregationRequests.Where(f => f != null)); } } } return(result); }
public virtual async Task <FiltersContainer> GetTermFiltersAsync(ProductIndexedSearchCriteria criteria) { var result = new FiltersContainer(); var terms = criteria.GetTerms(); if (terms.Any()) { var browseFilters = await _browseFilterService.GetBrowseFiltersAsync(criteria); var filtersAndValues = browseFilters ?.Select(f => new { Filter = f, Values = f.GetValues() }) .ToList(); foreach (var term in terms) { var browseFilter = browseFilters?.SingleOrDefault(x => x.Key.EqualsInvariant(term.Key)); // Handle special filter term with a key = "tags", it contains just values and we need to determine which filter to use if (browseFilter == null && term.Key == "tags") { foreach (var termValue in term.Values) { // Try to find filter by value var filterAndValues = filtersAndValues?.FirstOrDefault(x => x.Values?.Any(v => v.Id.Equals(termValue)) == true); if (filterAndValues != null) { var filter = ConvertBrowseFilter(filterAndValues.Filter, term.Values, criteria); result.PermanentFilters.Add(filter); } else { // Unknown term values should produce empty result result.PermanentFilters.Add(new IdsFilter { Values = new[] { string.Empty } }); } } } else if (browseFilter != null) // Predefined filter { var filter = ConvertBrowseFilter(browseFilter, term.Values, criteria); result.RemovableFilters.Add(new KeyValuePair <string, IFilter>(browseFilter.Key, filter)); } else // Custom term { var filter = FiltersHelper.CreateTermFilter(term.Key, term.Values); result.PermanentFilters.Add(filter); } } } return(result); }