0

I need to execute an update of elasticsearch document using stored script. I do it first from Elasticsearch GUI and it works. Then I try to do the same thing from java API and it claims script has syntax errors.

First in GUI. I create a stored script:

`POST _scripts/chargepoint_device_status_update2
{
   "script": {
     "source": "if (ctx._source.deviceId==params['deviceId'] && ctx._source.laststatuschangeddate<params['updateTime']) { ctx._source.statusName=params['newStatus'];ctx._source.laststatuschangeddate=params['updateTime']  } else { ctx.op = 'noop' }",
    "lang": "painless"
  }
}`

The script updates record in place if the parameter updateTime is larger than field laststatuschangeddate, otherwise leaves record unchanged.

Now I execute this stored script using parameters:

`POST idx_chargepoint_device_status/_update_by_query
{
  "script": {
    "id": "chargepoint_device_status_update2",
    "params": {
      "updateTime": 1668462920315,
      "newStatus": "available3",
      "deviceId": "52334"
    }
  }
}`

Everything works fine, as expected.

Now I try to execute the stored script chargepoint_device_status_update2 from Java. Spring boot gives me a high level Rest client to this ES and I write code:

` @Autowired private RestHighLevelClient client;

  Map\<String,Object\> parameters=new HashMap\<\>();
  parameters.put("updateTime",1668462920315L);
  parameters.put("newStatus","available3");
  parameters.put("deviceId","52334");

  Script script = new Script(ScriptType.STORED, null, "chargepoint_device_status_update2", parameters);

  QueryBuilder qb = new ScriptQueryBuilder(script);
  UpdateByQueryRequest request = new UpdateByQueryRequest("idx_chargepoint_device_status");
  request.setQuery(qb);
  client.updateByQuery(request, RequestOptions.DEFAULT);`

I execute this and I get errors:

`OpenSearch exception [type=illegal_argument_exception, reason=cannot resolve symbol [ctx._source.deviceId]]

Suppressed: org.opensearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/idx_chargepoint_device_status/_update_by_query?slices=1&requests_per_second=-1&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&wait_for_completion=true&timeout=1m], status line [HTTP/1.1 400 Bad Request]

{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["if (ctx._source.deviceId==params['device ..."," ^---- HERE"],"script":"if (ctx._source.deviceId==params['deviceId'] && ctx._source.laststatuschangeddate<params['updateTime']) { ctx._source.statusName=params['newStatus'];ctx._source.laststatuschangeddate=params['updateTime'] } else { ctx.op = 'noop' }","lang":"painless","position":{"offset":15,"start":0,"end":40}}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"idx_chargepoint_device_status","node":"zMQFoB7kQ2eWVi3kEWOHWA","reason":{"type":"query_shard_exception","reason":"failed to create query: compile error","index":"idx_chargepoint_device_status","index_uuid":"UKGQ1wU7TFWkAKh9zgsmXw","caused_by":{"type":"script_exception","reason":"compile error","script_stack":["if (ctx._source.deviceId==params['device ..."," ^---- HERE"],"script":"if (ctx._source.deviceId==params['deviceId'] && ctx._source.laststatuschangeddate<params['updateTime']) { ctx._source.statusName=params['newStatus'];ctx._source.laststatuschangeddate=params['updateTime'] } else { ctx.op = 'noop' }","lang":"painless","position":{"offset":15,"start":0,"end":40},"caused_by":{"type":"illegal_argument_exception","reason":"cannot resolve symbol [ctx._source.deviceId]"}}}}],"caused_by":{"type":"script_exception","reason":"compile error","script_stack":["if (ctx._source.deviceId==params['device ..."," ^---- HERE"],"script":"if (ctx._source.deviceId==params['deviceId'] && ctx._source.laststatuschangeddate<params['updateTime']) { ctx._source.statusName=params['newStatus'];ctx._source.laststatuschangeddate=params['updateTime'] } else { ctx.op = 'noop' }","lang":"painless","position":{"offset":15,"start":0,"end":40},"caused_by":{"type":"illegal_argument_exception","reason":"cannot resolve symbol [ctx._source.deviceId]"}}},"status":400}


So obviously code connects to ElasticSearch, it sees the stored script, but for some reason the script does not execute and has syntax errors when called from java. But when called from GUI it works fine.

Any ideas what I am doing wrong?`

1 Answer 1

0

Set Script in UpdateByQueryRequest.

This works:

Map<String, Object> parameters = new HashMap<>();
parameters.put("updateTime", 1668462920315L);
parameters.put("newStatus", "available3");
parameters.put("deviceId", "52334");

Script script = new Script(ScriptType.STORED, null, "chargepoint_device_status_update2", parameters);

UpdateByQueryRequest request = new UpdateByQueryRequest("idx_chargepoint_device_status");
request.setScript(script);
var response = client.updateByQuery(request, RequestOptions.DEFAULT);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.