|
| 1 | +package org.opensearch.transport.grpc.proto.request.search.aggregation.bucket.terms; |
| 2 | + |
| 3 | +import org.opensearch.core.xcontent.XContentParser; |
| 4 | +import org.opensearch.protobufs.AggregationContainer; |
| 5 | +import org.opensearch.protobufs.SortOrder; |
| 6 | +import org.opensearch.protobufs.SortOrderSingleMap; |
| 7 | +import org.opensearch.protobufs.TermsAggregation; |
| 8 | +import org.opensearch.protobufs.TermsAggregationCollectMode; |
| 9 | +import org.opensearch.protobufs.TermsAggregationExecutionHint; |
| 10 | +import org.opensearch.protobufs.TermsAggregationFields; |
| 11 | +import org.opensearch.protobufs.TermsInclude; |
| 12 | +import org.opensearch.search.aggregations.AggregationBuilder; |
| 13 | +import org.opensearch.search.aggregations.Aggregator.SubAggCollectionMode; |
| 14 | +import org.opensearch.search.aggregations.BucketOrder; |
| 15 | +import org.opensearch.search.aggregations.InternalOrder; |
| 16 | +import org.opensearch.search.aggregations.bucket.terms.IncludeExclude; |
| 17 | +import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder; |
| 18 | +import org.opensearch.transport.grpc.proto.request.common.ScriptProtoUtils; |
| 19 | +import org.opensearch.transport.grpc.proto.request.search.aggregation.support.ValuesSourceAggregationProtoUtils; |
| 20 | +import org.opensearch.transport.grpc.proto.request.search.aggregation.support.ValuesSourceProtoFields; |
| 21 | +import org.opensearch.transport.grpc.proto.response.common.FieldValueProtoUtils; |
| 22 | +import org.opensearch.transport.grpc.spi.AggregationBuilderProtoConverter; |
| 23 | +import org.opensearch.transport.grpc.spi.AggregationBuilderProtoConverterRegistry; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Converter from proto {@link TermsAggregation} to {@link TermsAggregationBuilder} |
| 31 | + */ |
| 32 | +public class TermsAggregationBuilderConverter implements AggregationBuilderProtoConverter { |
| 33 | + |
| 34 | + private AggregationBuilderProtoConverterRegistry registry; |
| 35 | + |
| 36 | + @Override |
| 37 | + public AggregationContainer.AggregationContainerCase getHandledAggregationCase() { |
| 38 | + return AggregationContainer.AggregationContainerCase.TERMS_AGGREGATION; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * This method parallels the REST parsing logic in {@link TermsAggregationBuilder#PARSER} |
| 43 | + * |
| 44 | + * {@inheritDoc} |
| 45 | + */ |
| 46 | + @Override |
| 47 | + public AggregationBuilder fromProto(String name, AggregationContainer container) { |
| 48 | + if (!container.hasTermsAggregation()) { |
| 49 | + throw new IllegalStateException("AggregationContainer doesn't have terms aggregation"); |
| 50 | + } |
| 51 | + TermsAggregation proto = container.getTermsAggregation(); |
| 52 | + TermsAggregationBuilder aggBuilder = new TermsAggregationBuilder(name); |
| 53 | + |
| 54 | + if (!proto.hasTerms()) { |
| 55 | + throw new IllegalArgumentException("Terms fields are not provided, cannot parse"); |
| 56 | + } |
| 57 | + |
| 58 | + TermsAggregationFields terms = proto.getTerms(); |
| 59 | + |
| 60 | + // common fields for ValuesSourceAggregation |
| 61 | + ValuesSourceProtoFields valuesSourceFields = ValuesSourceProtoFields.builder() |
| 62 | + .field(terms.hasField() ? terms.getField() : null) |
| 63 | + .missing(terms.hasMissing() ? FieldValueProtoUtils.fromProto(terms.getMissing()) : null) |
| 64 | + .valueType(terms.hasValueType() ? terms.getValueType() : null) |
| 65 | + .format(terms.hasFormat() ? terms.getFormat() : null) |
| 66 | + .script(terms.hasScript() ? ScriptProtoUtils.parseFromProtoRequest(terms.getScript()) : null) |
| 67 | + .build(); |
| 68 | + ValuesSourceAggregationProtoUtils.declareFields(aggBuilder, valuesSourceFields, true, true, false); |
| 69 | + |
| 70 | + if (terms.hasShowTermDocCountError()) { |
| 71 | + aggBuilder.showTermDocCountError(terms.getShowTermDocCountError()); |
| 72 | + } |
| 73 | + |
| 74 | + if (terms.hasShardSize()) { |
| 75 | + aggBuilder.shardSize(terms.getShardSize()); |
| 76 | + } |
| 77 | + |
| 78 | + if (terms.hasMinDocCount()) { |
| 79 | + aggBuilder.minDocCount(terms.getMinDocCount()); |
| 80 | + } |
| 81 | + |
| 82 | + if (terms.hasShardMinDocCount()) { |
| 83 | + aggBuilder.shardMinDocCount(terms.getShardMinDocCount()); |
| 84 | + } |
| 85 | + |
| 86 | + if (terms.hasSize()) { |
| 87 | + aggBuilder.size(terms.getSize()); |
| 88 | + } |
| 89 | + |
| 90 | + if (terms.hasExecutionHint()) { |
| 91 | + aggBuilder.executionHint(convertExecutionHint(terms.getExecutionHint())); |
| 92 | + } |
| 93 | + |
| 94 | + if (terms.hasCollectMode()) { |
| 95 | + aggBuilder.collectMode(convertCollectMode(terms.getCollectMode())); |
| 96 | + } |
| 97 | + |
| 98 | + if (terms.getOrderCount() > 0) { |
| 99 | + aggBuilder.order(convertOrder(terms.getOrderList())); |
| 100 | + } |
| 101 | + |
| 102 | + // include/exclude handling mirrors IncludeExclude.merge logic in the REST parser |
| 103 | + IncludeExclude includeExclude = null; |
| 104 | + if (terms.hasInclude()) { |
| 105 | + includeExclude = convertInclude(terms.getInclude()); |
| 106 | + } |
| 107 | + if (terms.getExcludeCount() > 0) { |
| 108 | + // See IncludeExclude::parseExclude |
| 109 | + String[] excludeValues = terms.getExcludeList().toArray(new String[0]); |
| 110 | + IncludeExclude excludeIE = new IncludeExclude(null, excludeValues); |
| 111 | + includeExclude = IncludeExclude.merge(includeExclude, excludeIE); |
| 112 | + } |
| 113 | + if (includeExclude != null) { |
| 114 | + aggBuilder.includeExclude(includeExclude); |
| 115 | + } |
| 116 | + |
| 117 | + // sub-aggregations |
| 118 | + if (proto.getAggregationsCount() > 0) { |
| 119 | + if (registry == null) { |
| 120 | + throw new IllegalStateException("Registry not set properly, cannot parse sub aggregations"); |
| 121 | + } |
| 122 | + for (Map.Entry<String, AggregationContainer> entry : proto.getAggregationsMap().entrySet()) { |
| 123 | + aggBuilder.subAggregation(registry.fromProto(entry.getKey(), entry.getValue())); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return aggBuilder; |
| 128 | + } |
| 129 | + |
| 130 | + private static String convertExecutionHint(TermsAggregationExecutionHint hint) { |
| 131 | + // TODO: Seems the schema is a bit off sync'd between proto and java impl? Enum in proto but string in java |
| 132 | + return switch (hint) { |
| 133 | + case TERMS_AGGREGATION_EXECUTION_HINT_MAP -> "map"; |
| 134 | + case TERMS_AGGREGATION_EXECUTION_HINT_GLOBAL_ORDINALS -> "global_ordinals"; |
| 135 | + case TERMS_AGGREGATION_EXECUTION_HINT_GLOBAL_ORDINALS_HASH -> "global_ordinals_hash"; |
| 136 | + case TERMS_AGGREGATION_EXECUTION_HINT_GLOBAL_ORDINALS_LOW_CARDINALITY -> "global_ordinals_low_cardinality"; |
| 137 | + default -> throw new IllegalArgumentException("Unsupported execution hint: " + hint); |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + private static SubAggCollectionMode convertCollectMode(TermsAggregationCollectMode mode) { |
| 142 | + return switch (mode) { |
| 143 | + case TERMS_AGGREGATION_COLLECT_MODE_BREADTH_FIRST -> SubAggCollectionMode.BREADTH_FIRST; |
| 144 | + case TERMS_AGGREGATION_COLLECT_MODE_DEPTH_FIRST -> SubAggCollectionMode.DEPTH_FIRST; |
| 145 | + default -> throw new IllegalArgumentException("Unsupported collect mode: " + mode); |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + private static List<BucketOrder> convertOrder(List<SortOrderSingleMap> orderList) { |
| 150 | + List<BucketOrder> orders = new ArrayList<>(orderList.size()); |
| 151 | + for (SortOrderSingleMap entry : orderList) { |
| 152 | + boolean asc = entry.getSortOrder() == SortOrder.SORT_ORDER_ASC; |
| 153 | + String key = entry.getField(); |
| 154 | + orders.add(InternalOrder.Parser.resolveOrderParam(key, asc)); |
| 155 | + } |
| 156 | + return orders; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * See {@link IncludeExclude#parseInclude(XContentParser)} |
| 161 | + */ |
| 162 | + private static IncludeExclude convertInclude(TermsInclude include) { |
| 163 | + return switch (include.getTermsIncludeCase()) { |
| 164 | + case TERMS -> { |
| 165 | + String[] includeTerms = include.getTerms().getStringArrayList().toArray(new String[0]); |
| 166 | + yield new IncludeExclude(includeTerms, null); |
| 167 | + } |
| 168 | + case PARTITION -> |
| 169 | + new IncludeExclude(include.getPartition().getPartition(), include.getPartition().getNumPartitions()); |
| 170 | + default -> throw new IllegalArgumentException("Unsupported include type: " + include.getTermsIncludeCase()); |
| 171 | + }; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void setRegistry(AggregationBuilderProtoConverterRegistry registry) { |
| 176 | + this.registry = registry; |
| 177 | + } |
| 178 | +} |
0 commit comments