Search in sources :

Example 1 with PutStoredScriptRequest

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());
}
Also used : XContentType(org.opensearch.common.xcontent.XContentType) HashMap(java.util.HashMap) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 2 with PutStoredScriptRequest

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}}\"}}}"));
    }
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) BytesArray(org.opensearch.common.bytes.BytesArray) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 3 with PutStoredScriptRequest

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));
}
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 4 with PutStoredScriptRequest

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));
}
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 5 with PutStoredScriptRequest

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

Aggregations

PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)8 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)4 BytesArray (org.opensearch.common.bytes.BytesArray)4 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)3 StoredScriptSource (org.opensearch.script.StoredScriptSource)3 IOException (java.io.IOException)2 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 BulkRequest (org.opensearch.action.bulk.BulkRequest)2 DeleteRequest (org.opensearch.action.delete.DeleteRequest)2 ExplainRequest (org.opensearch.action.explain.ExplainRequest)2 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)2 GetRequest (org.opensearch.action.get.GetRequest)2 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)2 WriteRequest (org.opensearch.action.support.WriteRequest)2 UpdateRequest (org.opensearch.action.update.UpdateRequest)2