@@ -8,18 +8,31 @@ trait ProcessesDistinctAggregations
88{
99 public function processDistinctAggregations ($ index , $ result , $ columns , $ withCount ): Collection
1010 {
11- if (! empty ($ result ['hits ' ]['hits ' ]) && is_array ($ result ['hits ' ]['hits ' ])) {
12- $ last = collect ($ result ['hits ' ]['hits ' ])->last ();
13- if (! empty ($ last ['sort ' ])) {
14- $ this ->query ->getMetaTransfer ()->set ('after_key ' , $ last ['sort ' ]);
15- }
16- }
11+ $ this ->pullAfterKey ($ result );
1712 $ keys = [];
1813 foreach ($ columns as $ column ) {
1914 $ keys [] = 'by_ ' .$ column ;
2015 }
16+ $ aggregations = $ this ->parseDistinctBucket ($ columns , $ keys , $ result ['aggregations ' ], 0 , $ withCount );
17+ $ aggregations = collect ($ aggregations );
2118
22- $ aggregations = collect ($ this ->parseDistinctBucket ($ columns , $ keys , $ result ['aggregations ' ], 0 , $ withCount ));
19+ return $ aggregations ->map (function ($ aggregation ) use ($ index ) {
20+ return $ this ->liftToMeta ($ aggregation , ['_index ' => $ index ], ['doc_count ' ]);
21+ });
22+ }
23+
24+ public function processBulkDistinctAggregations ($ index , $ result , $ columns , $ withCount ): Collection
25+ {
26+ $ this ->pullAfterKey ($ result );
27+ $ aggregations = [];
28+ foreach ($ columns as $ column ) {
29+ $ keys = ['by_ ' .$ column ];
30+ $ aggregations = [
31+ ...$ aggregations ,
32+ ...$ this ->parseDistinctBucket ([$ column ], $ keys , $ result ['aggregations ' ], 0 , $ withCount ),
33+ ];
34+ }
35+ $ aggregations = collect ($ aggregations );
2336
2437 return $ aggregations ->map (function ($ aggregation ) use ($ index ) {
2538 return $ this ->liftToMeta ($ aggregation , ['_index ' => $ index ], ['doc_count ' ]);
@@ -54,4 +67,14 @@ protected function parseDistinctBucket($columns, $keys, $response, $index, $incl
5467
5568 return $ data ;
5669 }
70+
71+ protected function pullAfterKey ($ result )
72+ {
73+ if (! empty ($ result ['hits ' ]['hits ' ]) && is_array ($ result ['hits ' ]['hits ' ])) {
74+ $ last = collect ($ result ['hits ' ]['hits ' ])->last ();
75+ if (! empty ($ last ['sort ' ])) {
76+ $ this ->query ->getMetaTransfer ()->set ('after_key ' , $ last ['sort ' ]);
77+ }
78+ }
79+ }
5780}
0 commit comments