private SearchAggregationItem GetBucketAggregation(Nest.KeyedBucket <object> aggregate) { var aggregationItem = new SearchAggregationItem(aggregate.Key.ToString()) { Count = aggregate.DocCount }; if (aggregate.Aggregations != null) { //Check aggregated data [BucketAggregate] rappresent nested nodes if (aggregate.Aggregations.Any(d => (d.Value as Nest.BucketAggregate) != null)) { aggregationItem.Items = GetAggregations(aggregate.Aggregations); } //Get all value aggregated data (Value Type (Sum, Avg,Count etc..) aggregate.Aggregations.Where(d => (d.Value as Nest.ValueAggregate) != null) .ForEach(dd => { aggregationItem.Items.Add(new SearchAggregationItem(dd.Key) { Count = (dd.Value as ValueAggregate).Value, }); }); } return(aggregationItem); }
private IList <SearchAggregationItem> GetAggregations(IReadOnlyDictionary <string, IAggregate> source) { List <SearchAggregationItem> aggregations = new List <SearchAggregationItem>(); foreach (var item in source) { SearchAggregationItem currentAgg = new SearchAggregationItem(item.Key); var currentBucket = item.Value as Nest.BucketAggregate; if (currentBucket != null && currentBucket.Items.Count() > 0) { foreach (var agg in currentBucket.Items) { var aggBucket = agg as Nest.KeyedBucket <object>; var aggregationItem = GetBucketAggregation(aggBucket); // If current bucket dosent have another aggregation // means all aggregated data are belonging to the current aggregation // *Otherwise the result belong to the Parent aggregation //ex : Distinct Count (Term) if (aggBucket.Aggregations == null) { currentAgg.Items.Add(aggregationItem); } else { aggregationItem.Parentkey = currentAgg.Key; aggregations.Add(aggregationItem); } } } else { var valueAgg = item.Value as Nest.ValueAggregate; currentAgg.Count = valueAgg.Value; aggregations.Add(currentAgg); } if (currentAgg.Items.Count() > 0) { aggregations.Add(currentAgg); } } return(aggregations); }