Put Stored Script APIedit

Put Stored Script Requestedit

A PutStoredScriptRequest requires an id and content:

PutStoredScriptRequest request = new PutStoredScriptRequest();
request.id("id"); 
request.content(new BytesArray(
    "{\n" +
        "\"script\": {\n" +
        "\"lang\": \"painless\",\n" +
        "\"source\": \"Math.log(_score * 2) + params.multiplier\"" +
        "}\n" +
        "}\n"
), XContentType.JSON); 

The id of the script

The content of the script

Contentedit

The content of a script can be written in different languages and provided in different ways:

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();
request.content(BytesReference.bytes(builder), XContentType.JSON); 

Specify a painless script and provided as XContentBuilder object. Note that the builder needs to be passed as a BytesReference object

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();
request.content(BytesReference.bytes(builder), XContentType.JSON); 

Specify a mustache script and provided as XContentBuilder object. Note that value of source can be directly provided as a JSON string

Optional argumentsedit

The following arguments can optionally be provided:

request.context("context"); 

The context the script should be executed in.

request.timeout(TimeValue.timeValueMinutes(2)); 
request.timeout("2m"); 

Timeout to wait for the all the nodes to acknowledge the script creation as a TimeValue

Timeout to wait for the all the nodes to acknowledge the script creation as a String

request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); 
request.masterNodeTimeout("1m"); 

Timeout to connect to the master node as a TimeValue

Timeout to connect to the master node as a String

Synchronous Executionedit

AcknowledgedResponse putStoredScriptResponse = client.putScript(request, RequestOptions.DEFAULT);

Asynchronous Executionedit

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

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

The PutStoredScriptRequest 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 AcknowledgedResponse looks like:

ActionListener<AcknowledgedResponse> listener =
    new ActionListener<AcknowledgedResponse>() {
        @Override
        public void onResponse(AcknowledgedResponse 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

Put Stored Script Responseedit

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

boolean acknowledged = putStoredScriptResponse.isAcknowledged(); 

Indicates whether all of the nodes have acknowledged the request