Skip to content

Commit 0f1b076

Browse files
authored
Scripting: fill in get contexts REST API (elastic#48319)
* Scripting: fill in get contexts REST API Updates response for `GET /_script_context`, returning a `contexts` object with a list of context description objects. The description includes the context name and a list of methods available. The methods list has the signature for the `execute` mathod and any getters. eg. ``` { "contexts": [ { "name" : "moving-function", "methods" : [ { "name" : "execute", "return_type" : "double", "params" : [ { "type" : "java.util.Map", "name" : "params" }, { "type" : "double[]", "name" : "values" } ] } ] }, { "name" : "number_sort", "methods" : [ { "name" : "execute", "return_type" : "double", "params" : [ ] }, { "name" : "getDoc", "return_type" : "java.util.Map", "params" : [ ] }, { "name" : "getParams", "return_type" : "java.util.Map", "params" : [ ] }, { "name" : "get_score", "return_type" : "double", "params" : [ ] } ] }, ... ] } ``` fixes: elastic#47411
1 parent 741dfef commit 0f1b076

File tree

10 files changed

+1166
-79
lines changed

10 files changed

+1166
-79
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/scripts/20_get_script_context.yml

+4-21
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,7 @@
44
reason: "get_all_contexts introduced in 7.6.0"
55
- do:
66
get_script_context: {}
7-
- match: { contexts.aggregation_selector: {} }
8-
- match: { contexts.aggs: {} }
9-
- match: { contexts.aggs_combine: {} }
10-
- match: { contexts.aggs_init: {} }
11-
- match: { contexts.aggs_map: {} }
12-
- match: { contexts.aggs_reduce: {} }
13-
- match: { contexts.bucket_aggregation: {} }
14-
- match: { contexts.field: {} }
15-
- match: { contexts.filter: {} }
16-
- match: { contexts.ingest: {} }
17-
- match: { contexts.interval: {} }
18-
- match: { contexts.number_sort: {} }
19-
- match: { contexts.processor_conditional: {} }
20-
- match: { contexts.score: {} }
21-
- match: { contexts.script_heuristic: {} }
22-
- match: { contexts.similarity: {} }
23-
- match: { contexts.similarity_weight: {} }
24-
- match: { contexts.string_sort: {} }
25-
- match: { contexts.template: {} }
26-
- match: { contexts.terms_set: {} }
27-
- match: { contexts.update: {} }
7+
8+
- is_true: contexts.0.name
9+
- is_true: contexts.0.methods.0.return_type
10+
- match: { contexts.0.methods.0.name: "execute" }

server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptContextResponse.java

+38-35
Original file line numberDiff line numberDiff line change
@@ -28,69 +28,72 @@
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
2929
import org.elasticsearch.common.xcontent.XContentParser;
3030
import org.elasticsearch.rest.RestStatus;
31+
import org.elasticsearch.script.ScriptContextInfo;
3132

3233
import java.io.IOException;
33-
import java.util.ArrayList;
3434
import java.util.Collections;
35+
import java.util.Comparator;
36+
import java.util.HashMap;
3537
import java.util.List;
3638
import java.util.Map;
3739
import java.util.Objects;
40+
import java.util.Set;
41+
import java.util.function.Function;
3842
import java.util.stream.Collectors;
3943

40-
import static org.elasticsearch.common.xcontent.XContentParser.Token.END_OBJECT;
41-
import static org.elasticsearch.common.xcontent.XContentParser.Token.START_OBJECT;
42-
4344
public class GetScriptContextResponse extends ActionResponse implements StatusToXContentObject {
4445

4546
private static final ParseField CONTEXTS = new ParseField("contexts");
46-
private final List<String> contextNames;
47+
final Map<String,ScriptContextInfo> contexts;
4748

4849
@SuppressWarnings("unchecked")
4950
public static final ConstructingObjectParser<GetScriptContextResponse,Void> PARSER =
5051
new ConstructingObjectParser<>("get_script_context", true,
5152
(a) -> {
52-
Map<String, Object> contexts = ((List<String>) a[0]).stream().collect(Collectors.toMap(
53-
name -> name, name -> new Object()
54-
));
53+
Map<String,ScriptContextInfo> contexts = ((List<ScriptContextInfo>)a[0]).stream().collect(
54+
Collectors.toMap(ScriptContextInfo::getName, c -> c)
55+
);
5556
return new GetScriptContextResponse(contexts);
5657
}
5758
);
5859

5960
static {
60-
PARSER.declareNamedObjects(
61-
ConstructingObjectParser.constructorArg(),
62-
(p, c, n) ->
63-
{
64-
// advance empty object
65-
assert(p.nextToken() == START_OBJECT);
66-
assert(p.nextToken() == END_OBJECT);
67-
return n;
68-
},
69-
CONTEXTS
70-
);
61+
PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(),
62+
(parser, ctx) -> ScriptContextInfo.PARSER.apply(parser, ctx), CONTEXTS);
7163
}
7264

7365
GetScriptContextResponse(StreamInput in) throws IOException {
7466
super(in);
7567
int size = in.readInt();
76-
ArrayList<String> contextNames = new ArrayList<>(size);
68+
HashMap<String, ScriptContextInfo> contexts = new HashMap<>(size);
7769
for (int i = 0; i < size; i++) {
78-
contextNames.add(in.readString());
70+
ScriptContextInfo info = new ScriptContextInfo(in);
71+
contexts.put(info.name, info);
7972
}
80-
this.contextNames = Collections.unmodifiableList(contextNames);
73+
this.contexts = Collections.unmodifiableMap(contexts);
74+
}
75+
76+
// TransportAction constructor
77+
GetScriptContextResponse(Set<ScriptContextInfo> contexts) {
78+
this.contexts = Map.copyOf(contexts.stream().collect(
79+
Collectors.toMap(ScriptContextInfo::getName, Function.identity())
80+
));
81+
}
82+
83+
// Parser constructor
84+
private GetScriptContextResponse(Map<String,ScriptContextInfo> contexts) {
85+
this.contexts = Map.copyOf(contexts);
8186
}
8287

83-
GetScriptContextResponse(Map<String,Object> contexts) {
84-
List<String> contextNames = new ArrayList<>(contexts.keySet());
85-
contextNames.sort(String::compareTo);
86-
this.contextNames = Collections.unmodifiableList(contextNames);
88+
private List<ScriptContextInfo> byName() {
89+
return contexts.values().stream().sorted(Comparator.comparing(ScriptContextInfo::getName)).collect(Collectors.toList());
8790
}
8891

8992
@Override
9093
public void writeTo(StreamOutput out) throws IOException {
91-
out.writeInt(this.contextNames.size());
92-
for (String context: this.contextNames) {
93-
out.writeString(context);
94+
out.writeInt(contexts.size());
95+
for (ScriptContextInfo context: contexts.values()) {
96+
context.writeTo(out);
9497
}
9598
}
9699

@@ -101,11 +104,11 @@ public RestStatus status() {
101104

102105
@Override
103106
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
104-
builder.startObject().startObject(CONTEXTS.getPreferredName());
105-
for (String contextName: this.contextNames) {
106-
builder.startObject(contextName).endObject();
107+
builder.startObject().startArray(CONTEXTS.getPreferredName());
108+
for (ScriptContextInfo context: byName()) {
109+
context.toXContent(builder, params);
107110
}
108-
builder.endObject().endObject(); // CONTEXTS
111+
builder.endArray().endObject(); // CONTEXTS
109112
return builder;
110113
}
111114

@@ -122,11 +125,11 @@ public boolean equals(Object o) {
122125
return false;
123126
}
124127
GetScriptContextResponse that = (GetScriptContextResponse) o;
125-
return contextNames.equals(that.contextNames);
128+
return contexts.equals(that.contexts);
126129
}
127130

128131
@Override
129132
public int hashCode() {
130-
return Objects.hash(contextNames);
133+
return Objects.hash(contexts);
131134
}
132135
}

server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/TransportGetScriptContextAction.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import org.elasticsearch.action.support.ActionFilters;
2323
import org.elasticsearch.action.support.HandledTransportAction;
2424
import org.elasticsearch.common.inject.Inject;
25+
import org.elasticsearch.script.ScriptContextInfo;
2526
import org.elasticsearch.script.ScriptService;
2627
import org.elasticsearch.tasks.Task;
2728
import org.elasticsearch.transport.TransportService;
2829

29-
import java.util.Map;
30-
import java.util.stream.Collectors;
30+
import java.util.Set;
3131

3232
public class TransportGetScriptContextAction extends HandledTransportAction<GetScriptContextRequest, GetScriptContextResponse> {
3333

@@ -41,9 +41,7 @@ public TransportGetScriptContextAction(TransportService transportService, Action
4141

4242
@Override
4343
protected void doExecute(Task task, GetScriptContextRequest request, ActionListener<GetScriptContextResponse> listener) {
44-
Map<String,Object> contexts = scriptService.getContextNames().stream().collect(
45-
Collectors.toMap(name -> name, name -> new Object())
46-
);
44+
Set<ScriptContextInfo> contexts = scriptService.getContextInfos();
4745
listener.onResponse(new GetScriptContextResponse(contexts));
4846
}
4947
}

0 commit comments

Comments
 (0)