Skip to content

Commit dd3bd38

Browse files
committed
Merge script related code
1 parent 09be31d commit dd3bd38

File tree

9 files changed

+218
-250
lines changed

9 files changed

+218
-250
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
package org.elasticsearch.client;
2121

2222
import org.elasticsearch.action.ActionListener;
23-
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
24-
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptResponse;
2523
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
2624
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
2725
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
@@ -723,33 +721,4 @@ public void getTemplateAsync(GetIndexTemplatesRequest getIndexTemplatesRequest,
723721
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest, RequestConverters::getTemplates,
724722
options, GetIndexTemplatesResponse::fromXContent, listener, emptySet());
725723
}
726-
727-
/**
728-
* Puts an stored script using the Scripting API.
729-
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-scripting.html"> Scripting API
730-
* on elastic.co</a>
731-
* @param putStoredScriptRequest the request
732-
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
733-
* @return the response
734-
* @throws IOException in case there is a problem sending the request or parsing back the response
735-
*/
736-
public PutStoredScriptResponse putScript(PutStoredScriptRequest putStoredScriptRequest,
737-
RequestOptions options) throws IOException {
738-
return restHighLevelClient.performRequestAndParseEntity(putStoredScriptRequest, RequestConverters::putScript, options,
739-
PutStoredScriptResponse::fromXContent, emptySet());
740-
}
741-
742-
/**
743-
* Asynchronously puts an stored script using the Scripting API.
744-
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-scripting.html"> Scripting API
745-
* on elastic.co</a>
746-
* @param putStoredScriptRequest the request
747-
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
748-
* @param listener the listener to be notified upon request completion
749-
*/
750-
public void putScriptAsync(PutStoredScriptRequest putStoredScriptRequest, RequestOptions options,
751-
ActionListener<PutStoredScriptResponse> listener) {
752-
restHighLevelClient.performRequestAsyncAndParseEntity(putStoredScriptRequest, RequestConverters::putScript, options,
753-
PutStoredScriptResponse::fromXContent, listener, emptySet());
754-
}
755724
}

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptResponse;
3131
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
3232
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
33+
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
34+
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptResponse;
3335
import org.elasticsearch.action.bulk.BulkRequest;
3436
import org.elasticsearch.action.bulk.BulkResponse;
3537
import org.elasticsearch.action.delete.DeleteRequest;
@@ -712,6 +714,35 @@ public void deleteScriptAsync(DeleteStoredScriptRequest request, RequestOptions
712714
DeleteStoredScriptResponse::fromXContent, listener, emptySet());
713715
}
714716

