summaryrefslogtreecommitdiff
path: root/modules/lang-mustache/src/main
diff options
context:
space:
mode:
authorJack Conradson <osjdconrad@gmail.com>2017-01-31 13:27:02 -0800
committerGitHub <noreply@github.com>2017-01-31 13:27:02 -0800
commit3d2626c4c694db0abd1ad15badecd75a06818f40 (patch)
tree3bf18ef971cfcbf84798753e4b886b02e68ea0dd /modules/lang-mustache/src/main
parenteb36b82de48750777172ecdc3b019de32535e3f4 (diff)
Change Namespace for Stored Script to Only Use Id (#22206)
Currently, stored scripts use a namespace of (lang, id) to be put, get, deleted, and executed. This is not necessary since the lang is stored with the stored script. A user should only have to specify an id to use a stored script. This change makes that possible while keeping backwards compatibility with the previous namespace of (lang, id). Anywhere the previous namespace is used will log deprecation warnings. The new behavior is the following: When a user specifies a stored script, that script will be stored under both the new namespace and old namespace. Take for example script 'A' with lang 'L0' and data 'D0'. If we add script 'A' to the empty set, the scripts map will be ["A" -- D0, "A#L0" -- D0]. If a script 'A' with lang 'L1' and data 'D1' is then added, the scripts map will be ["A" -- D1, "A#L1" -- D1, "A#L0" -- D0]. When a user deletes a stored script, that script will be deleted from both the new namespace (if it exists) and the old namespace. Take for example a scripts map with {"A" -- D1, "A#L1" -- D1, "A#L0" -- D0}. If a script is removed specified by an id 'A' and lang null then the scripts map will be {"A#L0" -- D0}. To remove the final script, the deprecated namespace must be used, so an id 'A' and lang 'L0' would need to be specified. When a user gets/executes a stored script, if the new namespace is used then the script will be retrieved/executed using only 'id', and if the old namespace is used then the script will be retrieved/executed using 'id' and 'lang'
Diffstat (limited to 'modules/lang-mustache/src/main')
-rw-r--r--modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestDeleteSearchTemplateAction.java21
-rw-r--r--modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestGetSearchTemplateAction.java55
-rw-r--r--modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestPutSearchTemplateAction.java23
-rw-r--r--modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TemplateQueryBuilder.java12
4 files changed, 89 insertions, 22 deletions
diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestDeleteSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestDeleteSearchTemplateAction.java
index 027235496b..61a394462f 100644
--- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestDeleteSearchTemplateAction.java
+++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestDeleteSearchTemplateAction.java
@@ -18,21 +18,32 @@
*/
package org.elasticsearch.script.mustache;
+import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
+import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
-import org.elasticsearch.rest.action.admin.cluster.RestDeleteStoredScriptAction;
+import org.elasticsearch.rest.action.AcknowledgedRestListener;
+import org.elasticsearch.script.Script;
+
+import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
-public class RestDeleteSearchTemplateAction extends RestDeleteStoredScriptAction {
+public class RestDeleteSearchTemplateAction extends BaseRestHandler {
+
public RestDeleteSearchTemplateAction(Settings settings, RestController controller) {
- super(settings, controller, false);
+ super(settings);
+
controller.registerHandler(DELETE, "/_search/template/{id}", this);
}
@Override
- protected String getScriptLang(RestRequest request) {
- return "mustache";
+ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
+ String id = request.param("id");
+
+ DeleteStoredScriptRequest deleteStoredScriptRequest = new DeleteStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG);
+ return channel -> client.admin().cluster().deleteStoredScript(deleteStoredScriptRequest, new AcknowledgedRestListener<>(channel));
}
}
diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestGetSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestGetSearchTemplateAction.java
index c02c01273a..7d1ed4b57a 100644
--- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestGetSearchTemplateAction.java
+++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestGetSearchTemplateAction.java
@@ -18,29 +18,64 @@
*/
package org.elasticsearch.script.mustache;
+import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
+import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
+import org.elasticsearch.client.node.NodeClient;
+import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.rest.BaseRestHandler;
+import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
-import org.elasticsearch.rest.action.admin.cluster.RestGetStoredScriptAction;
+import org.elasticsearch.rest.RestResponse;
+import org.elasticsearch.rest.RestStatus;
+import org.elasticsearch.rest.action.RestBuilderListener;
+import org.elasticsearch.script.Script;
+import org.elasticsearch.script.StoredScriptSource;
+
+import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
-public class RestGetSearchTemplateAction extends RestGetStoredScriptAction {
+public class RestGetSearchTemplateAction extends BaseRestHandler {
+
+ public static final ParseField _ID_PARSE_FIELD = new ParseField("_id");
- private static final String TEMPLATE = "template";
+ public static final ParseField FOUND_PARSE_FIELD = new ParseField("found");
public RestGetSearchTemplateAction(Settings settings, RestController controller) {
- super(settings, controller, false);
+ super(settings);
+
controller.registerHandler(GET, "/_search/template/{id}", this);
}
@Override
- protected String getScriptLang(RestRequest request) {
- return "mustache";
- }
+ public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
+ String id = request.param("id");
- @Override
- protected String getScriptFieldName() {
- return TEMPLATE;
+ GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG);
+
+ return channel -> client.admin().cluster().getStoredScript(getRequest, new RestBuilderListener<GetStoredScriptResponse>(channel) {
+ @Override
+ public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuilder builder) throws Exception {
+ builder.startObject();
+ builder.field(_ID_PARSE_FIELD.getPreferredName(), id);
+
+ builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), Script.DEFAULT_TEMPLATE_LANG);
+
+ StoredScriptSource source = response.getSource();
+ boolean found = source != null;
+ builder.field(FOUND_PARSE_FIELD.getPreferredName(), found);
+
+ if (found) {
+ builder.field(StoredScriptSource.TEMPLATE_PARSE_FIELD.getPreferredName(), source.getCode());
+ }
+
+ builder.endObject();
+
+ return new BytesRestResponse(found ? RestStatus.OK : RestStatus.NOT_FOUND, builder);
+ }
+ });
}
}
diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestPutSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestPutSearchTemplateAction.java
index 2426a09ec8..6c22b0160b 100644
--- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestPutSearchTemplateAction.java
+++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestPutSearchTemplateAction.java
@@ -18,23 +18,36 @@
*/
package org.elasticsearch.script.mustache;
+import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
+import org.elasticsearch.client.node.NodeClient;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
-import org.elasticsearch.rest.action.admin.cluster.RestPutStoredScriptAction;
+import org.elasticsearch.rest.action.AcknowledgedRestListener;
+import org.elasticsearch.script.Script;
+
+import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
-public class RestPutSearchTemplateAction extends RestPutStoredScriptAction {
+public class RestPutSearchTemplateAction extends BaseRestHandler {
+
public RestPutSearchTemplateAction(Settings settings, RestController controller) {
- super(settings, controller, false);
+ super(settings);
+
controller.registerHandler(POST, "/_search/template/{id}", this);
controller.registerHandler(PUT, "/_search/template/{id}", this);
}
@Override
- protected String getScriptLang(RestRequest request) {
- return "mustache";
+ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
+ String id = request.param("id");
+ BytesReference content = request.content();
+
+ PutStoredScriptRequest put = new PutStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG, content);
+ return channel -> client.admin().cluster().putStoredScript(put, new AcknowledgedRestListener<>(channel));
}
}
diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TemplateQueryBuilder.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TemplateQueryBuilder.java
index 8bbcf09990..66a5c09977 100644
--- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TemplateQueryBuilder.java
+++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TemplateQueryBuilder.java
@@ -60,8 +60,9 @@ public class TemplateQueryBuilder extends AbstractQueryBuilder<TemplateQueryBuil
}
public TemplateQueryBuilder(String template, ScriptType scriptType, Map<String, Object> params, XContentType ct) {
- this(new Script(scriptType, "mustache", template,
- ct == null ? Collections.emptyMap() : Collections.singletonMap(Script.CONTENT_TYPE_OPTION, ct.mediaType()), params));
+ this(new Script(scriptType, "mustache", template, scriptType == ScriptType.INLINE ?
+ (ct == null ? Collections.emptyMap() : Collections.singletonMap(Script.CONTENT_TYPE_OPTION, ct.mediaType()))
+ : null, params));
}
TemplateQueryBuilder(Script template) {
@@ -138,6 +139,13 @@ public class TemplateQueryBuilder extends AbstractQueryBuilder<TemplateQueryBuil
public static TemplateQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
XContentParser parser = parseContext.parser();
Script template = Script.parse(parser, Script.DEFAULT_TEMPLATE_LANG);
+
+ // for deprecation of stored script namespaces the default lang is ignored,
+ // so the template lang must be set for a stored script
+ if (template.getType() == ScriptType.STORED) {
+ template = new Script(ScriptType.STORED, Script.DEFAULT_TEMPLATE_LANG, template.getIdOrCode(), template.getParams());
+ }
+
return new TemplateQueryBuilder(template);
}
}