Skip to content

Commit f975177

Browse files
removed support for deprecated stored script contexts
-- related to #27612
1 parent 237650e commit f975177

File tree

6 files changed

+132
-241
lines changed

6 files changed

+132
-241
lines changed

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java

Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.action.bulk.BulkRequestBuilder;
2424
import org.elasticsearch.action.search.SearchRequest;
2525
import org.elasticsearch.common.bytes.BytesArray;
26-
import org.elasticsearch.common.bytes.BytesReference;
2726
import org.elasticsearch.common.xcontent.XContentType;
2827
import org.elasticsearch.common.xcontent.json.JsonXContent;
2928
import org.elasticsearch.plugins.Plugin;
@@ -152,25 +151,22 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws
152151
public void testIndexedTemplateClient() throws Exception {
153152
assertAcked(client().admin().cluster().preparePutStoredScript()
154153
.setId("testTemplate")
155-
.setContent(new BytesArray("{" +
156-
"\"template\":{" +
157-
" \"query\":{" +
158-
" \"match\":{" +
159-
" \"theField\" : \"{{fieldParam}}\"}" +
160-
" }" +
161-
"}" +
162-
"}"), XContentType.JSON));
163-
164-
165-
assertAcked(client().admin().cluster().preparePutStoredScript()
166-
.setId("testTemplate").setContent(new BytesArray("{" +
167-
"\"template\":{" +
168-
" \"query\":{" +
169-
" \"match\":{" +
170-
" \"theField\" : \"{{fieldParam}}\"}" +
171-
" }" +
172-
"}" +
173-
"}"), XContentType.JSON));
154+
.setContent(
155+
new BytesArray(
156+
"{" +
157+
" \"script\": {" +
158+
" \"lang\": \"mustache\"," +
159+
" \"source\": {" +
160+
" \"query\": {" +
161+
" \"match\": {" +
162+
" \"theField\": \"{{fieldParam}}\"" +
163+
" }" +
164+
" }" +
165+
" }" +
166+
" }" +
167+
"}"
168+
),
169+
XContentType.JSON));
174170

175171
GetStoredScriptResponse getResponse = client().admin().cluster()
176172
.prepareGetStoredScript("testTemplate").get();
@@ -198,41 +194,32 @@ public void testIndexedTemplateClient() throws Exception {
198194

199195
getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get();
200196
assertNull(getResponse.getSource());
201-
assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element.");
202197
}
203198

204199
public void testIndexedTemplate() throws Exception {
205-
assertAcked(client().admin().cluster().preparePutStoredScript()
206-
.setId("1a")
207-
.setContent(new BytesArray("{" +
208-
"\"template\":{" +
209-
" \"query\":{" +
210-
" \"match\":{" +
211-
" \"theField\" : \"{{fieldParam}}\"}" +
212-
" }" +
213-
"}" +
214-
"}"
215-
), XContentType.JSON)
200+
201+
String script =
202+
"{" +
203+
" \"script\": {" +
204+
" \"lang\": \"mustache\"," +
205+
" \"source\": {" +
206+
" \"query\": {" +
207+
" \"match\": {" +
208+
" \"theField\": \"{{fieldParam}}\"" +
209+
" }" +
210+
" }" +
211+
" }" +
212+
" }" +
213+
"}";
214+
215+
assertAcked(
216+
client().admin().cluster().preparePutStoredScript().setId("1a").setContent(new BytesArray(script), XContentType.JSON)
216217
);
217-
assertAcked(client().admin().cluster().preparePutStoredScript()
218-
.setId("2")
219-
.setContent(new BytesArray("{" +
220-
"\"template\":{" +
221-
" \"query\":{" +
222-
" \"match\":{" +
223-
" \"theField\" : \"{{fieldParam}}\"}" +
224-
" }" +
225-
"}" +
226-
"}"), XContentType.JSON)
218+
assertAcked(
219+
client().admin().cluster().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON)
227220
);
228-
assertAcked(client().admin().cluster().preparePutStoredScript()
229-
.setId("3")
230-
.setContent(new BytesArray("{" +
231-
"\"template\":{" +
232-
" \"match\":{" +
233-
" \"theField\" : \"{{fieldParam}}\"}" +
234-
" }" +
235-
"}"), XContentType.JSON)
221+
assertAcked(
222+
client().admin().cluster().preparePutStoredScript().setId("3").setContent(new BytesArray(script), XContentType.JSON)
236223
);
237224

238225
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
@@ -268,7 +255,6 @@ public void testIndexedTemplate() throws Exception {
268255
.setScript("2").setScriptType(ScriptType.STORED).setScriptParams(templateParams)
269256
.get();
270257
assertHitCount(searchResponse.getResponse(), 1);
271-
assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element.");
272258
}
273259

274260
// Relates to #10397
@@ -282,13 +268,27 @@ public void testIndexedTemplateOverwrite() throws Exception {
282268
client().admin().indices().prepareRefresh().get();
283269

284270
int iterations = randomIntBetween(2, 11);
271+
String query =
272+
"{" +
273+
" \"script\": {" +
274+
" \"lang\": \"mustache\"," +
275+
" \"source\": {" +
276+
" \"query\": {" +
277+
" \"match_phrase_prefix\": {" +
278+
" \"searchtext\": {" +
279+
" \"query\": \"{{P_Keyword1}}\"," +
280+
" \"slop\": {{slop}}" +
281+
" }" +
282+
" }" +
283+
" }" +
284+
" }" +
285+
" }" +
286+
"}";
285287
for (int i = 1; i < iterations; i++) {
286288
assertAcked(client().admin().cluster().preparePutStoredScript()
287289
.setId("git01")
288-
.setContent(new BytesArray(
289-
"{\"template\":{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\","
290-
+ "\"slop\": -1}}}}}"),
291-
XContentType.JSON));
290+
.setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(-1))), XContentType.JSON)
291+
);
292292

293293
GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("git01").get();
294294
assertNotNull(getResponse.getSource());
@@ -304,25 +304,39 @@ public void testIndexedTemplateOverwrite() throws Exception {
304304

305305
assertAcked(client().admin().cluster().preparePutStoredScript()
306306
.setId("git01")
307-
.setContent(new BytesArray("{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," +
308-
"\"slop\": 0}}}}"), XContentType.JSON));
307+
.setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(0))), XContentType.JSON)
308+
);
309309

