use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest in project OpenSearch by opensearch-project.
the class RestGetStoredScriptAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
String id = request.param("id");
GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id);
getRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getRequest.masterNodeTimeout()));
return channel -> client.admin().cluster().getStoredScript(getRequest, new RestStatusToXContentListener<>(channel));
}
use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest 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.action.admin.cluster.storedscripts.GetStoredScriptRequest 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));
}
}
use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest in project OpenSearch by opensearch-project.
the class StoredScriptsIT method testGetStoredScript.
public void testGetStoredScript() 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));
GetStoredScriptRequest getRequest = new GetStoredScriptRequest("calculate-score");
getRequest.masterNodeTimeout("50s");
GetStoredScriptResponse getResponse = execute(getRequest, highLevelClient()::getScript, highLevelClient()::getScriptAsync);
assertThat(getResponse.getSource(), equalTo(scriptSource));
}
use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testGetScriptRequest.
public void testGetScriptRequest() {
GetStoredScriptRequest getStoredScriptRequest = new GetStoredScriptRequest("x-script");
Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(getStoredScriptRequest, expectedParams);
Request request = RequestConverters.getScript(getStoredScriptRequest);
assertThat(request.getEndpoint(), equalTo("/_scripts/" + getStoredScriptRequest.id()));
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
assertThat(request.getParameters(), equalTo(expectedParams));
assertThat(request.getEntity(), nullValue());
}
Aggregations