717+
/**
718+
* Puts an stored script using the Scripting API.
719+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-scripting.html"> Scripting API
720+
* on elastic.co</a>
721+
* @param putStoredScriptRequest the request
722+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
723+
* @return the response
724+
* @throws IOException in case there is a problem sending the request or parsing back the response
725+
*/
726+
public PutStoredScriptResponse putScript(PutStoredScriptRequest putStoredScriptRequest,
727+
RequestOptions options) throws IOException {
728+
return performRequestAndParseEntity(putStoredScriptRequest, RequestConverters::putScript, options,
729+
PutStoredScriptResponse::fromXContent, emptySet());
730+
}
731+
732+
/**
733+
* Asynchronously puts an stored script using the Scripting API.
734+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-scripting.html"> Scripting API
735+
* on elastic.co</a>
736+
* @param putStoredScriptRequest the request
737+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
738+
* @param listener the listener to be notified upon request completion
739+
*/
740+
public void putScriptAsync(PutStoredScriptRequest putStoredScriptRequest, RequestOptions options,
741+
ActionListener<PutStoredScriptResponse> listener) {
742+
performRequestAsyncAndParseEntity(putStoredScriptRequest, RequestConverters::putScript, options,
743+
PutStoredScriptResponse::fromXContent, listener, emptySet());
744+
}
745+
715746
/**
716747
* Asynchronously executes a request using the Field Capabilities API.
717748
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-field-caps.html">Field Capabilities API

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.apache.http.client.methods.HttpPut;
2525
import org.elasticsearch.ElasticsearchException;
2626
import org.elasticsearch.ElasticsearchStatusException;
27-
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
28-
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptResponse;
2927
import org.elasticsearch.action.admin.indices.alias.Alias;
3028
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
3129
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
@@ -76,14 +74,12 @@
7674
import org.elasticsearch.cluster.metadata.IndexMetaData;
7775
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
7876
import org.elasticsearch.common.ValidationException;
79-
import org.elasticsearch.common.bytes.BytesReference;
8077
import org.elasticsearch.common.settings.Setting;
8178
import org.elasticsearch.common.settings.Settings;
8279
import org.elasticsearch.common.unit.ByteSizeUnit;
8380
import org.elasticsearch.common.unit.ByteSizeValue;
8481
import org.elasticsearch.common.unit.TimeValue;
8582
import org.elasticsearch.common.xcontent.XContentBuilder;
86-
import org.elasticsearch.common.xcontent.XContentType;
8783
import org.elasticsearch.common.xcontent.json.JsonXContent;
8884
import org.elasticsearch.common.xcontent.support.XContentMapValues;
8985
import org.elasticsearch.index.IndexSettings;
@@ -1244,31 +1240,4 @@ public void testGetIndexTemplate() throws Exception {
12441240
new GetIndexTemplatesRequest().names("the-template-*"), client.indices()::getTemplate, client.indices()::getTemplateAsync));
12451241
assertThat(notFound.status(), equalTo(RestStatus.NOT_FOUND));
12461242
}
1247-
1248-
public void testPutScript() throws Exception {
1249-
RestHighLevelClient client = highLevelClient();
1250-
XContentType xContentType = randomFrom(XContentType.values());
1251-
PutStoredScriptRequest request = new PutStoredScriptRequest()
1252-
.id("script1");
1253-
1254-
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
1255-
builder.startObject();
1256-
builder.startObject("script")
1257-
.field("lang", "painless")
1258-
.field("source", "Math.log(_score * 2) + params.multiplier")
1259-
.endObject();
1260-
builder.endObject();
1261-
request.content(BytesReference.bytes(builder), xContentType);
1262-
}
1263-
1264-
PutStoredScriptResponse response = execute(request,
1265-
client.indices()::putScript, client.indices()::putScriptAsync);
1266-
assertThat(response.isAcknowledged(), equalTo(true));
1267-
1268-
Map<String, Object> script = getAsMap("/_scripts/script1");
1269-
assertThat(extractValue("_id", script), equalTo("script1"));
1270-
assertThat(extractValue("found", script), equalTo(true));
1271-
assertThat(extractValue("script.lang", script), equalTo("painless"));
1272-
assertThat(extractValue("script.source", script), equalTo("Math.log(_score * 2) + params.multiplier"));
1273-
}
12741243
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
3838
import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest;
3939
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
40-
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
4140
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
4241
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
42+
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
4343
import org.elasticsearch.action.admin.indices.alias.Alias;
4444
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
4545
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
@@ -1951,7 +1951,7 @@ public void testGetTemplateRequest() throws Exception {
19511951
assertThat(request.getEntity(), nullValue());
19521952
}
19531953

1954-
public void testPutStoredScript() throws Exception {
1954+
public void testPutScript() throws Exception {
19551955
PutStoredScriptRequest putStoredScriptRequest = new PutStoredScriptRequest();
19561956

19571957
String id = randomAlphaOfLengthBetween(5, 10);
@@ -1984,8 +1984,9 @@ public void testPutStoredScript() throws Exception {
19841984
assertThat(request.getEndpoint(), equalTo("/_scripts/" + id));
19851985
assertThat(request.getParameters(), equalTo(expectedParams));
19861986
assertThat(request.getEntity(), notNullValue());
1987+
assertToXContentBody(putStoredScriptRequest, request.getEntity());
19871988
}
1988-
1989+
19891990
public void testGetScriptRequest() {
19901991
GetStoredScriptRequest getStoredScriptRequest = new GetStoredScriptRequest("x-script");
19911992
Map<String, String> expectedParams = new HashMap<>();
Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package org.elasticsearch.client;/*
1+
package org.elasticsearch.client;
2+
/*
23
* Licensed to Elasticsearch under one or more contributor
34
* license agreements. See the NOTICE file distributed with
45
* this work for additional information regarding copyright
@@ -17,50 +18,42 @@
1718
* under the License.
1819
*/
1920

20-
21-
import org.apache.http.entity.ContentType;
22-
import org.apache.http.entity.StringEntity;
23-
import org.apache.http.util.EntityUtils;
2421
import org.elasticsearch.ElasticsearchStatusException;
2522
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
2623
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptResponse;
2724
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
2825
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
29-
import org.elasticsearch.common.Strings;
30-
import org.elasticsearch.common.xcontent.ToXContent;
26+
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
27+
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptResponse;
28+
import org.elasticsearch.common.bytes.BytesArray;
3129
import org.elasticsearch.common.xcontent.XContentType;
3230
import org.elasticsearch.rest.RestStatus;
3331
import org.elasticsearch.script.Script;
3432
import org.elasticsearch.script.StoredScriptSource;
3533

