|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.analytics; |
| 21 | + |
| 22 | +import org.elasticsearch.client.ml.inference.trainedmodel.InferenceConfig; |
| 23 | +import org.elasticsearch.common.ParseField; |
| 24 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 25 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 28 | +import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; |
| 29 | +import org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder; |
| 30 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 31 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Objects; |
| 36 | +import java.util.TreeMap; |
| 37 | + |
| 38 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 39 | + |
| 40 | +/** |
| 41 | + * For building inference pipeline aggregations |
| 42 | + * |
| 43 | + * NOTE: This extends {@linkplain AbstractPipelineAggregationBuilder} for compatibility |
| 44 | + * with {@link SearchSourceBuilder#aggregation(PipelineAggregationBuilder)} but it |
| 45 | + * doesn't support any "server" side things like {@linkplain #doWriteTo(StreamOutput)} |
| 46 | + * or {@linkplain #createInternal(Map)} |
| 47 | + */ |
| 48 | +public class InferencePipelineAggregationBuilder extends AbstractPipelineAggregationBuilder<InferencePipelineAggregationBuilder> { |
| 49 | + |
| 50 | + public static String NAME = "inference"; |
| 51 | + |
| 52 | + public static final ParseField MODEL_ID = new ParseField("model_id"); |
| 53 | + private static final ParseField INFERENCE_CONFIG = new ParseField("inference_config"); |
| 54 | + |
| 55 | + |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + private static final ConstructingObjectParser<InferencePipelineAggregationBuilder, String> PARSER = new ConstructingObjectParser<>( |
| 58 | + NAME, false, |
| 59 | + (args, name) -> new InferencePipelineAggregationBuilder(name, (String)args[0], (Map<String, String>) args[1]) |
| 60 | + ); |
| 61 | + |
| 62 | + static { |
| 63 | + PARSER.declareString(constructorArg(), MODEL_ID); |
| 64 | + PARSER.declareObject(constructorArg(), (p, c) -> p.mapStrings(), BUCKETS_PATH_FIELD); |
| 65 | + PARSER.declareNamedObject(InferencePipelineAggregationBuilder::setInferenceConfig, |
| 66 | + (p, c, n) -> p.namedObject(InferenceConfig.class, n, c), INFERENCE_CONFIG); |
| 67 | + } |
| 68 | + |
| 69 | + private final Map<String, String> bucketPathMap; |
| 70 | + private final String modelId; |
| 71 | + private InferenceConfig inferenceConfig; |
| 72 | + |
| 73 | + public static InferencePipelineAggregationBuilder parse(String pipelineAggregatorName, |
| 74 | + XContentParser parser) { |
| 75 | + return PARSER.apply(parser, pipelineAggregatorName); |
| 76 | + } |
| 77 | + |
| 78 | + public InferencePipelineAggregationBuilder(String name, String modelId, Map<String, String> bucketsPath) { |
| 79 | + super(name, NAME, new TreeMap<>(bucketsPath).values().toArray(new String[] {})); |
| 80 | + this.modelId = modelId; |
| 81 | + this.bucketPathMap = bucketsPath; |
| 82 | + } |
| 83 | + |
| 84 | + public void setInferenceConfig(InferenceConfig inferenceConfig) { |
| 85 | + this.inferenceConfig = inferenceConfig; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected void validate(ValidationContext context) { |
| 90 | + // validation occurs on the server |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void doWriteTo(StreamOutput out) { |
| 95 | + throw new UnsupportedOperationException(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected PipelineAggregator createInternal(Map<String, Object> metaData) { |
| 100 | + throw new UnsupportedOperationException(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected boolean overrideBucketsPath() { |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException { |
| 110 | + builder.field(MODEL_ID.getPreferredName(), modelId); |
| 111 | + builder.field(BUCKETS_PATH_FIELD.getPreferredName(), bucketPathMap); |
| 112 | + if (inferenceConfig != null) { |
| 113 | + builder.startObject(INFERENCE_CONFIG.getPreferredName()); |
| 114 | + builder.field(inferenceConfig.getName(), inferenceConfig); |
| 115 | + builder.endObject(); |
| 116 | + } |
| 117 | + return builder; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String getWriteableName() { |
| 122 | + return NAME; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int hashCode() { |
| 127 | + return Objects.hash(super.hashCode(), bucketPathMap, modelId, inferenceConfig); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean equals(Object obj) { |
| 132 | + if (this == obj) return true; |
| 133 | + if (obj == null || getClass() != obj.getClass()) return false; |
| 134 | + if (super.equals(obj) == false) return false; |
| 135 | + |
| 136 | + InferencePipelineAggregationBuilder other = (InferencePipelineAggregationBuilder) obj; |
| 137 | + return Objects.equals(bucketPathMap, other.bucketPathMap) |
| 138 | + && Objects.equals(modelId, other.modelId) |
| 139 | + && Objects.equals(inferenceConfig, other.inferenceConfig); |
| 140 | + } |
| 141 | +} |
0 commit comments