Search in sources :

Example 1 with GetStoredScriptRequest

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));
}
Also used : List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections.singletonList(java.util.Collections.singletonList) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)

Example 2 with GetStoredScriptRequest

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));
}
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 3 with GetStoredScriptRequest

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));
    }
}
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)

Example 4 with GetStoredScriptRequest

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));
}
Also used : 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) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 5 with GetStoredScriptRequest

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());
}
Also used : HashMap(java.util.HashMap) 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) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)

Aggregations

GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)7 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)4 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)3 BytesArray (org.opensearch.common.bytes.BytesArray)3 StoredScriptSource (org.opensearch.script.StoredScriptSource)3 IOException (java.io.IOException)2 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 GetStoredScriptResponse (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)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