|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.inference.action; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionRequest; |
| 11 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 12 | +import org.elasticsearch.action.ActionResponse; |
| 13 | +import org.elasticsearch.action.ActionType; |
| 14 | +import org.elasticsearch.action.RemoteClusterActionType; |
| 15 | +import org.elasticsearch.cluster.metadata.InferenceFieldMetadata; |
| 16 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 17 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 18 | +import org.elasticsearch.core.Nullable; |
| 19 | +import org.elasticsearch.inference.InferenceResults; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +public class GetInferenceFieldsAction extends ActionType<GetInferenceFieldsAction.Response> { |
| 28 | + public static final GetInferenceFieldsAction INSTANCE = new GetInferenceFieldsAction(); |
| 29 | + public static final RemoteClusterActionType<Response> REMOTE_TYPE = new RemoteClusterActionType<>(INSTANCE.name(), Response::new); |
| 30 | + |
| 31 | + public static final String NAME = "cluster:monitor/xpack/inference_fields/get"; |
| 32 | + |
| 33 | + public GetInferenceFieldsAction() { |
| 34 | + super(NAME); |
| 35 | + } |
| 36 | + |
| 37 | + public static class Request extends ActionRequest { |
| 38 | + private final List<String> indices; |
| 39 | + private final List<String> fields; |
| 40 | + private final boolean resolveWildcards; |
| 41 | + private final boolean useDefaultFields; |
| 42 | + private final String query; |
| 43 | + |
| 44 | + public Request( |
| 45 | + List<String> indices, |
| 46 | + List<String> fields, |
| 47 | + boolean resolveWildcards, |
| 48 | + boolean useDefaultFields, |
| 49 | + @Nullable String query |
| 50 | + ) { |
| 51 | + this.indices = indices; |
| 52 | + this.fields = fields; |
| 53 | + this.resolveWildcards = resolveWildcards; |
| 54 | + this.useDefaultFields = useDefaultFields; |
| 55 | + this.query = query; |
| 56 | + } |
| 57 | + |
| 58 | + public Request(StreamInput in) throws IOException { |
| 59 | + super(in); |
| 60 | + this.indices = in.readCollectionAsList(StreamInput::readString); |
| 61 | + this.fields = in.readCollectionAsList(StreamInput::readString); |
| 62 | + this.resolveWildcards = in.readBoolean(); |
| 63 | + this.useDefaultFields = in.readBoolean(); |
| 64 | + this.query = in.readOptionalString(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void writeTo(StreamOutput out) throws IOException { |
| 69 | + super.writeTo(out); |
| 70 | + out.writeStringCollection(indices); |
| 71 | + out.writeStringCollection(fields); |
| 72 | + out.writeBoolean(resolveWildcards); |
| 73 | + out.writeBoolean(useDefaultFields); |
| 74 | + out.writeOptionalString(query); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ActionRequestValidationException validate() { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + public List<String> getIndices() { |
| 83 | + return Collections.unmodifiableList(indices); |
| 84 | + } |
| 85 | + |
| 86 | + public List<String> getFields() { |
| 87 | + return Collections.unmodifiableList(fields); |
| 88 | + } |
| 89 | + |
| 90 | + public boolean resolveWildcards() { |
| 91 | + return resolveWildcards; |
| 92 | + } |
| 93 | + |
| 94 | + public boolean useDefaultFields() { |
| 95 | + return useDefaultFields; |
| 96 | + } |
| 97 | + |
| 98 | + public String getQuery() { |
| 99 | + return query; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object o) { |
| 104 | + if (this == o) return true; |
| 105 | + if (o == null || getClass() != o.getClass()) return false; |
| 106 | + Request request = (Request) o; |
| 107 | + return Objects.equals(indices, request.indices) |
| 108 | + && Objects.equals(fields, request.fields) |
| 109 | + && resolveWildcards == request.resolveWildcards |
| 110 | + && useDefaultFields == request.useDefaultFields |
| 111 | + && Objects.equals(query, request.query); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int hashCode() { |
| 116 | + return Objects.hash(indices, fields, resolveWildcards, useDefaultFields, query); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static class Response extends ActionResponse { |
| 121 | + private final Map<String, List<InferenceFieldMetadata>> inferenceFieldsMap; |
| 122 | + private final Map<String, InferenceResults> inferenceResultsMap; |
| 123 | + |
| 124 | + public Response(Map<String, List<InferenceFieldMetadata>> inferenceFieldsMap, Map<String, InferenceResults> inferenceResultsMap) { |
| 125 | + this.inferenceFieldsMap = inferenceFieldsMap; |
| 126 | + this.inferenceResultsMap = inferenceResultsMap; |
| 127 | + } |
| 128 | + |
| 129 | + public Response(StreamInput in) throws IOException { |
| 130 | + this.inferenceFieldsMap = in.readImmutableMap(i -> i.readCollectionAsImmutableList(InferenceFieldMetadata::new)); |
| 131 | + this.inferenceResultsMap = in.readImmutableMap(i -> i.readNamedWriteable(InferenceResults.class)); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void writeTo(StreamOutput out) throws IOException { |
| 136 | + out.writeMap(inferenceFieldsMap, StreamOutput::writeCollection); |
| 137 | + out.writeMap(inferenceResultsMap, StreamOutput::writeNamedWriteable); |
| 138 | + } |
| 139 | + |
| 140 | + public Map<String, List<InferenceFieldMetadata>> getInferenceFieldsMap() { |
| 141 | + return Collections.unmodifiableMap(this.inferenceFieldsMap); |
| 142 | + } |
| 143 | + |
| 144 | + public Map<String, InferenceResults> getInferenceResultsMap() { |
| 145 | + return Collections.unmodifiableMap(this.inferenceResultsMap); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean equals(Object o) { |
| 150 | + if (this == o) return true; |
| 151 | + if (o == null || getClass() != o.getClass()) return false; |
| 152 | + Response response = (Response) o; |
| 153 | + return Objects.equals(inferenceFieldsMap, response.inferenceFieldsMap) |
| 154 | + && Objects.equals(inferenceResultsMap, response.inferenceResultsMap); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public int hashCode() { |
| 159 | + return Objects.hash(inferenceFieldsMap, inferenceResultsMap); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments