diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java index fe2fedf62b5..884e26e7df8 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java @@ -198,6 +198,7 @@ public class SearchTemplateIT extends ESSingleNodeTestCase { getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get(); assertNull(getResponse.getSource()); + assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } public void testIndexedTemplate() throws Exception { @@ -267,6 +268,7 @@ public class SearchTemplateIT extends ESSingleNodeTestCase { .setScript("2").setScriptType(ScriptType.STORED).setScriptParams(templateParams) .get(); assertHitCount(searchResponse.getResponse(), 1); + assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } // Relates to #10397 @@ -311,6 +313,7 @@ public class SearchTemplateIT extends ESSingleNodeTestCase { .get(); assertHitCount(searchResponse.getResponse(), 1); } + assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } public void testIndexedTemplateWithArray() throws Exception { @@ -339,6 +342,7 @@ public class SearchTemplateIT extends ESSingleNodeTestCase { .setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams) .get(); assertHitCount(searchResponse.getResponse(), 5); + assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } } diff --git a/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java b/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java index da6dad1dff3..11f8769c86b 100644 --- a/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java +++ b/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java @@ -74,6 +74,11 @@ public class StoredScriptSource extends AbstractDiffable imp */ public static final ParseField TEMPLATE_PARSE_FIELD = new ParseField("template"); + /** + * Standard {@link ParseField} for query on the inner field. + */ + public static final ParseField TEMPLATE_NO_WRAPPER_PARSE_FIELD = new ParseField("query"); + /** * Standard {@link ParseField} for lang on the inner level. */ @@ -189,6 +194,26 @@ public class StoredScriptSource extends AbstractDiffable imp PARSER.declareField(Builder::setOptions, XContentParser::mapStrings, OPTIONS_PARSE_FIELD, ValueType.OBJECT); } + private static StoredScriptSource parseRemaining(Token token, XContentParser parser) throws IOException { + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + if (token != Token.START_OBJECT) { + builder.startObject(); + builder.copyCurrentStructure(parser); + builder.endObject(); + } else { + builder.copyCurrentStructure(parser); + } + + String source = Strings.toString(builder); + + if (source == null || source.isEmpty()) { + DEPRECATION_LOGGER.deprecated("empty templates should no longer be used"); + } + + return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, source, Collections.emptyMap()); + } + } + /** * This will parse XContent into a {@link StoredScriptSource}. The following formats can be parsed: * @@ -304,38 +329,28 @@ public class StoredScriptSource extends AbstractDiffable imp } else { throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, ]"); } - } else { - if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) { - token = parser.nextToken(); + } else if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) { - if (token == Token.VALUE_STRING) { - String source = parser.text(); + DEPRECATION_LOGGER.deprecated("the template context is now deprecated. Specify templates in a \"script\" element."); - if (source == null || source.isEmpty()) { - DEPRECATION_LOGGER.deprecated("empty templates should no longer be used"); - } - - return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, source, Collections.emptyMap()); - } - } - - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - if (token != Token.START_OBJECT) { - builder.startObject(); - builder.copyCurrentStructure(parser); - builder.endObject(); - } else { - builder.copyCurrentStructure(parser); - } - - String source = Strings.toString(builder); + token = parser.nextToken(); + if (token == Token.VALUE_STRING) { + String source = parser.text(); if (source == null || source.isEmpty()) { DEPRECATION_LOGGER.deprecated("empty templates should no longer be used"); } return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, source, Collections.emptyMap()); + } else { + return parseRemaining(token, parser); } + } else if (TEMPLATE_NO_WRAPPER_PARSE_FIELD.getPreferredName().equals(name)) { + DEPRECATION_LOGGER.deprecated("the template context is now deprecated. Specify templates in a \"script\" element."); + return parseRemaining(token, parser); + } else { + DEPRECATION_LOGGER.deprecated("scripts should not be stored without a context. Specify them in a \"script\" element."); + return parseRemaining(token, parser); } } catch (IOException ioe) { throw new UncheckedIOException(ioe); diff --git a/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java b/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java index 32d4d48a448..bef20190acf 100644 --- a/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java +++ b/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java @@ -81,10 +81,12 @@ public class ScriptMetaDataTests extends AbstractSerializingTestCase