Skip to content

Commit

Permalink
Scripting: Remove support for deprecated StoredScript contexts (#31394)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sohaibiftikhar authored and nik9000 committed Jul 5, 2018
1 parent 894fb97 commit 40b822c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 271 deletions.
4 changes: 4 additions & 0 deletions docs/reference/migration/migrate_7_0/api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ will be for such settings to be copied on such operations. To enable users in
`copy_settings` parameter was added on the REST layer. As this behavior will be
the only behavior in 8.0.0, this parameter is deprecated in 7.0.0 for removal in
8.0.0.

==== The deprecated stored script contexts have now been removed
When putting stored scripts, support for storing them with the deprecated `template` context or without a context is
now removed. Scripts must be stored using the `script` context as mentioned in the documentation.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.plugins.Plugin;
Expand Down Expand Up @@ -152,25 +151,22 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws
public void testIndexedTemplateClient() throws Exception {
assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("testTemplate")
.setContent(new BytesArray("{" +
"\"template\":{" +
" \"query\":{" +
" \"match\":{" +
" \"theField\" : \"{{fieldParam}}\"}" +
" }" +
"}" +
"}"), XContentType.JSON));


assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("testTemplate").setContent(new BytesArray("{" +
"\"template\":{" +
" \"query\":{" +
" \"match\":{" +
" \"theField\" : \"{{fieldParam}}\"}" +
" }" +
"}" +
"}"), XContentType.JSON));
.setContent(
new BytesArray(
"{" +
" \"script\": {" +
" \"lang\": \"mustache\"," +
" \"source\": {" +
" \"query\": {" +
" \"match\": {" +
" \"theField\": \"{{fieldParam}}\"" +
" }" +
" }" +
" }" +
" }" +
"}"
),
XContentType.JSON));

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

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 {
assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("1a")
.setContent(new BytesArray("{" +
"\"template\":{" +
" \"query\":{" +
" \"match\":{" +
" \"theField\" : \"{{fieldParam}}\"}" +
" }" +
"}" +
"}"
), XContentType.JSON)

String script =
"{" +
" \"script\": {" +
" \"lang\": \"mustache\"," +
" \"source\": {" +
" \"query\": {" +
" \"match\": {" +
" \"theField\": \"{{fieldParam}}\"" +
" }" +
" }" +
" }" +
" }" +
"}";

assertAcked(
client().admin().cluster().preparePutStoredScript().setId("1a").setContent(new BytesArray(script), XContentType.JSON)
);
assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("2")
.setContent(new BytesArray("{" +
"\"template\":{" +
" \"query\":{" +
" \"match\":{" +
" \"theField\" : \"{{fieldParam}}\"}" +
" }" +
"}" +
"}"), XContentType.JSON)
assertAcked(
client().admin().cluster().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON)
);
assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("3")
.setContent(new BytesArray("{" +
"\"template\":{" +
" \"match\":{" +
" \"theField\" : \"{{fieldParam}}\"}" +
" }" +
"}"), XContentType.JSON)
assertAcked(
client().admin().cluster().preparePutStoredScript().setId("3").setContent(new BytesArray(script), XContentType.JSON)
);

BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
Expand Down Expand Up @@ -268,7 +255,6 @@ public void testIndexedTemplate() throws Exception {
.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
Expand All @@ -282,13 +268,27 @@ public void testIndexedTemplateOverwrite() throws Exception {
client().admin().indices().prepareRefresh().get();

int iterations = randomIntBetween(2, 11);
String query =
"{" +
" \"script\": {" +
" \"lang\": \"mustache\"," +
" \"source\": {" +
" \"query\": {" +
" \"match_phrase_prefix\": {" +
" \"searchtext\": {" +
" \"query\": \"{{P_Keyword1}}\"," +
" \"slop\": {{slop}}" +
" }" +
" }" +
" }" +
" }" +
" }" +
"}";
for (int i = 1; i < iterations; i++) {
assertAcked(client().admin().cluster().preparePutStoredScript()
.setId("git01")
.setContent(new BytesArray(
"{\"template\":{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\","
+ "\"slop\": -1}}}}}"),
XContentType.JSON));
.setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(-1))), XContentType.JSON)
);

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

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

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

public void testIndexedTemplateWithArray() throws Exception {
String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
String multiQuery =
"{\n" +
" \"script\": {\n" +
" \"lang\": \"mustache\",\n" +
" \"source\": {\n" +
" \"query\": {\n" +
" \"terms\": {\n" +
" \"theField\": [\n" +
" \"{{#fieldParam}}\",\n" +
" \"{{.}}\",\n" +
" \"{{/fieldParam}}\"\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
assertAcked(
client().admin().cluster().preparePutStoredScript()
.setId("4")
.setContent(BytesReference.bytes(jsonBuilder().startObject().field("template", multiQuery).endObject()),
XContentType.JSON)
.setContent(new BytesArray(multiQuery), XContentType.JSON)
);
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
Expand All @@ -342,7 +356,6 @@ public void testIndexedTemplateWithArray() throws Exception {
.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.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);

if (in.readBoolean()) {
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
source = new StoredScriptSource(in);
} else {
source = new StoredScriptSource(in.readString());
}
source = new StoredScriptSource(in);
} else {
source = null;
}
Expand All @@ -136,12 +132,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(false);
} else {
out.writeBoolean(true);

if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
source.writeTo(out);
} else {
out.writeString(source.getSource());
}
source.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeString(id);
Expand Down
Loading

0 comments on commit 40b822c

Please sign in to comment.