use of org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testPutScript.
public void testPutScript() throws Exception {
PutStoredScriptRequest putStoredScriptRequest = new PutStoredScriptRequest();
String id = randomAlphaOfLengthBetween(5, 10);
putStoredScriptRequest.id(id);
XContentType xContentType = randomFrom(XContentType.values());
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
builder.startObject();
builder.startObject("script").field("lang", "painless").field("source", "Math.log(_score * 2) + params.multiplier").endObject();
builder.endObject();
putStoredScriptRequest.content(BytesReference.bytes(builder), xContentType);
}
Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(putStoredScriptRequest, expectedParams);
setRandomTimeout(putStoredScriptRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
if (randomBoolean()) {
String context = randomAlphaOfLengthBetween(5, 10);
putStoredScriptRequest.context(context);
expectedParams.put("context", context);
}
Request request = RequestConverters.putScript(putStoredScriptRequest);
assertThat(request.getEndpoint(), equalTo("/_scripts/" + id));
assertThat(request.getParameters(), equalTo(expectedParams));
assertNotNull(request.getEntity());
assertToXContentBody(putStoredScriptRequest, request.getEntity());
}
use of org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest in project OpenSearch by opensearch-project.
the class StoredScriptsDocumentationIT method testPutScript.
public void testPutScript() throws Exception {
RestHighLevelClient client = highLevelClient();
{
// tag::put-stored-script-request
PutStoredScriptRequest request = new PutStoredScriptRequest();
// <1>
request.id("id");
request.content(new BytesArray("{\n" + "\"script\": {\n" + "\"lang\": \"painless\",\n" + "\"source\": \"Math.log(_score * 2) + params.multiplier\"" + "}\n" + "}\n"), // <2>
XContentType.JSON);
// end::put-stored-script-request
// tag::put-stored-script-context
// <1>
request.context("context");
// end::put-stored-script-context
// tag::put-stored-script-timeout
// <1>
request.timeout(TimeValue.timeValueMinutes(2));
// <2>
request.timeout("2m");
// end::put-stored-script-timeout
// tag::put-stored-script-masterTimeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
// <2>
request.masterNodeTimeout("1m");
// end::put-stored-script-masterTimeout
}
{
PutStoredScriptRequest request = new PutStoredScriptRequest();
request.id("id");
// tag::put-stored-script-content-painless
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("script");
{
builder.field("lang", "painless");
builder.field("source", "Math.log(_score * 2) + params.multiplier");
}
builder.endObject();
}
builder.endObject();
// <1>
request.content(BytesReference.bytes(builder), XContentType.JSON);
// end::put-stored-script-content-painless
// tag::put-stored-script-execute
AcknowledgedResponse putStoredScriptResponse = client.putScript(request, RequestOptions.DEFAULT);
// end::put-stored-script-execute
// tag::put-stored-script-response
// <1>
boolean acknowledged = putStoredScriptResponse.isAcknowledged();
// end::put-stored-script-response
assertTrue(acknowledged);
// tag::put-stored-script-execute-listener
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::put-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::put-stored-script-execute-async
// <1>
client.putScriptAsync(request, RequestOptions.DEFAULT, listener);
// end::put-stored-script-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
{
PutStoredScriptRequest request = new PutStoredScriptRequest();
request.id("id");
// tag::put-stored-script-content-mustache
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("script");
{
builder.field("lang", "mustache");
builder.field("source", "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}");
}
builder.endObject();
}
builder.endObject();
// <1>
request.content(BytesReference.bytes(builder), XContentType.JSON);
// end::put-stored-script-content-mustache
client.putScript(request, RequestOptions.DEFAULT);
Map<String, Object> script = getAsMap("/_scripts/id");
assertThat(extractValue("script.lang", script), equalTo("mustache"));
assertThat(extractValue("script.source", script), equalTo("{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}"));
}
}
use of org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest 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.action.admin.cluster.storedscripts.PutStoredScriptRequest 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.PutStoredScriptRequest 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"));
}
Aggregations