Пример #1
0
        public override ITypeSearch <FindProduct> ApplyFilter(ITypeSearch <FindProduct> query)
        {
            if (string.IsNullOrEmpty(FieldName) == false)
            {
                string fullFieldName = query.Client.GetFullFieldName(FieldName, typeof(double));
                switch (FieldOperator)
                {
                case NumericOperatorSelectionFactory.OperatorNames.GreaterThan:
                    RangeFilter <double> greaterThanFilter = RangeFilter.Create(fullFieldName, FieldValue, double.MaxValue);
                    greaterThanFilter.IncludeLower = false;
                    greaterThanFilter.IncludeUpper = true;
                    query = query.Filter(greaterThanFilter);
                    break;

                case NumericOperatorSelectionFactory.OperatorNames.LessThan:
                    RangeFilter <double> lessThanFilter = RangeFilter.Create(fullFieldName, double.MinValue, FieldValue);
                    lessThanFilter.IncludeLower = false;
                    lessThanFilter.IncludeUpper = true;
                    query = query.Filter(lessThanFilter);
                    break;

                default:
                case NumericOperatorSelectionFactory.OperatorNames.Equal:
                    var termFilter = new TermFilter(fullFieldName, FieldValue);
                    query = query.Filter(termFilter);
                    break;
                }
            }
            return(query);
        }
Пример #2
0
        private static FilterBuilder <T> GetOrFilterForNumericRange <T>(ITypeSearch <T> query,
                                                                        IEnumerable <SelectableNumericRange> range,
                                                                        string fieldName,
                                                                        Type type)
        {
            // Appends type convention to field name (like "$$string")
            var client        = query.Client;
            var fullFieldName = client.GetFullFieldName(fieldName, type);

            var filters = new List <Filter>();

            foreach (var rangeItem in range)
            {
                var rangeFilter = RangeFilter.Create(fullFieldName,
                                                     rangeItem.From ?? 0,
                                                     rangeItem.To ?? double.MaxValue);
                rangeFilter.IncludeUpper = false;
                filters.Add(rangeFilter);
            }


            var orFilter      = new OrFilter(filters);
            var filterBuilder = new FilterBuilder <T>(client, orFilter);

            return(filterBuilder);
        }
Пример #3
0
        public override Filter GetFilter()
        {
            if (string.IsNullOrEmpty(FieldName))
            {
                return(null);
            }

            var fullFieldName = SearchClient.Instance.GetFullFieldName(FieldName, typeof(double));

            switch (FieldOperator)
            {
            case NumericOperatorSelectionFactory.OperatorNames.GreaterThan:
                var greaterThanFilter = RangeFilter.Create(fullFieldName, FieldValue, double.MaxValue);
                greaterThanFilter.IncludeLower = false;
                greaterThanFilter.IncludeUpper = true;
                return(greaterThanFilter);

            case NumericOperatorSelectionFactory.OperatorNames.LessThan:
                var lessThanFilter = RangeFilter.Create(fullFieldName, double.MinValue, FieldValue);
                lessThanFilter.IncludeLower = false;
                lessThanFilter.IncludeUpper = true;
                return(lessThanFilter);

            default:
                return(new TermFilter(fullFieldName, FieldValue));
            }
        }
        private IEnumerable <EPiServer.Find.Api.Querying.Filter> GetFilters(Commerce.Models.Blocks.ProductSearchBlock productSearchBlock)
        {
            var filters = new List <EPiServer.Find.Api.Querying.Filter>();

            if (productSearchBlock.Nodes?.FilteredItems != null && productSearchBlock.Nodes.FilteredItems.Any())
            {
                var nodes          = productSearchBlock.Nodes.FilteredItems.Select(x => x.GetContent()).OfType <NodeContent>().ToList();
                var outlines       = nodes.Select(x => _searchService.GetOutline(x.Code)).ToList();
                var outlineFilters = outlines.Select(s => new PrefixFilter("Outline$$string.lowercase", s.ToLowerInvariant()))
                                     .ToList();

                if (outlineFilters.Count == 1)
                {
                    filters.Add(outlineFilters.First());
                }
                else
                {
                    filters.Add(new OrFilter(outlineFilters.ToArray()));
                }
            }

            if (productSearchBlock.MinPrice > 0 || productSearchBlock.MaxPrice > 0)
            {
                var rangeFilter = RangeFilter.Create("DefaultPrice$$number",
                                                     productSearchBlock.MinPrice.ToString(),
                                                     productSearchBlock.MaxPrice == 0 ? double.MaxValue.ToString() : productSearchBlock.MaxPrice.ToString());
                rangeFilter.IncludeUpper = true;
                filters.Add(rangeFilter);
            }

            if (productSearchBlock.BrandFilter != null)
            {
                var brands       = productSearchBlock.BrandFilter.Split(',');
                var brandFilters = brands.Select(s => new PrefixFilter("Brand$$string.lowercase", s.ToLowerInvariant())).ToList();
                if (brandFilters.Count == 1)
                {
                    filters.Add(brandFilters.First());
                }
                else
                {
                    filters.Add(new OrFilter(brandFilters.ToArray()));
                }
            }

            if (productSearchBlock.Filters == null)
            {
                return(filters);
            }
            foreach (var item in productSearchBlock.Filters.FilteredItems)
            {
                if (item.GetContent() is FilterBaseBlock filter)
                {
                    filters.Add(filter.GetFilter());
                }
            }
            return(filters);
        }
