Get Stored Script APIedit

Get Stored Script Requestedit

A GetStoredScriptRequest requires an id:

GetStoredScriptRequest request = new GetStoredScriptRequest("calculate-score"); 

The id of the script

Optional argumentsedit

The following arguments can optionally be provided:

request.masterNodeTimeout(TimeValue.timeValueSeconds(50)); 
request.masterNodeTimeout("50s"); 

Timeout to connect to the master node as a TimeValue

Timeout to connect to the master node as a String

Synchronous Executionedit

GetStoredScriptResponse getResponse = client.getScript(request, RequestOptions.DEFAULT);

Asynchronous Executionedit

The asynchronous execution of a get stored script request requires both the GetStoredScriptRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.getScriptAsync(request, RequestOptions.DEFAULT, listener); 

The GetStoredScriptRequest to execute and the ActionListener to use when the execution completes

Action Listeneredit

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for GetStoredScriptResponse looks like:

ActionListener<GetStoredScriptResponse> listener =
    new ActionListener<GetStoredScriptResponse>() {
        @Override
        public void onResponse(GetStoredScriptResponse response) {
            
        }

        @Override
        public void onFailure(Exception e) {
            
        }
    };

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument

Get Stored Script Responseedit

The returned GetStoredScriptResponse allows to retrieve information about the executed operation as follows:

StoredScriptSource storedScriptSource = getResponse.getSource(); 

String lang = storedScriptSource.getLang(); 
String source = storedScriptSource.getSource(); 
Map<String, String> options = storedScriptSource.getOptions(); 

The script object consists of a content and a metadata

The language the script is written in, which defaults to painless.

The content of the script

Any named options that should be passed into the script.