310310
SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client())
311311
.setRequest(new SearchRequest("testindex").types("test"))
312312
.setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams)
313313
.get();
314314
assertHitCount(searchResponse.getResponse(), 1);
315315
}
316-
assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element.");
317316
}
318317

319318
public void testIndexedTemplateWithArray() throws Exception {
320-
String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
319+
String multiQuery =
320+
"{\n" +
321+
" \"script\": {\n" +
322+
" \"lang\": \"mustache\",\n" +
323+
" \"source\": {\n" +
324+
" \"query\": {\n" +
325+
" \"terms\": {\n" +
326+
" \"theField\": [\n" +
327+
" \"{{#fieldParam}}\",\n" +
328+
" \"{{.}}\",\n" +
329+
" \"{{/fieldParam}}\"\n" +
330+
" ]\n" +
331+
" }\n" +
332+
" }\n" +
333+
" }\n" +
334+
" }\n" +
335+
"}";
321336
assertAcked(
322337
client().admin().cluster().preparePutStoredScript()
323338
.setId("4")
324-
.setContent(BytesReference.bytes(jsonBuilder().startObject().field("template", multiQuery).endObject()),
325-
XContentType.JSON)
339+
.setContent(new BytesArray(multiQuery), XContentType.JSON)
326340
);
327341
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
328342
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
@@ -342,7 +356,6 @@ public void testIndexedTemplateWithArray() throws Exception {
342356
.setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams)
343357
.get();
344358
assertHitCount(searchResponse.getResponse(), 5);
345-
assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element.");
346359
}
347360

348361
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.action.admin.cluster.storedscripts;
2121

22-
import org.elasticsearch.Version;
2322
import org.elasticsearch.action.ActionResponse;
2423
import org.elasticsearch.common.ParseField;
2524
import org.elasticsearch.common.io.stream.StreamInput;
@@ -114,11 +113,7 @@ public void readFrom(StreamInput in) throws IOException {
114113
super.readFrom(in);
115114

116115
if (in.readBoolean()) {
117-
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
118-
source = new StoredScriptSource(in);
119-
} else {
120-
source = new StoredScriptSource(in.readString());
121-
}
116+
source = new StoredScriptSource(in);
122117
} else {
123118
source = null;
124119
}
@@ -136,12 +131,7 @@ public void writeTo(StreamOutput out) throws IOException {
136131
out.writeBoolean(false);
137132
} else {
138133
out.writeBoolean(true);
139-
140-
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
141-
source.writeTo(out);
142-
} else {
143-
out.writeString(source.getSource());
144-
}
134+
source.writeTo(out);
145135
}
146136
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
147137
out.writeString(id);

server/src/main/java/org/elasticsearch/script/StoredScriptSource.java

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919

2020
package org.elasticsearch.script;
2121

22-
import org.elasticsearch.Version;
23-
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
2422
import org.elasticsearch.cluster.AbstractDiffable;
2523
import org.elasticsearch.cluster.ClusterState;
2624
import org.elasticsearch.cluster.Diff;
2725
import org.elasticsearch.common.ParseField;
2826
import org.elasticsearch.common.ParsingException;
2927
import org.elasticsearch.common.Strings;
30-
import org.elasticsearch.common.bytes.BytesArray;
3128
import org.elasticsearch.common.bytes.BytesReference;
3229
import org.elasticsearch.common.io.stream.StreamInput;
3330
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -271,27 +268,8 @@ private static StoredScriptSource parseRemaining(Token token, XContentParser par
271268
* }
272269
* }
273270
*
274-
* The simple template format:
275-
*
276-
* {@code
277-
* {
278-
* "query" : ...
279-
* }
280-
* }
281-
*
282-
* The complex template format:
283-
*
284-
* {@code
285-
* {
286-
* "template": {
287-
* "query" : ...
288-
* }
289-
* }
290-
* }
291-
*
292-
* Note that templates can be handled as both strings and complex JSON objects.
293-
* Also templates may be part of the 'source' parameter in a script. The Parser
294-
* can handle this case as well.
271+
* Note that the "source" parameter can also handle template parsing including from
272+
* a complex JSON object.
295273
*
296274
* @param content The content from the request to be parsed as described above.
297275
* @return The parsed {@link StoredScriptSource}.
@@ -316,7 +294,7 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
316294

