use of org.opensearch.script.StoredScriptSource in project OpenSearch by opensearch-project.
the class RestPutStoredScriptAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String id = request.param("id");
String context = request.param("context");
BytesReference content = request.requiredContent();
XContentType xContentType = request.getXContentType();
StoredScriptSource source = StoredScriptSource.parse(content, xContentType);
PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, context, content, request.getXContentType(), source);
putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout()));
putRequest.timeout(request.paramAsTime("timeout", putRequest.timeout()));
return channel -> client.admin().cluster().putStoredScript(putRequest, new RestToXContentListener<>(channel));
}
use of org.opensearch.script.StoredScriptSource in project OpenSearch by opensearch-project.
the class PutStoredScriptRequestTests method testSerialization.
public void testSerialization() throws IOException {
PutStoredScriptRequest storedScriptRequest = new PutStoredScriptRequest("bar", "context", new BytesArray("{}"), XContentType.JSON, new StoredScriptSource("foo", "bar", Collections.emptyMap()));
assertEquals(XContentType.JSON, storedScriptRequest.xContentType());
try (BytesStreamOutput output = new BytesStreamOutput()) {
storedScriptRequest.writeTo(output);
try (StreamInput in = output.bytes().streamInput()) {
PutStoredScriptRequest serialized = new PutStoredScriptRequest(in);
assertEquals(XContentType.JSON, serialized.xContentType());
assertEquals(storedScriptRequest.id(), serialized.id());
assertEquals(storedScriptRequest.context(), serialized.context());
}
}
}
use of org.opensearch.script.StoredScriptSource in project OpenSearch by opensearch-project.
the class StoredScriptsIT method testDeleteStoredScript.
public void testDeleteStoredScript() throws Exception {
final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest(id);
deleteRequest.masterNodeTimeout("50s");
deleteRequest.timeout("50s");
assertAcked(execute(deleteRequest, highLevelClient()::deleteScript, highLevelClient()::deleteScriptAsync));
GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id);
final OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getRequest, highLevelClient()::getScript, highLevelClient()::getScriptAsync));
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
}
use of org.opensearch.script.StoredScriptSource in project OpenSearch by opensearch-project.
the class StoredScriptsIT method testPutScript.
public void testPutScript() throws Exception {
final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
Map<String, Object> script = getAsMap("/_scripts/" + id);
assertThat(extractValue("_id", script), equalTo(id));
assertThat(extractValue("found", script), equalTo(true));
assertThat(extractValue("script.lang", script), equalTo("painless"));
assertThat(extractValue("script.source", script), equalTo("Math.log(_score * 2) + params.my_modifier"));
}
use of org.opensearch.script.StoredScriptSource in project OpenSearch by opensearch-project.
the class StoredScriptsDocumentationIT method testGetStoredScript.
@SuppressWarnings("unused")
public void testGetStoredScript() throws Exception {
RestHighLevelClient client = highLevelClient();
final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
putStoredScript("calculate-score", scriptSource);
{
// tag::get-stored-script-request
// <1>
GetStoredScriptRequest request = new GetStoredScriptRequest("calculate-score");
// end::get-stored-script-request
// tag::get-stored-script-request-masterTimeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueSeconds(50));
// <2>
request.masterNodeTimeout("50s");
// end::get-stored-script-request-masterTimeout
// tag::get-stored-script-execute
GetStoredScriptResponse getResponse = client.getScript(request, RequestOptions.DEFAULT);
// end::get-stored-script-execute
// tag::get-stored-script-response
// <1>
StoredScriptSource storedScriptSource = getResponse.getSource();
// <2>
String lang = storedScriptSource.getLang();
// <3>
String source = storedScriptSource.getSource();
// <4>
Map<String, String> options = storedScriptSource.getOptions();
// end::get-stored-script-response
assertThat(storedScriptSource, equalTo(scriptSource));
// tag::get-stored-script-execute-listener
ActionListener<GetStoredScriptResponse> listener = new ActionListener<GetStoredScriptResponse>() {
@Override
public void onResponse(GetStoredScriptResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-stored-script-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::get-stored-script-execute-async
// <1>
client.getScriptAsync(request, RequestOptions.DEFAULT, listener);
// end::get-stored-script-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
Aggregations