@@ -17,14 +17,18 @@ public function processBucketAggregations($bucketAggregations, $rawAggs): array
1717 protected function parseBucket ($ bucketAggregation , $ rawAggs )
1818 {
1919 $ key = $ bucketAggregation ['key ' ];
20+ $ type = $ bucketAggregation ['type ' ] ?? null ;
2021
2122 if (! isset ($ rawAggs [$ key ]['buckets ' ])) {
2223 return $ rawAggs [$ key ];
2324 }
24-
25- $ result = collect ($ rawAggs [$ key ]['buckets ' ])->map (function ($ bucket ) use ($ key ) {
25+ $ result = collect ($ rawAggs [$ key ]['buckets ' ])->map (function ($ bucket ) use ($ key , $ type ) {
2626 $ metricAggs = $ this ->appendMetricsToBucket ($ bucket );
27- // ES is super annoying with how it does keys. For composite, it returns keys as an array but in other cases it does not.
27+
28+ if ($ type === 'range ' ) {
29+ return $ this ->makeRangeBucket ($ bucket , $ metricAggs );
30+ }
31+
2832 if (! is_array ($ bucket ['key ' ])) {
2933 $ bucket ['key ' ] = [$ key => $ bucket ['key ' ]];
3034 }
@@ -40,6 +44,24 @@ protected function parseBucket($bucketAggregation, $rawAggs)
4044 return $ result ->toArray ();
4145 }
4246
47+ protected function makeRangeBucket ($ bucket , $ metricAggs )
48+ {
49+ $ cleanBucket = [
50+ 'key ' => $ bucket ['key ' ],
51+ 'from ' => $ bucket ['from ' ] ?? null ,
52+ 'to ' => $ bucket ['to ' ] ?? null ,
53+ ];
54+ $ docCount = $ bucket ['doc_count ' ] ?? 0 ;
55+ unset($ bucket ['doc_count ' ]);
56+ $ cleanBucket ['count ' ] = $ docCount ;
57+
58+ return [
59+ ...$ cleanBucket ,
60+ ...$ metricAggs ,
61+ '_meta ' => $ this ->metaFromResult (['doc_count ' => $ docCount ]),
62+ ];
63+ }
64+
4365 protected function appendMetricsToBucket ($ bucket )
4466 {
4567 if (! $ this ->query ->metricsAggregations ) {
0 commit comments