Пример #5
0
        private Filter CreateLessThanFilter(string fieldName, Expression valueExpression, bool includeUpper)
        {
            var valueGetter = Expression.Lambda(valueExpression, false, null).Compile();
            var value       = valueGetter.DynamicInvoke();

            if (value is string)
            {
            }
            else if (value is int)
            {
                var rangeFilter = RangeFilter.Create(fieldName, Int32.MinValue, (int)value);
                rangeFilter.IncludeUpper = includeUpper;
                return(rangeFilter);
            }
            else if (value is DateTime)
            {
                var rangeFilter = RangeFilter.Create(fieldName, DateTime.MinValue, (DateTime)value);
                rangeFilter.IncludeUpper = includeUpper;
                return(rangeFilter);
            }
            return(null);
        }
Пример #6
0
        private Filter GetSelectedFilter(List <FacetGroupOption> options, string currentField)
        {
            var filters = new List <Filter>();
            var facets  = _facetRegistry.GetFacetDefinitions();

            foreach (var facetGroupOption in options)
            {
                if (facetGroupOption.GroupFieldName.Equals(currentField))
                {
                    continue;
                }

                var filter = facets.FirstOrDefault(x => x.FieldName.Equals(facetGroupOption.GroupFieldName));
                if (filter == null)
                {
                    continue;
                }

                if (!facetGroupOption.Facets.Any(x => x.Selected))
                {
                    continue;
                }

                if (filter is FacetStringDefinition)
                {
                    filters.Add(new TermsFilter(_findClient.GetFullFieldName(facetGroupOption.GroupFieldName, typeof(string)),
                                                facetGroupOption.Facets.Where(x => x.Selected).Select(x => FieldFilterValue.Create(x.Name))));
                }
                else if (filter is FacetStringListDefinition)
                {
                    var termFilters = facetGroupOption.Facets.Where(x => x.Selected)
                                      .Select(s => new TermFilter(facetGroupOption.GroupFieldName, FieldFilterValue.Create(s.Name)))
                                      .Cast <Filter>()
                                      .ToList();

                    filters.AddRange(termFilters);
                }
                else if (filter is FacetNumericRangeDefinition)
                {
                    var rangeFilters = filter as FacetNumericRangeDefinition;
                    foreach (var selectedRange in facetGroupOption.Facets.Where(x => x.Selected))
                    {
                        var rangeFilter = rangeFilters.Range.FirstOrDefault(x => x.Id.Equals(selectedRange.Key.Split(':')[1]));
                        if (rangeFilter == null)
                        {
                            continue;
                        }
                        filters.Add(RangeFilter.Create(_findClient.GetFullFieldName(facetGroupOption.GroupFieldName, typeof(double)),
                                                       rangeFilter.From ?? 0,
                                                       rangeFilter.To ?? double.MaxValue));
                    }
                }
            }

            if (!filters.Any())
            {
                return(null);
            }

            if (filters.Count == 1)
            {
                return(filters.FirstOrDefault());
            }

            var boolFilter = new BoolFilter();

            foreach (var filter in filters)
            {
                boolFilter.Should.Add(filter);
            }
            return(boolFilter);
        }