Skip to content

Commit

Permalink
Scripting: fill in get contexts REST API
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: elastic#47411
  • Loading branch information
stu-elastic committed Oct 21, 2019
1 parent 704317d commit e6574dc
Show file tree
Hide file tree
Showing 10 changed files with 1,207 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,68 @@
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: {} }
- match: { contexts.0.name: "aggregation_selector" }
- match: { contexts.0.methods: [{"name": "execute", "return_type": "boolean", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.1.name: "aggs" }
- match: { contexts.1.methods: [{"name": "execute", "return_type": "java.lang.Object", "params": []}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "get_score", "return_type": "java.lang.Number", "params": []}, {"name": "get_value", "return_type": "java.lang.Object", "params": []}] }

- match: { contexts.2.name: "aggs_combine" }
- match: { contexts.2.methods: [{"name": "execute", "return_type": "java.lang.Object", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "getState", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.3.name: "aggs_init" }
- match: { contexts.3.methods: [{"name": "execute", "return_type": "void", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "getState", "return_type": "java.lang.Object", "params": []}] }

- match: { contexts.4.name: "aggs_map" }
- match: { contexts.4.methods: [{"name": "execute", "return_type": "void", "params": []}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "getState", "return_type": "java.util.Map", "params": []}, {"name": "get_score", "return_type": "double", "params": []}] }

- match: { contexts.5.name: "aggs_reduce" }
- match: { contexts.5.methods: [{"name": "execute", "return_type": "java.lang.Object", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "getStates", "return_type": "java.util.List", "params": []}] }

- match: { contexts.6.name: "bucket_aggregation" }
- match: { contexts.6.methods: [{"name": "execute", "return_type": "java.lang.Number", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.7.name: "field" }
- match: { contexts.7.methods: [{"name": "execute", "return_type": "java.lang.Object", "params": []}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.8.name: "filter" }
- match: { contexts.8.methods: [{"name": "execute", "return_type": "boolean", "params": []}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.9.name: "ingest" }
- match: { contexts.9.methods: [{"name": "execute", "return_type": "void", "params": [{"type": "java.util.Map", "name": "ctx"}]}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.10.name: "interval" }
- match: { contexts.10.methods: [{"name": "execute", "return_type": "boolean", "params": [{"type": "org.elasticsearch.index.query.IntervalFilterScript$Interval", "name": "interval"}]}] }

- match: { contexts.11.name: "moving-function" }
- match: { contexts.11.methods: [{"name": "execute", "return_type": "double", "params": [{"type": "java.util.Map", "name": "params"}, {"type": "double[]", "name": "values"}]}] }

- match: { contexts.12.name: "number_sort" }
- match: { contexts.12.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": []}] }

- match: { contexts.13.name: "processor_conditional" }
- match: { contexts.13.methods: [{"name": "execute", "return_type": "boolean", "params": [{"type": "java.util.Map", "name": "ctx"}]}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.14.name: "score" }
- match: { contexts.14.methods: [{"name": "execute", "return_type": "double", "params": [{"type": "org.elasticsearch.script.ScoreScript$ExplanationHolder", "name": "explanation"}]}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "get_score", "return_type": "double", "params": []}] }

- match: { contexts.15.name: "script_heuristic" }
- match: { contexts.15.methods: [{"name": "execute", "return_type": "double", "params": [{"type": "java.util.Map", "name": "params"}]}] }

- match: { contexts.16.name: "similarity" }
- match: { contexts.16.methods: [{"name": "execute", "return_type": "double", "params": [{"type": "double", "name": "weight"}, {"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Query", "name": "query"}, {"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Field", "name": "field"}, {"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Term", "name": "term"}, {"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Doc", "name": "doc"}]}] }

- match: { contexts.17.name: "similarity_weight" }
- match: { contexts.17.methods: [{"name": "execute", "return_type": "double", "params": [{"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Query", "name": "query"}, {"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Field", "name": "field"}, {"type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Term", "name": "term"}]}] }

- match: { contexts.18.name: "string_sort" }
- match: { contexts.18.methods: [{"name": "execute", "return_type": "java.lang.String", "params": []}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}, {"name": "get_score", "return_type": "double", "params": []}] }

- match: { contexts.19.name: "template" }
- match: { contexts.19.methods: [{"name": "execute", "return_type": "java.lang.String", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.20.name: "terms_set" }
- match: { contexts.20.methods: [{"name": "execute", "return_type": "java.lang.Number", "params": []}, {"name": "getDoc", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }

- match: { contexts.21.name: "update" }
- match: { contexts.21.methods: [{"name": "execute", "return_type": "void", "params": []}, {"name": "getCtx", "return_type": "java.util.Map", "params": []}, {"name": "getParams", "return_type": "java.util.Map", "params": []}] }
Original file line number Diff line number Diff line change
Expand Up @@ -28,69 +28,55 @@
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.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.HashSet;
import java.util.Set;
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 Set<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()
));
return new GetScriptContextResponse(contexts);
return new GetScriptContextResponse((List<ScriptContextInfo>)a[0]);
}
);

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);
HashSet<ScriptContextInfo> contexts = new HashSet<>(size);
for (int i = 0; i < size; i++) {
contextNames.add(in.readString());
contexts.add(new ScriptContextInfo(in));
}
this.contextNames = Collections.unmodifiableList(contextNames);
this.contexts = Collections.unmodifiableSet(contexts);
}

GetScriptContextResponse(Map<String,Object> contexts) {
List<String> contextNames = new ArrayList<>(contexts.keySet());
contextNames.sort(String::compareTo);
this.contextNames = Collections.unmodifiableList(contextNames);
GetScriptContextResponse(Collection<ScriptContextInfo> contexts) {
this.contexts = Set.copyOf(contexts);
}

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

Expand All @@ -101,11 +87,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: contexts.stream().sorted(Comparator.comparing(s -> s.name)).collect(Collectors.toList())) {
context.toXContent(builder, params);
}
builder.endObject().endObject(); // CONTEXTS
builder.endArray().endObject(); // CONTEXTS
return builder;
}

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

@Override
public int hashCode() {
return Objects.hash(contextNames);
return Objects.hash(new HashSet<>(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 e6574dc

Please sign in to comment.