Beispiel #1
0
        public static FacetSetting ConvertToFacetSetting(IFacet facet, List <IFacet> dependsOn = null, List <IPromotedFacetValueSetting> promotedFacetValueSettings = null)
        {
            if (facet == null)
            {
                throw new ArgumentNullException(nameof(facet));
            }
            if (dependsOn == null)
            {
                dependsOn = new List <IFacet>();
            }
            if (promotedFacetValueSettings == null)
            {
                promotedFacetValueSettings = new List <IPromotedFacetValueSetting>();
            }

            var facetSetting = new FacetSetting(facet.FieldName)
            {
                FacetType              = GetFacetType(facet),
                FacetValueType         = GetFacetValueType(facet),
                SortWeight             = (double)facet.SortWeight,
                MaxCollapsedValueCount = facet.MaxCollapsedValueCount,
                MaxExpendedValueCount  = facet.MaxExpendedValueCount.GetValueOrDefault(int.MaxValue),
                DependsOn              = dependsOn.Select(f => f.FieldName).ToList(),
                // StartValue = facet.StartValue,
                // EndValue = facet.EndValue,
                // GapSize = facet.GapSize,
                IsDisplayed     = facet.IsDisplayed,
                PromotedValues  = promotedFacetValueSettings.Select(ConvertToPromotedFacetValueSetting).ToList(),
                IsCategoryFacet = facet.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix)
            };

            return(facetSetting);
        }
        protected override Facets.Facet BuildFacet(
            Overture.ServiceModel.Search.Facet facetResult,
            FacetSetting setting,
            List <FacetValue> facetValues,
            CultureInfo cultureInfo)
        {
            var facet = new Facets.Facet
            {
                Title       = facetResult.Title,
                FieldName   = facetResult.FieldName,
                Quantity    = facetValues.Sum(value => value.Quantity),
                FacetType   = FacetType,
                SortWeight  = setting.SortWeight,
                StartValue  = facetResult.StartValue,
                EndValue    = facetResult.EndValue,
                GapSize     = facetResult.GapSize,
                IsDisplayed = setting.IsDisplayed
            };

            //In order to always see selected facet values
            var selectedValueCount     = facetValues.Count(x => x.IsSelected);
            var maxCollapsedValueCount = selectedValueCount > setting.MaxCollapsedValueCount
                ? selectedValueCount
                : setting.MaxCollapsedValueCount;

            facet.FacetValues         = facetValues.Take(maxCollapsedValueCount).ToList();
            facet.OnDemandFacetValues = facetValues
                                        .Skip(maxCollapsedValueCount)
                                        .Take(setting.MaxExpendedValueCount - setting.MaxCollapsedValueCount)
                                        .ToList();

            return(facet);
        }
