ES Test
ES Test

Reputation: 11

How to send stored painless script id to spring data elasticsearch update query?

Updatequery.builder only has withscript(string script) function to send the script source , how to send the script id in case of stored script? . I am using spring data elasticsearch 4.1. Thanks

Edit to show the error with Spring data elasticsearch 4.2.1

Map<String,Object> params = new HashMap<>(); params.put("message","this is a test message to test");

UpdateQuery updateQuery = UpdateQuery.builder(Query.findAll()).withScriptType(ScriptType.STORED) .withScriptName("updateScript01").withParams(params).build(); this.elasticsearchRestTemplate(updateQuery,IndexCoordniates.of("sampleIndex1"));

exception: Caused by: org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: id is missing;2: script or doc is missing; at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:26) at org.elasticsearch.action.update.UpdateRequest.validate(UpdateRequest.java:206)

Stored script in elasticsearch from kibana: POST _scripts/updateScript01 { "script": { "lang": "painless", "source": "ctx._source.message = params.message;" } }

Upvotes: 0

Views: 1916

Answers (2)

Abacus
Abacus

Reputation: 19471

Support for using stored scripts was added in version 4.2. For usage see Sahil's answer.

Edit:

After looking at your sample code:

You need to use updateByQuery(), not update():

this.elasticsearchOperations.updateByQuery(updateQuery,IndexCoordinates.of("sampleindex1"));

btw: "indexSample1" as you had it is no valid Elasticsearch index name. Capital letters are not allowed in an index name.

Upvotes: 1

Sahil Gupta
Sahil Gupta

Reputation: 2166

Use the below methods with ScriptType->Stored and name being id.

enter image description here

Upvotes: 0

Related Questions