3634
import java.util.Collections;
35+
import java.util.Map;
3736

38-
import static java.util.Collections.emptyMap;
39-
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
37+
import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue;
4038
import static org.hamcrest.Matchers.equalTo;
4139

4240
public class StoredScriptsIT extends ESRestHighLevelClientTestCase {
4341

44-
final String id = "calculate-score";
42+
private static final String id = "calculate-score";
4543

4644
public void testGetStoredScript() throws Exception {
4745
final StoredScriptSource scriptSource =
4846
new StoredScriptSource("painless",
4947
"Math.log(_score * 2) + params.my_modifier",
5048
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
5149

52-
final String script = Strings.toString(scriptSource.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS));
53-
// TODO: change to HighLevel PutStoredScriptRequest when it will be ready
54-
// so far - using low-level REST API
55-
Response putResponse =
56-
adminClient()
57-
.performRequest("PUT", "/_scripts/calculate-score", emptyMap(),
58-
new StringEntity("{\"script\":" + script + "}",
59-
ContentType.APPLICATION_JSON));
60-
assertEquals(putResponse.getStatusLine().getReasonPhrase(), 200, putResponse.getStatusLine().getStatusCode());
61-
assertEquals("{\"acknowledged\":true}", EntityUtils.toString(putResponse.getEntity()));
62-
63-
GetStoredScriptRequest getRequest = new GetStoredScriptRequest("calculate-score");
50+
PutStoredScriptRequest request =
51+
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
52+
PutStoredScriptResponse putResponse = execute(request, highLevelClient()::putScript,
53+
highLevelClient()::putScriptAsync);
54+
assertThat(putResponse.isAcknowledged(), equalTo(true));
55+
56+
GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id);
6457
getRequest.masterNodeTimeout("50s");
6558

6659
GetStoredScriptResponse getResponse = execute(getRequest, highLevelClient()::getScript,
@@ -75,16 +68,11 @@ public void testDeleteStoredScript() throws Exception {
7568
"Math.log(_score * 2) + params.my_modifier",
7669
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
7770

78-
final String script = Strings.toString(scriptSource.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS));
79-
// TODO: change to HighLevel PutStoredScriptRequest when it will be ready
80-
// so far - using low-level REST API
81-
Response putResponse =
82-
adminClient()
83-
.performRequest("PUT", "/_scripts/" + id, emptyMap(),
84-
new StringEntity("{\"script\":" + script + "}",
85-
ContentType.APPLICATION_JSON));
86-
assertEquals(putResponse.getStatusLine().getReasonPhrase(), 200, putResponse.getStatusLine().getStatusCode());
87-
assertEquals("{\"acknowledged\":true}", EntityUtils.toString(putResponse.getEntity()));
71+
PutStoredScriptRequest request =
72+
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
73+
PutStoredScriptResponse putResponse = execute(request, highLevelClient()::putScript,
74+
highLevelClient()::putScriptAsync);
75+
assertThat(putResponse.isAcknowledged(), equalTo(true));
8876

8977
DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest(id);
9078
deleteRequest.masterNodeTimeout("50s");
@@ -102,4 +90,23 @@ public void testDeleteStoredScript() throws Exception {
10290
highLevelClient()::getScriptAsync));
10391
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
10492
}
93+
94+
public void testPutScript() throws Exception {
95+
final StoredScriptSource scriptSource =
96+
new StoredScriptSource("painless",
97+
"Math.log(_score * 2) + params.my_modifier",
98+
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
99+
100+
PutStoredScriptRequest request =
101+
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
102+
PutStoredScriptResponse putResponse = execute(request, highLevelClient()::putScript,
103+
highLevelClient()::putScriptAsync);
104+
assertThat(putResponse.isAcknowledged(), equalTo(true));
105+
106+
Map<String, Object> script = getAsMap("/_scripts/" + id);
107+
assertThat(extractValue("_id", script), equalTo(id));
108+
assertThat(extractValue("found", script), equalTo(true));
109+
assertThat(extractValue("script.lang", script), equalTo("painless"));
110+
assertThat(extractValue("script.source", script), equalTo("Math.log(_score * 2) + params.my_modifier"));
111+
}
105112
}

0 commit comments

Comments
 (0)