Beispiel #3
0
        protected List <CategoryFacetValuesTreeNode> GetTreeNodes(FacetSetting facetSetting, IList <Facet> facets, SelectedFacets selectedFacets,
                                                                  TreeNode <Overture.ServiceModel.Products.Category> categoryTree, CultureInfo culture, CategoryFacetCounts counts)
        {
            var categoryChildren       = categoryTree.Children;
            var categoryChildrenLookup = categoryChildren.ToLookup(_ => _.Value.DisplayName.GetLocalizedValue(culture.Name));
            var facet = facets.FirstOrDefault(f => f.FieldName == facetSetting.FieldName);
            var countsForFacetValues = counts?.Facets?.FirstOrDefault(fc => facetSetting.FieldName.StartsWith(fc.FieldName))?.FacetValues;

            List <CategoryFacetValuesTreeNode> nodes = null;

            var facetValues = facet?.FacetValues.Concat(facet.OnDemandFacetValues);

            if (facetValues != null)
            {
                nodes = (from fv in facetValues
                         let category = categoryChildrenLookup[fv.Value].FirstOrDefault()
                                        where category != null
                                        let totalCount = countsForFacetValues?.FirstOrDefault(fcv => fcv.Value.Equals(category.Value.Id, StringComparison.OrdinalIgnoreCase))?.Quantity
                                                         select new CategoryFacetValuesTreeNode(fv.Title, fv.Value, totalCount != null ? totalCount.Value : fv.Quantity, facetSetting.FacetType, facetSetting.FieldName, fv.IsSelected, fv.IsRemovable)
                {
                    CategoryId = category.Value.Id
                }).ToList();
            }

            if (nodes == null || nodes.Count == 0)
            {
                var selected = selectedFacets.Facets.Where(f => f.FieldName == facetSetting.FieldName);
                if (selected != null && selected.Count() > 0)
                {
                    nodes = (from fv in selected
                             let category = categoryChildrenLookup[fv.Value].FirstOrDefault()
                                            where category != null
                                            let totalCount = countsForFacetValues?.FirstOrDefault(fcv => fcv.Value.Equals(category.Value.Id, StringComparison.OrdinalIgnoreCase))?.Quantity
                                                             select new CategoryFacetValuesTreeNode(fv.DisplayName, fv.Value, totalCount != null ? totalCount.Value : 0, facetSetting.FacetType, facetSetting.FieldName, true, fv.IsRemovable)
                    {
                        CategoryId = category.Value.Id
                    }).ToList();
                }
            }

            return(nodes?.OrderByDescending(n => n.Quantity).ToList());
        }
        protected virtual List <FacetValue> GetFacetValues(
            Overture.ServiceModel.Search.Facet facetResult, FacetSetting setting,
            IReadOnlyCollection <string> selectedFacetValues, CultureInfo cultureInfo)
        {
            var promotedValues = setting.PromotedValues;

            /*Expected to be already sorted*/
            var facetValues = facetResult.Values
                              .Select(resultFacetValue =>
            {
                var facetValue = new FacetValue
                {
                    Title        = FacetLocalizationProvider.GetFormattedFacetValueTitle(facetResult.FieldName, resultFacetValue.Value, cultureInfo),
                    Value        = resultFacetValue.Value,
                    Quantity     = resultFacetValue.Count,
                    IsSelected   = selectedFacetValues.Contains(resultFacetValue.Value),
                    MinimumValue = resultFacetValue.MinimumValue,
                    MaximumValue = resultFacetValue.MaximumValue
                };

                var promotedValueSetting = promotedValues.FirstOrDefault(
                    value => value.Title.Equals(resultFacetValue.Value, StringComparison.OrdinalIgnoreCase));

                if (promotedValueSetting != null)
                {
                    facetValue.IsPromoted          = true;
                    facetValue.PromotionSortWeight = promotedValueSetting.SortWeight;
                }

                return(facetValue);
            })
                              .ToList();

            facetValues = facetValues.OrderByDescending(x => x.IsSelected).ToList();
            return(facetValues);
        }
        /// <summary>
        ///     Creates a new list of a <see cref="Facets.SelectedFacet" /> from a <see cref="filter" /> object.
        /// </summary>
        /// <param name="filter">Facet to create the facet predicate from.</param>
        /// <param name="setting">Settings of the facet</param>
        /// <param name="cultureInfo">Culture in which the display names will be returned in</param>
        public IEnumerable <Facets.SelectedFacet> CreateSelectedFacetList(SearchFilter filter, FacetSetting setting,
                                                                          CultureInfo cultureInfo)
        {
            if (filter == null)
            {
                throw new ArgumentNullException("filter");
            }
            if (setting == null)
            {
                throw new ArgumentNullException("setting");
            }
            if (!setting.FieldName.Equals(filter.Name, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                          string.Format(
                              "The specified setting is for the facet '{0}', whereas the filter is for the facet '{1}'",
                              setting.FieldName, filter.Name), "setting");
            }
            if (setting.FacetType != FacetType)
            {
                throw new ArgumentException(
                          string.Format("The facetResult is defined as '{0}' which does not match '{1}'", setting.FacetType,
                                        FacetType), "setting");
            }

            return(new List <Facets.SelectedFacet>
            {
                new Facets.SelectedFacet
                {
                    FieldName = filter.Name,
                    DisplayName = FacetLocalizationProvider.GetFormattedFacetValueTitle(filter.Name, filter.Value, cultureInfo),
                    FacetType = FacetType,
                    IsRemovable = !filter.IsSystem,
                    Value = filter.Value
                }
            });
        }
 /// <summary>
 ///     Creates a new instance of a <see cref="Facets.Facet" /> from a <see cref="facetResult" /> object.
 /// </summary>
 /// <param name="facetResult">Facet to create the facet predicate from.</param>
 /// <param name="setting">Settings of the facet</param>
 /// <param name="selectedFacets">List of selected facet to determine if the values of the facet were selected or not</param>
 /// <param name="cultureInfo">Culture in which the display names will be returned in</param>
 public Facet CreateFacet(Overture.ServiceModel.Search.Facet facetResult, FacetSetting setting,
                          IReadOnlyList <SearchFilter> selectedFacets, CultureInfo cultureInfo)
 {
     return(new Facet());
 }
        /// <summary>
        ///     Creates a new list of a <see cref="Facets.SelectedFacet" /> from a <see cref="filter" /> object.
        /// </summary>
        /// <param name="filter">Facet to create the facet predicate from.</param>
        /// <param name="setting">Settings of the facet</param>
        /// <param name="cultureInfo">Culture in which the display names will be returned in</param>
        public IEnumerable <Facets.SelectedFacet> CreateSelectedFacetList(SearchFilter filter, FacetSetting setting,
                                                                          CultureInfo cultureInfo)
        {
            if (filter == null)
            {
                throw new ArgumentNullException("filter");
            }
            if (setting == null)
            {
                throw new ArgumentNullException("setting");
            }
            if (!setting.FieldName.Equals(filter.Name, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                          string.Format(
                              "The specified setting is for the facet '{0}', whereas the filter is for the facet '{1}'",
                              setting.FieldName, filter.Name), "setting");
            }
            if (setting.FacetType != FacetType)
            {
                throw new ArgumentException(
                          string.Format("The facetResult is defined as '{0}' which does not match '{1}'", setting.FacetType,
                                        FacetType), "setting");
            }

            var rangeValues = filter.Value.Split(SearchConfiguration.FacetRangeValueSplitter);
            var minValue    = rangeValues[0];
            var maxValue    = rangeValues.Length > 1 ? rangeValues[1] : null;

            return(new List <Facets.SelectedFacet>
            {
                new Facets.SelectedFacet
                {
                    FieldName = filter.Name,
                    DisplayName = FacetLocalizationProvider.GetFormattedRangeFacetValues(filter.Name, minValue, maxValue, setting.FacetValueType, cultureInfo),
                    FacetType = FacetType,
                    IsRemovable = !filter.IsSystem,
                    MinimumValue = minValue,
                    MaximumValue = maxValue
                }
            });
        }
        /// <summary>
        /// Creates a new instance of a <see cref="Facet"/> from a <see cref="facetResult"/> object.
        /// </summary>
        /// <param name="facetResult">Facet to create the facet predicate from.</param>
        /// <param name="setting">Settings of the facet</param>
        /// <param name="selectedFacets">List of selected facet to determine if the values of the facet were selected or not</param>
        /// <param name="cultureInfo">Culture in which the display names will be returned in</param>
        public virtual Facets.Facet CreateFacet(Overture.ServiceModel.Search.Facet facetResult, FacetSetting setting, IReadOnlyList <SearchFilter> selectedFacets, CultureInfo cultureInfo)
        {
            if (facetResult == null)
            {
                throw new ArgumentNullException(nameof(facetResult));
            }
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }

            if (!setting.FieldName.Equals(facetResult.FieldName, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                          string.Format("The specified setting is for the facet '{0}', whereas the facetResult is for the facet '{1}'",
                                        setting.FieldName, facetResult.FieldName), nameof(setting));
            }

            if (setting.FacetType != FacetType)
            {
                throw new ArgumentException(
                          string.Format("The facetResult is defined as '{0}' which does not match '{1}'",
                                        setting.FacetType, FacetType), nameof(setting));
            }

            var selectedFacetValues = GetSelectedFacetValues(facetResult, selectedFacets);

            // For field facets, return only those whose values were not all selected
            if (HasAllFacetValuesSelected(facetResult, selectedFacetValues))
            {
                return(null);
            }

            var facetValues = GetFacetValues(facetResult, setting, selectedFacetValues, cultureInfo);

            var facet = BuildFacet(facetResult, setting, facetValues, cultureInfo);

            return(facet);
        }
        protected virtual Facets.Facet BuildFacet(Overture.ServiceModel.Search.Facet facetResult, FacetSetting setting, List <FacetValue> facetValues, CultureInfo cultureInfo)
        {
            var facet = new Facets.Facet
            {
                Title       = facetResult.Title,
                FieldName   = facetResult.FieldName,
                Quantity    = facetValues.Count,
                FacetType   = FacetType,
                SortWeight  = setting.SortWeight,
                IsDisplayed = !facetValues.Any(value => value.IsPromoted) && setting.IsDisplayed
            };

            //In order to always see selected facet values
            var selectedValueIndex    = facetValues.FindLastIndex(x => x.IsSelected);
            var maxExpendedValueCount = selectedValueIndex > setting.MaxExpendedValueCount
                ? selectedValueIndex
                : (setting.MaxExpendedValueCount - setting.MaxCollapsedValueCount);

            facet.FacetValues         = facetValues.Take(setting.MaxCollapsedValueCount).ToList();
            facet.OnDemandFacetValues = facetValues
                                        .Skip(setting.MaxCollapsedValueCount)
                                        .Take(maxExpendedValueCount)
                                        .ToList();

            return(facet);
        }
 /// <summary>
 ///     Creates a new list of a <see cref="Facets.SelectedFacet" /> from a <see cref="filter" /> object.
 /// </summary>
 /// <param name="filter">Facet to create the facet predicate from.</param>
 /// <param name="setting">Settings of the facet</param>
 /// <param name="cultureInfo">Culture in which the display names will be returned in</param>
 public IEnumerable <SelectedFacet> CreateSelectedFacetList(SearchFilter filter, FacetSetting setting,
                                                            CultureInfo cultureInfo)
 {
     return(new List <SelectedFacet> {
         new SelectedFacet()
     });
 }