Skip to content

Commit 40b822c

Browse files
sohaibiftikharnik9000
authored andcommitted
Scripting: Remove support for deprecated StoredScript contexts (#31394)
Removes support for storing scripts without the usual json around the script. So You can no longer do: ``` POST _scripts/<templatename> { "query": { "match": { "title": "{{query_string}}" } } } ``` and must instead do: ``` POST _scripts/<templatename> { "script": { "lang": "mustache", "source": { "query": { "match": { "title": "{{query_string}}" } } } } } ``` This improves error reporting when you attempt to store a script but don't quite get the syntax right. Before, there was a good chance that we'd think of it as a "raw" template and just store it. Now we won't do that. Nice.
1 parent 894fb97 commit 40b822c

File tree

8 files changed

+141
-271
lines changed

8 files changed

+141
-271
lines changed

docs/reference/migration/migrate_7_0/api.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ will be for such settings to be copied on such operations. To enable users in
7575
`copy_settings` parameter was added on the REST layer. As this behavior will be
7676
the only behavior in 8.0.0, this parameter is deprecated in 7.0.0 for removal in
7777
8.0.0.
78+
79+
==== The deprecated stored script contexts have now been removed
80+
When putting stored scripts, support for storing them with the deprecated `template` context or without a context is
81+
now removed. Scripts must be stored using the `script` context as mentioned in the documentation.

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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,7 @@ public void readFrom(StreamInput in) throws IOException {
114114
super.readFrom(in);
115115

116116
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-
}
117+
source = new StoredScriptSource(in);
122118
} else {
123119
source = null;
124120
}
@@ -136,12 +132,7 @@ public void writeTo(StreamOutput out) throws IOException {
136132
out.writeBoolean(false);
137133
} else {
138134
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-
}
135+
source.writeTo(out);
145136
}
146137
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
147138
out.writeString(id);

0 commit comments

Comments
 (0)