Exemple #1
0
        public static FacetToken Create(FacetBase facet, Func <object, string> addQueryParameter)
        {
            var optionsParameterName = facet.Options != null && facet.Options != FacetOptions.Default ? addQueryParameter(facet.Options) : null;

            string        aggregateByField = null;
            List <string> ranges           = null;

            var aggregationFacet = facet.AsFacet();
            var rangeFacet       = facet.AsRangeFacet();

            if (aggregationFacet != null)
            {
                aggregateByField = aggregationFacet.FieldName;
            }

            if (rangeFacet != null)
            {
                ranges = rangeFacet.Ranges;
            }

            var token = new FacetToken(aggregateByField, facet.DisplayFieldName, ranges, optionsParameterName);

            foreach (var aggregation in facet.Aggregations)
            {
                FacetAggregationToken aggregationToken;
                switch (aggregation.Key)
                {
                case FacetAggregation.Max:
                    aggregationToken = FacetAggregationToken.Max(aggregation.Value);
                    break;

                case FacetAggregation.Min:
                    aggregationToken = FacetAggregationToken.Min(aggregation.Value);
                    break;

                case FacetAggregation.Average:
                    aggregationToken = FacetAggregationToken.Average(aggregation.Value);
                    break;

                case FacetAggregation.Sum:
                    aggregationToken = FacetAggregationToken.Sum(aggregation.Value);
                    break;

                default:
                    throw new NotSupportedException($"Unsupported aggregation method: {aggregation.Key}");
                }

                token._aggregations.Add(aggregationToken);
            }

            return(token);
        }
        private bool AreFacetsEqual(FacetBase left, FacetBase right)
        {
            if (left is Facet leftFacet)
            {
                var rightFacet = right.AsFacet();

                return(leftFacet.FieldName == rightFacet.FieldName);
            }

            var leftRangeFacet  = left.AsRangeFacet();
            var rightRangeFacet = right.AsRangeFacet();

            return(leftRangeFacet.Ranges.Count == rightRangeFacet.Ranges.Count &&
                   leftRangeFacet.Ranges.All(x => rightRangeFacet.Ranges.Contains(x)));
        }