public static IBucket ToBucket(this Nest.IBucket bucket, IDictionary <string, object> parentData = null)
        {
            if (bucket is Nest.DateHistogramBucket dateHistogramBucket)
            {
                var kind = parentData != null && parentData.ContainsKey("@timezone") ? DateTimeKind.Unspecified : DateTimeKind.Utc;
                var date = new DateTime(_epochTicks + ((long)dateHistogramBucket.Key * TimeSpan.TicksPerMillisecond), kind);
                return(new DateHistogramBucket(date, dateHistogramBucket.Aggregations.ToAggregations())
                {
                    Total = dateHistogramBucket.DocCount,
                    Key = dateHistogramBucket.Key,
                    KeyAsString = date.ToString("O")
                });
            }

            if (bucket is Nest.RangeBucket rangeBucket)
            {
                return new KeyedBucket <string>(rangeBucket.Aggregations.ToAggregations())
                       {
                           Total       = rangeBucket.DocCount,
                           Key         = rangeBucket.Key,
                           KeyAsString = rangeBucket.Key
                       }
            }
            ;

            if (bucket is Nest.KeyedBucket <string> stringKeyedBucket)
            {
                return new KeyedBucket <string>(stringKeyedBucket.Aggregations.ToAggregations())
                       {
                           Total       = stringKeyedBucket.DocCount,
                           Key         = stringKeyedBucket.Key,
                           KeyAsString = stringKeyedBucket.KeyAsString
                       }
            }
            ;

            if (bucket is Nest.KeyedBucket <double> doubleKeyedBucket)
            {
                return new KeyedBucket <double>(doubleKeyedBucket.Aggregations.ToAggregations())
                       {
                           Total       = doubleKeyedBucket.DocCount,
                           Key         = doubleKeyedBucket.Key,
                           KeyAsString = doubleKeyedBucket.KeyAsString
                       }
            }
            ;

            if (bucket is Nest.KeyedBucket <object> objectKeyedBucket)
            {
                return new KeyedBucket <object>(objectKeyedBucket.Aggregations.ToAggregations())
                       {
                           Total       = objectKeyedBucket.DocCount,
                           Key         = objectKeyedBucket.Key,
                           KeyAsString = objectKeyedBucket.KeyAsString
                       }
            }
            ;

            return(null);
        }
        public static IBucket ToBucket(this Nest.IBucket bucket, IDictionary <string, object> parentData = null)
        {
            if (bucket is Nest.DateHistogramBucket dateHistogramBucket)
            {
                bool hasTimezone = parentData != null && parentData.ContainsKey("@timezone");
                var  kind        = hasTimezone ? DateTimeKind.Unspecified : DateTimeKind.Utc;
                var  date        = new DateTime(_epochTicks + ((long)dateHistogramBucket.Key * TimeSpan.TicksPerMillisecond), kind);
                var  data        = new Dictionary <string, object> {
                    { "@type", "datehistogram" }
                };

                if (hasTimezone)
                {
                    data.Add("@timezone", parentData["@timezone"]);
                }

                return(new DateHistogramBucket(date, dateHistogramBucket.Aggregations.ToAggregations())
                {
                    Total = dateHistogramBucket.DocCount,
                    Key = dateHistogramBucket.Key,
                    KeyAsString = date.ToString("O"),
                    Data = data
                });
            }

            if (bucket is Nest.RangeBucket rangeBucket)
            {
                return new RangeBucket(rangeBucket.Aggregations.ToAggregations())
                       {
                           Total        = rangeBucket.DocCount,
                           Key          = rangeBucket.Key,
                           From         = rangeBucket.From,
                           FromAsString = rangeBucket.FromAsString,
                           To           = rangeBucket.To,
                           ToAsString   = rangeBucket.ToAsString,
                           Data         = new Dictionary <string, object> {
                               { "@type", "range" }
                           }
                       }
            }
            ;

            if (bucket is Nest.KeyedBucket <string> stringKeyedBucket)
            {
                return new KeyedBucket <string>(stringKeyedBucket.Aggregations.ToAggregations())
                       {
                           Total       = stringKeyedBucket.DocCount,
                           Key         = stringKeyedBucket.Key,
                           KeyAsString = stringKeyedBucket.KeyAsString,
                           Data        = new Dictionary <string, object> {
                               { "@type", "string" }
                           }
                       }
            }
            ;

            if (bucket is Nest.KeyedBucket <double> doubleKeyedBucket)
            {
                return new KeyedBucket <double>(doubleKeyedBucket.Aggregations.ToAggregations())
                       {
                           Total       = doubleKeyedBucket.DocCount,
                           Key         = doubleKeyedBucket.Key,
                           KeyAsString = doubleKeyedBucket.KeyAsString,
                           Data        = new Dictionary <string, object> {
                               { "@type", "double" }
                           }
                       }
            }
            ;

            if (bucket is Nest.KeyedBucket <object> objectKeyedBucket)
            {
                return new KeyedBucket <object>(objectKeyedBucket.Aggregations.ToAggregations())
                       {
                           Total       = objectKeyedBucket.DocCount,
                           Key         = objectKeyedBucket.Key,
                           KeyAsString = objectKeyedBucket.KeyAsString,
                           Data        = new Dictionary <string, object> {
                               { "@type", "object" }
                           }
                       }
            }
            ;

            return(null);
        }