コード例 #1
0
        /// <summary>
        /// Converts the FacetResult object to a <see cref="Category"/> object.
        /// </summary>
        /// <param name="facetResult">The FacetResult object.</param>
        /// <returns></returns>
        public static Category ToCategory(this FacetResult facetResult)
        {
            if (facetResult == null)
                throw new ArgumentNullException(nameof(facetResult));

            var category = new Category
            {
                Name = facetResult.Dim,
                Count = facetResult.Value.intValue()
            };

            if (category.Count < 0)
                category.Count = null;

            if (facetResult.ChildCount > 0)
            {
                category.Values = new List<Category>();
                foreach (var value in facetResult.LabelValues)
                    category.Values.Add(new Category { Name = value.Label, Count = value.Value.intValue() });

                category.Count = category.Values.Sum(sc => sc.Count);
            }

            return category;
        }
コード例 #2
0
        /// <summary>
        /// Gets the Categories from the Facets object.
        /// </summary>
        /// <param name="facets">The Facets.</param>
        /// <param name="topNCategories">The top N categories to extract.</param>
        /// <param name="selectedFacets">The selected facets.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException">{nameof(topNCategories)}</exception>
        public static IEnumerable<Category> GetCategories(this Facets facets, int topNCategories, IEnumerable<FacetField> selectedFacets)
        {
            if (facets == null)
                throw new ArgumentException(nameof(facets));
            if (topNCategories <= 0)
                throw new ArgumentException($"{nameof(topNCategories)} cannot be zero or less.");
            if (selectedFacets == null)
                throw new ArgumentException(nameof(selectedFacets));

            var categories = new List<Category>();
            var allDims = facets.GetAllDims(topNCategories);

            for (var i = 0; i < allDims.size(); i++)
            {
                // Get the category (facet) names
                var fc = allDims.get(i) as FacetResult;
                if (fc != null)
                {
                    var category = fc.ToCategory();

                    // Check if the current category/facet is one of the categories
                    // that the user wants to drill-down to.

                    var facetToDrillDownTo = selectedFacets.FirstOrDefault(f => f.Dim == category.Name);
                    if (facetToDrillDownTo != null &&
                        (facetToDrillDownTo.Path?.Length ?? 0) > 0)
                    {
                        // If yes, then we want to get the names and counts of all category child values.
                        // Do this by traversing the facet path.

                        var currentCategory = category;
                        for (var j = 0; j < facetToDrillDownTo.Path.Length; j++)
                        {
                            var currentFacetPath = facetToDrillDownTo.Path.Take(j + 1).ToArray();
                            var childFacetResult = facets.GetTopChildren(topNCategories, facetToDrillDownTo.Dim, currentFacetPath);
                            if (childFacetResult == null)
                                break;

                            if (childFacetResult.ChildCount > 0)
                            {
                                var childName = currentFacetPath.Last();
                                if (currentCategory.Values == null)
                                    currentCategory.Values = new List<Category>();

                                var childCategory = currentCategory.Values.FirstOrDefault(c => c.Name == childName);
                                if (childCategory == null)
                                {
                                    childCategory = new Category { Name = childName };
                                    currentCategory.Values.Add(childCategory);
                                }

                                if (childCategory.Values == null)
                                    childCategory.Values = new List<Category>();

                                foreach (var lv in childFacetResult.LabelValues)
                                    childCategory.Values.Add(new Category { Name = lv.Label, Count = lv.Value.intValue() });

                                childCategory.Count = childCategory.Values.Sum(sc => sc.Count);

                                currentCategory = childCategory;
                            }
                        }
                    }

                    categories.Add(category);
                }
            }

            return categories;
        }