317295
if (token != Token.FIELD_NAME) {
318296
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + ", expected [" +
319-
SCRIPT_PARSE_FIELD.getPreferredName() + ", " + TEMPLATE_PARSE_FIELD.getPreferredName());
297+
SCRIPT_PARSE_FIELD.getPreferredName() + "]");
320298
}
321299

322300
String name = parser.currentName();
@@ -329,28 +307,9 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
329307
} else {
330308
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, <source>]");
331309
}
332-
} else if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) {
333-
334-
DEPRECATION_LOGGER.deprecated("the template context is now deprecated. Specify templates in a \"script\" element.");
335-
336-
token = parser.nextToken();
337-
if (token == Token.VALUE_STRING) {
338-
String source = parser.text();
339-
340-
if (source == null || source.isEmpty()) {
341-
DEPRECATION_LOGGER.deprecated("empty templates should no longer be used");
342-
}
343-
344-
return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, source, Collections.emptyMap());
345-
} else {
346-
return parseRemaining(token, parser);
347-
}
348-
} else if (TEMPLATE_NO_WRAPPER_PARSE_FIELD.getPreferredName().equals(name)) {
349-
DEPRECATION_LOGGER.deprecated("the template context is now deprecated. Specify templates in a \"script\" element.");
350-
return parseRemaining(token, parser);
351310
} else {
352-
DEPRECATION_LOGGER.deprecated("scripts should not be stored without a context. Specify them in a \"script\" element.");
353-
return parseRemaining(token, parser);
311+
throw new ParsingException(parser.getTokenLocation(), "unexpected field [" + name + "], expected [" +
312+
SCRIPT_PARSE_FIELD.getPreferredName() + "]");
354313
}
355314
} catch (IOException ioe) {
356315
throw new UncheckedIOException(ioe);
@@ -397,16 +356,6 @@ public static Diff<StoredScriptSource> readDiffFrom(StreamInput in) throws IOExc
397356
private final String source;
398357
private final Map<String, String> options;
399358

400-
/**
401-
* Constructor for use with {@link GetStoredScriptResponse}
402-
* to support the deprecated stored script namespace.
403-
*/
404-
public StoredScriptSource(String source) {
405-
this.lang = null;
406-
this.source = Objects.requireNonNull(source);
407-
this.options = null;
408-
}
409-
410359
/**
411360
* Standard StoredScriptSource constructor.
412361
* @param lang The language to compile the script with. Must not be {@code null}.
@@ -426,35 +375,24 @@ public StoredScriptSource(String lang, String source, Map<String, String> option
426375
* only the source parameter will be read in as a bytes reference.
427376
*/
428377
public StoredScriptSource(StreamInput in) throws IOException {
429-
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
430-
this.lang = in.readString();
431-
this.source = in.readString();
432-
@SuppressWarnings("unchecked")
433-
Map<String, String> options = (Map<String, String>)(Map)in.readMap();
434-
this.options = options;
435-
} else {
436-
this.lang = null;
437-
this.source = in.readBytesReference().utf8ToString();
438-
this.options = null;
439-
}
378+
this.lang = in.readString();
379+
this.source = in.readString();
380+
@SuppressWarnings("unchecked")
381+
Map<String, String> options = (Map<String, String>)(Map)in.readMap();
382+
this.options = options;
440383
}
441384

442385
/**
443-
* Writes a {@link StoredScriptSource} to a stream. Version 5.3+ will write
444-
* all of the lang, source, and options parameters. For versions prior to 5.3,
445-
* only the source parameter will be read in as a bytes reference.
386+
* Writes a {@link StoredScriptSource} to a stream. Will write
387+
* all of the lang, source, and options parameters.
446388
*/
447389
@Override
448390
public void writeTo(StreamOutput out) throws IOException {
449-
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
450-
out.writeString(lang);
451-
out.writeString(source);
452-
@SuppressWarnings("unchecked")
453-
Map<String, Object> options = (Map<String, Object>)(Map)this.options;
454-
out.writeMap(options);
455-
} else {
456-
out.writeBytesReference(new BytesArray(source));
457-
}
391+
out.writeString(lang);
392+
out.writeString(source);
393+
@SuppressWarnings("unchecked")
394+
Map<String, Object> options = (Map<String, Object>)(Map)this.options;
395+
out.writeMap(options);
458396
}
459397

460398
/**

0 commit comments

Comments
 (0)