Skip to content

Commit

Permalink
Scripting: fill in get contexts REST API (#48319) (#48602)
Browse files Browse the repository at this point in the history
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: #47411
  • Loading branch information
stu-elastic authored Oct 29, 2019
1 parent 50d7424 commit 55d00cf
Show file tree
Hide file tree
Showing 10 changed files with 1,179 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,7 @@
reason: "get_all_contexts introduced in 7.6.0"
- do:
get_script_context: {}
- match: { contexts.aggregation_selector: {} }
- match: { contexts.aggs: {} }
- match: { contexts.aggs_combine: {} }
- match: { contexts.aggs_init: {} }
- match: { contexts.aggs_map: {} }
- match: { contexts.aggs_reduce: {} }
- match: { contexts.bucket_aggregation: {} }
- match: { contexts.field: {} }
- match: { contexts.filter: {} }
- match: { contexts.ingest: {} }
- match: { contexts.interval: {} }
- match: { contexts.number_sort: {} }
- match: { contexts.processor_conditional: {} }
- match: { contexts.score: {} }
- match: { contexts.script_heuristic: {} }
- match: { contexts.similarity: {} }
- match: { contexts.similarity_weight: {} }
- match: { contexts.string_sort: {} }
- match: { contexts.template: {} }
- match: { contexts.terms_set: {} }
- match: { contexts.update: {} }

- is_true: contexts.0.name
- is_true: contexts.0.methods.0.return_type
- match: { contexts.0.methods.0.name: "execute" }
Original file line number Diff line number Diff line change
Expand Up @@ -28,69 +28,72 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.ScriptContextInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.XContentParser.Token.END_OBJECT;
import static org.elasticsearch.common.xcontent.XContentParser.Token.START_OBJECT;

public class GetScriptContextResponse extends ActionResponse implements StatusToXContentObject {

private static final ParseField CONTEXTS = new ParseField("contexts");
private final List<String> contextNames;
final Map<String,ScriptContextInfo> contexts;

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<GetScriptContextResponse,Void> PARSER =
new ConstructingObjectParser<>("get_script_context", true,
(a) -> {
Map<String, Object> contexts = ((List<String>) a[0]).stream().collect(Collectors.toMap(
name -> name, name -> new Object()
));
Map<String,ScriptContextInfo> contexts = ((List<ScriptContextInfo>)a[0]).stream().collect(
Collectors.toMap(ScriptContextInfo::getName, c -> c)
);
return new GetScriptContextResponse(contexts);
}
);

static {
PARSER.declareNamedObjects(
ConstructingObjectParser.constructorArg(),
(p, c, n) ->
{
// advance empty object
assert(p.nextToken() == START_OBJECT);
assert(p.nextToken() == END_OBJECT);
return n;
},
CONTEXTS
);
PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(),
(parser, ctx) -> ScriptContextInfo.PARSER.apply(parser, ctx), CONTEXTS);
}

GetScriptContextResponse(StreamInput in) throws IOException {
super(in);
int size = in.readInt();
ArrayList<String> contextNames = new ArrayList<>(size);
HashMap<String, ScriptContextInfo> contexts = new HashMap<>(size);
for (int i = 0; i < size; i++) {
contextNames.add(in.readString());
ScriptContextInfo info = new ScriptContextInfo(in);
contexts.put(info.name, info);
}
this.contextNames = Collections.unmodifiableList(contextNames);
this.contexts = Collections.unmodifiableMap(contexts);
}

// TransportAction constructor
GetScriptContextResponse(Set<ScriptContextInfo> contexts) {
this.contexts = Collections.unmodifiableMap(contexts.stream().collect(
Collectors.toMap(ScriptContextInfo::getName, Function.identity())
));
}

// Parser constructor
private GetScriptContextResponse(Map<String,ScriptContextInfo> contexts) {
this.contexts = Collections.unmodifiableMap(contexts);
}

GetScriptContextResponse(Map<String,Object> contexts) {
List<String> contextNames = new ArrayList<>(contexts.keySet());
contextNames.sort(String::compareTo);
this.contextNames = Collections.unmodifiableList(contextNames);
private List<ScriptContextInfo> byName() {
return contexts.values().stream().sorted(Comparator.comparing(ScriptContextInfo::getName)).collect(Collectors.toList());
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeInt(this.contextNames.size());
for (String context: this.contextNames) {
out.writeString(context);
out.writeInt(contexts.size());
for (ScriptContextInfo context: contexts.values()) {
context.writeTo(out);
}
}

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject().startObject(CONTEXTS.getPreferredName());
for (String contextName: this.contextNames) {
builder.startObject(contextName).endObject();
builder.startObject().startArray(CONTEXTS.getPreferredName());
for (ScriptContextInfo context: byName()) {
context.toXContent(builder, params);
}
builder.endObject().endObject(); // CONTEXTS
builder.endArray().endObject(); // CONTEXTS
return builder;
}

Expand All @@ -122,11 +125,11 @@ public boolean equals(Object o) {
return false;
}
GetScriptContextResponse that = (GetScriptContextResponse) o;
return contextNames.equals(that.contextNames);
return contexts.equals(that.contexts);
}

@Override
public int hashCode() {
return Objects.hash(contextNames);
return Objects.hash(contexts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.script.ScriptContextInfo;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.transport.TransportService;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.Set;

public class TransportGetScriptContextAction extends HandledTransportAction<GetScriptContextRequest, GetScriptContextResponse> {

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

@Override
protected void doExecute(Task task, GetScriptContextRequest request, ActionListener<GetScriptContextResponse> listener) {
Map<String,Object> contexts = scriptService.getContextNames().stream().collect(
Collectors.toMap(name -> name, name -> new Object())
);
Set<ScriptContextInfo> contexts = scriptService.getContextInfos();
listener.onResponse(new GetScriptContextResponse(contexts));
}
}
Loading

0 comments on commit 55d00cf

Please sign in to comment.