55
66package org .opensearch .sql .opensearch .response .agg ;
77
8- import java .util .HashMap ;
98import java .util .List ;
109import java .util .Map ;
1110import lombok .EqualsAndHashCode ;
1211import lombok .Getter ;
13- import org .opensearch .search .SearchHits ;
1412import org .opensearch .search .aggregations .Aggregation ;
1513import org .opensearch .search .aggregations .Aggregations ;
1614import org .opensearch .search .aggregations .bucket .MultiBucketsAggregation ;
1715import org .opensearch .search .aggregations .bucket .composite .CompositeAggregation ;
1816import org .opensearch .search .aggregations .bucket .range .Range ;
1917
18+ /**
19+ * Parser for bucket aggregations that contain sub-aggregations. This parser handles multiple levels
20+ * of multi-bucket aggregations by delegates sublevels to sub-parsers.
21+ *
22+ * <p>Please note that it does not handle metric or value count responses -- they should be parsed
23+ * only in {@link LeafBucketAggregationParser}.
24+ */
2025@ Getter
21- @ EqualsAndHashCode
22- public class BucketAggregationParser implements OpenSearchAggregationResponseParser {
23- private final OpenSearchAggregationResponseParser subAggParser ;
26+ @ EqualsAndHashCode (callSuper = false )
27+ public class BucketAggregationParser extends AbstractBucketAggregationParser {
28+ /** The sub-aggregation parser used to process nested aggregations within each bucket. */
29+ private final AbstractBucketAggregationParser subAggParser ;
2430
25- public BucketAggregationParser (OpenSearchAggregationResponseParser subAggParser ) {
31+ /**
32+ * Constructs a new BucketAggregationParser with the specified sub-aggregation parser.
33+ *
34+ * @param subAggParser the parser to handle sublevel multi-bucket aggregations within each bucket
35+ */
36+ public BucketAggregationParser (AbstractBucketAggregationParser subAggParser ) {
2637 this .subAggParser = subAggParser ;
2738 }
2839
40+ /**
41+ * Parses the provided aggregations into a list of maps containing the aggregated data. This
42+ * method handles multi-bucket aggregations by processing each bucket and merging the results with
43+ * bucket-specific key information.
44+ *
45+ * @param aggregations the aggregations to parse
46+ * @return a list of maps containing the parsed aggregation data
47+ * @throws IllegalStateException if the aggregation type is not supported or if the sub-parser
48+ * type is invalid
49+ */
2950 @ Override
3051 public List <Map <String , Object >> parse (Aggregations aggregations ) {
3152 if (subAggParser instanceof BucketAggregationParser ) {
@@ -51,21 +72,16 @@ public List<Map<String, Object>> parse(Aggregations aggregations) {
5172 private List <Map <String , Object >> parse (MultiBucketsAggregation .Bucket bucket , String name ) {
5273 List <Map <String , Object >> results = subAggParser .parse (bucket .getAggregations ());
5374 if (bucket instanceof CompositeAggregation .Bucket compositeBucket ) {
54- Map <String , Object > common = new HashMap <> (compositeBucket . getKey () );
75+ Map <String , Object > common = extract (compositeBucket );
5576 for (Map <String , Object > r : results ) {
5677 r .putAll (common );
5778 }
58- } else if (bucket instanceof Range .Bucket ) {
79+ } else if (bucket instanceof Range .Bucket rangeBucket ) {
80+ Map <String , Object > common = extract (rangeBucket , name );
5981 for (Map <String , Object > r : results ) {
60- r .put ( name , bucket . getKey () );
82+ r .putAll ( common );
6183 }
6284 }
6385 return results ;
6486 }
65-
66- @ Override
67- public List <Map <String , Object >> parse (SearchHits hits ) {
68- throw new UnsupportedOperationException (
69- "BucketAggregationParser doesn't support parse(SearchHits)" );
70- }
7187}
0 commit comments