|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.aggregations.pipeline; |
| 10 | + |
| 11 | +import org.elasticsearch.TransportVersion; |
| 12 | +import org.elasticsearch.common.ParsingException; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.script.Script; |
| 16 | +import org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder; |
| 17 | +import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; |
| 18 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 19 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 20 | +import org.elasticsearch.xcontent.XContentParser; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.TreeMap; |
| 29 | + |
| 30 | +import static org.elasticsearch.search.aggregations.pipeline.PipelineAggregator.Parser.BUCKETS_PATH; |
| 31 | +import static org.elasticsearch.search.aggregations.pipeline.PipelineAggregator.Parser.GAP_POLICY; |
| 32 | + |
| 33 | +public class BucketSelectorPipelineAggregationBuilder extends AbstractPipelineAggregationBuilder<BucketSelectorPipelineAggregationBuilder> { |
| 34 | + public static final String NAME = "bucket_selector"; |
| 35 | + |
| 36 | + private final Map<String, String> bucketsPathsMap; |
| 37 | + private final Script script; |
| 38 | + private GapPolicy gapPolicy = GapPolicy.SKIP; |
| 39 | + |
| 40 | + public BucketSelectorPipelineAggregationBuilder(String name, Map<String, String> bucketsPathsMap, Script script) { |
| 41 | + super(name, NAME, new TreeMap<>(bucketsPathsMap).values().toArray(new String[bucketsPathsMap.size()])); |
| 42 | + this.bucketsPathsMap = bucketsPathsMap; |
| 43 | + this.script = script; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Read from a stream. |
| 48 | + */ |
| 49 | + public BucketSelectorPipelineAggregationBuilder(StreamInput in) throws IOException { |
| 50 | + super(in, NAME); |
| 51 | + bucketsPathsMap = in.readMap(StreamInput::readString, StreamInput::readString); |
| 52 | + script = new Script(in); |
| 53 | + gapPolicy = GapPolicy.readFrom(in); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void doWriteTo(StreamOutput out) throws IOException { |
| 58 | + out.writeMap(bucketsPathsMap, StreamOutput::writeString, StreamOutput::writeString); |
| 59 | + script.writeTo(out); |
| 60 | + gapPolicy.writeTo(out); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Sets the gap policy to use for this aggregation. |
| 65 | + */ |
| 66 | + public BucketSelectorPipelineAggregationBuilder gapPolicy(GapPolicy gapPolicy) { |
| 67 | + if (gapPolicy == null) { |
| 68 | + throw new IllegalArgumentException("[gapPolicy] must not be null: [" + name + "]"); |
| 69 | + } |
| 70 | + this.gapPolicy = gapPolicy; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets the gap policy to use for this aggregation. |
| 76 | + */ |
| 77 | + public GapPolicy gapPolicy() { |
| 78 | + return gapPolicy; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected PipelineAggregator createInternal(Map<String, Object> metadata) { |
| 83 | + throw new UnsupportedOperationException(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException { |
| 88 | + builder.field(BUCKETS_PATH.getPreferredName(), bucketsPathsMap); |
| 89 | + builder.field(Script.SCRIPT_PARSE_FIELD.getPreferredName(), script); |
| 90 | + builder.field(GAP_POLICY.getPreferredName(), gapPolicy.getName()); |
| 91 | + return builder; |
| 92 | + } |
| 93 | + |
| 94 | + public static BucketSelectorPipelineAggregationBuilder parse(String reducerName, XContentParser parser) throws IOException { |
| 95 | + XContentParser.Token token; |
| 96 | + Script script = null; |
| 97 | + String currentFieldName = null; |
| 98 | + Map<String, String> bucketsPathsMap = null; |
| 99 | + GapPolicy gapPolicy = null; |
| 100 | + |
| 101 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 102 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 103 | + currentFieldName = parser.currentName(); |
| 104 | + } else if (token == XContentParser.Token.VALUE_STRING) { |
| 105 | + if (BUCKETS_PATH.match(currentFieldName, parser.getDeprecationHandler())) { |
| 106 | + bucketsPathsMap = new HashMap<>(); |
| 107 | + bucketsPathsMap.put("_value", parser.text()); |
| 108 | + } else if (GAP_POLICY.match(currentFieldName, parser.getDeprecationHandler())) { |
| 109 | + gapPolicy = GapPolicy.parse(parser.text(), parser.getTokenLocation()); |
| 110 | + } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { |
| 111 | + script = Script.parse(parser); |
| 112 | + } else { |
| 113 | + throw new ParsingException( |
| 114 | + parser.getTokenLocation(), |
| 115 | + "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "]." |
| 116 | + ); |
| 117 | + } |
| 118 | + } else if (token == XContentParser.Token.START_ARRAY) { |
| 119 | + if (BUCKETS_PATH.match(currentFieldName, parser.getDeprecationHandler())) { |
| 120 | + List<String> paths = new ArrayList<>(); |
| 121 | + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { |
| 122 | + String path = parser.text(); |
| 123 | + paths.add(path); |
| 124 | + } |
| 125 | + bucketsPathsMap = new HashMap<>(); |
| 126 | + for (int i = 0; i < paths.size(); i++) { |
| 127 | + bucketsPathsMap.put("_value" + i, paths.get(i)); |
| 128 | + } |
| 129 | + } else { |
| 130 | + throw new ParsingException( |
| 131 | + parser.getTokenLocation(), |
| 132 | + "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "]." |
| 133 | + ); |
| 134 | + } |
| 135 | + } else if (token == XContentParser.Token.START_OBJECT) { |
| 136 | + if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { |
| 137 | + script = Script.parse(parser); |
| 138 | + } else if (BUCKETS_PATH.match(currentFieldName, parser.getDeprecationHandler())) { |
| 139 | + Map<String, Object> map = parser.map(); |
| 140 | + bucketsPathsMap = new HashMap<>(); |
| 141 | + for (Map.Entry<String, Object> entry : map.entrySet()) { |
| 142 | + bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue())); |
| 143 | + } |
| 144 | + } else { |
| 145 | + throw new ParsingException( |
| 146 | + parser.getTokenLocation(), |
| 147 | + "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "]." |
| 148 | + ); |
| 149 | + } |
| 150 | + } else { |
| 151 | + throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "]."); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (bucketsPathsMap == null) { |
| 156 | + throw new ParsingException( |
| 157 | + parser.getTokenLocation(), |
| 158 | + "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]" |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + if (script == null) { |
| 163 | + throw new ParsingException( |
| 164 | + parser.getTokenLocation(), |
| 165 | + "Missing required field [" |
| 166 | + + Script.SCRIPT_PARSE_FIELD.getPreferredName() |
| 167 | + + "] for bucket_selector aggregation [" |
| 168 | + + reducerName |
| 169 | + + "]" |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + BucketSelectorPipelineAggregationBuilder factory = new BucketSelectorPipelineAggregationBuilder( |
| 174 | + reducerName, |
| 175 | + bucketsPathsMap, |
| 176 | + script |
| 177 | + ); |
| 178 | + if (gapPolicy != null) { |
| 179 | + factory.gapPolicy(gapPolicy); |
| 180 | + } |
| 181 | + return factory; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + protected void validate(ValidationContext context) { |
| 186 | + context.validateHasParent(NAME, name); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + protected boolean overrideBucketsPath() { |
| 191 | + return true; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public int hashCode() { |
| 196 | + return Objects.hash(super.hashCode(), bucketsPathsMap, script, gapPolicy); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public boolean equals(Object obj) { |
| 201 | + if (this == obj) return true; |
| 202 | + if (obj == null || getClass() != obj.getClass()) return false; |
| 203 | + if (super.equals(obj) == false) return false; |
| 204 | + |
| 205 | + BucketSelectorPipelineAggregationBuilder other = (BucketSelectorPipelineAggregationBuilder) obj; |
| 206 | + return Objects.equals(bucketsPathsMap, other.bucketsPathsMap) |
| 207 | + && Objects.equals(script, other.script) |
| 208 | + && Objects.equals(gapPolicy, other.gapPolicy); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public String getWriteableName() { |
| 213 | + return NAME; |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public TransportVersion getMinimalSupportedVersion() { |
| 218 | + return TransportVersion.ZERO; |
| 219 | + } |
| 220 | +} |
0 commit comments