|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | +package org.opensearch.transport.grpc.proto.request.search.aggregation; |
| 9 | + |
| 10 | +import org.opensearch.core.xcontent.XContentParser; |
| 11 | +import org.opensearch.protobufs.AggregationContainer; |
| 12 | +import org.opensearch.protobufs.ObjectMap; |
| 13 | +import org.opensearch.search.aggregations.AggregationBuilder; |
| 14 | +import org.opensearch.transport.grpc.proto.request.common.ObjectMapProtoUtils; |
| 15 | +import org.opensearch.transport.grpc.proto.request.search.query.AbstractQueryBuilderProtoUtils; |
| 16 | + |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +/** |
| 20 | + * Utility class for converting AggregationContainer Protocol Buffers to OpenSearch AggregationBuilder objects. |
| 21 | + * This class handles the transformation of Protocol Buffer aggregation containers into their corresponding |
| 22 | + * OpenSearch AggregationBuilder implementations. It serves as the main entry point for converting all |
| 23 | + * aggregation types from protobuf format to OpenSearch format. |
| 24 | + * |
| 25 | + * <p>This class is analogous to the REST-side aggregation parsing framework where {@link XContentParser} |
| 26 | + * is used to parse aggregations from JSON/REST requests. Each aggregation type's {@code PARSER} field |
| 27 | + * (which uses {@code fromXContent}) has a corresponding protobuf converter method that performs the same |
| 28 | + * logical transformation but from Protocol Buffer representation. |
| 29 | + * |
| 30 | + * <p>The dispatch logic mirrors how REST aggregations are parsed: by examining the aggregation type field |
| 31 | + * and delegating to the appropriate type-specific parser. |
| 32 | + */ |
| 33 | +public class AggregationContainerProtoUtils { |
| 34 | + |
| 35 | + private AggregationContainerProtoUtils() { |
| 36 | + // Utility class, no instances |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Converts a Protocol Buffer AggregationContainer to an OpenSearch AggregationBuilder. |
| 41 | + * This method dispatches to the appropriate aggregation-specific converter based on the |
| 42 | + * aggregation type specified in the container. |
| 43 | + * |
| 44 | + * <p>This is the gRPC equivalent of the REST-side aggregation parsing in |
| 45 | + * {@code AggregationBuilder.parseAggregators}, where each aggregation type has a registered |
| 46 | + * parser that reads from {@link XContentParser} via {@code fromXContent}. This method performs |
| 47 | + * the same role but reads from Protocol Buffer structures instead of JSON. |
| 48 | + * |
| 49 | + * @param name The name of the aggregation |
| 50 | + * @param aggContainer The Protocol Buffer AggregationContainer object |
| 51 | + * @return A configured AggregationBuilder instance |
| 52 | + * @throws IllegalArgumentException if the aggregation type is not supported or unrecognized |
| 53 | + * @see org.opensearch.search.aggregations.AggregationBuilder |
| 54 | + */ |
| 55 | + public static AggregationBuilder fromProto(String name, AggregationContainer aggContainer) { |
| 56 | + return fromProto(name, aggContainer, null); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Converts a Protocol Buffer AggregationContainer to an OpenSearch AggregationBuilder. |
| 61 | + * This method dispatches to the appropriate aggregation-specific converter based on the |
| 62 | + * aggregation type specified in the container. It also supports nested queries in aggregations |
| 63 | + * (like filter aggregations) by accepting a queryUtils parameter. |
| 64 | + * |
| 65 | + * <p>This is the gRPC equivalent of the REST-side aggregation parsing in |
| 66 | + * {@code AggregationBuilder.parseAggregators}, where each aggregation type has a registered |
| 67 | + * parser that reads from {@link XContentParser} via {@code fromXContent}. This method performs |
| 68 | + * the same role but reads from Protocol Buffer structures instead of JSON. |
| 69 | + * |
| 70 | + * @param name The name of the aggregation |
| 71 | + * @param aggContainer The Protocol Buffer AggregationContainer object |
| 72 | + * @param queryUtils The query utils instance for parsing nested queries (can be null if not needed) |
| 73 | + * @return A configured AggregationBuilder instance |
| 74 | + * @throws IllegalArgumentException if the aggregation type is not supported or unrecognized |
| 75 | + * @see org.opensearch.search.aggregations.AggregationBuilder |
| 76 | + */ |
| 77 | + public static AggregationBuilder fromProto(String name, AggregationContainer aggContainer, AbstractQueryBuilderProtoUtils queryUtils) { |
| 78 | + AggregationBuilder builder; |
| 79 | + |
| 80 | + // Dispatch based on the aggregation type |
| 81 | + switch (aggContainer.getAggregationCase()) { |
| 82 | + case CARDINALITY: |
| 83 | + builder = CardinalityAggregationProtoUtils.fromProto(name, aggContainer.getCardinality()); |
| 84 | + break; |
| 85 | + |
| 86 | + case MISSING: |
| 87 | + builder = MissingAggregationProtoUtils.fromProto(name, aggContainer.getMissing()); |
| 88 | + break; |
| 89 | + |
| 90 | + case TERMS: |
| 91 | + builder = TermsAggregationProtoUtils.fromProto(name, aggContainer.getTerms()); |
| 92 | + break; |
| 93 | + |
| 94 | + case FILTER: |
| 95 | + if (queryUtils == null) { |
| 96 | + throw new IllegalArgumentException("Filter aggregation requires queryUtils to be provided for parsing nested queries"); |
| 97 | + } |
| 98 | + builder = FilterAggregationProtoUtils.fromProto(name, aggContainer.getFilter(), queryUtils); |
| 99 | + break; |
| 100 | + |
| 101 | + case AGGREGATION_NOT_SET: |
| 102 | + throw new IllegalArgumentException("Aggregation type not set for aggregation: " + name); |
| 103 | + |
| 104 | + default: |
| 105 | + throw new IllegalArgumentException( |
| 106 | + "Unsupported aggregation type: " + aggContainer.getAggregationCase() + " for aggregation: " + name |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + // Handle metadata if present |
| 111 | + if (aggContainer.hasMeta()) { |
| 112 | + ObjectMap metaProto = aggContainer.getMeta(); |
| 113 | + Map<String, Object> metadata = ObjectMapProtoUtils.fromProto(metaProto); |
| 114 | + builder.setMetadata(metadata); |
| 115 | + } |
| 116 | + |
| 117 | + return builder; |
| 118 | + } |
| 119 | +} |
0 commit comments