Search in sources :

Example 1 with StoredScriptSource

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));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) POST(org.opensearch.rest.RestRequest.Method.POST) StoredScriptSource(org.opensearch.script.StoredScriptSource) NodeClient(org.opensearch.client.node.NodeClient) BytesReference(org.opensearch.common.bytes.BytesReference) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) XContentType(org.opensearch.common.xcontent.XContentType) BaseRestHandler(org.opensearch.rest.BaseRestHandler) PUT(org.opensearch.rest.RestRequest.Method.PUT) XContentType(org.opensearch.common.xcontent.XContentType) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource)

Example 2 with StoredScriptSource

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());
        }
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) StreamInput(org.opensearch.common.io.stream.StreamInput) StoredScriptSource(org.opensearch.script.StoredScriptSource) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 3 with StoredScriptSource

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));
}
Also used : DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) BytesArray(org.opensearch.common.bytes.BytesArray) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 4 with StoredScriptSource

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"));
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource)

Example 5 with StoredScriptSource

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));
    }
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) Map(java.util.Map) IOException(java.io.IOException) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Aggregations

StoredScriptSource (org.opensearch.script.StoredScriptSource)8 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)4 BytesArray (org.opensearch.common.bytes.BytesArray)4 IOException (java.io.IOException)3 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)3 List (java.util.List)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ActionListener (org.opensearch.action.ActionListener)2 LatchedActionListener (org.opensearch.action.LatchedActionListener)2 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)2 GetStoredScriptResponse (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)2 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 HashMap (java.util.HashMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1