# New Java API Client: Rewrite an UpdateRequest with Script Part

**URL:** <https://discuss.elastic.co/t/new-java-api-client-rewrite-an-updaterequest-with-script-part/310661>\
**Category:** Elasticsearch\
**Tags:** painless, language-clients\
**Created:** [July 26, 2022, 3:06pm UTC](https://discuss.elastic.co/t/new-java-api-client-rewrite-an-updaterequest-with-script-part/310661 "2022-07-26T15:06:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aga31432432](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@aga31432432](https://discuss.elastic.co/u/aga31432432)\
**Post date:** [July 26, 2022, 3:06pm UTC](https://discuss.elastic.co/t/new-java-api-client-rewrite-an-updaterequest-with-script-part/310661/1 "2022-07-26T15:06:23Z")

</div>

Hello everyone,

We need to migrate some code from the current Java High Level REST Client (HLRC) to the new Java API Client 7.7.15.

I have this snippet of Kotlin code I need to rewrite using the new Java Client API:

```auto
val lockObject = lockObject(
    lockConfiguration.name,
    lockConfiguration.lockAtMostUntil,
    now()
)

val ur = UpdateRequest()
    .index(index)
    .id(lockConfiguration.name)
    .script(
        Script(
             ScriptType.INLINE,
             "painless",
             UPDATE_SCRIPT,
             lockObject
         )
     )
     .upsert(lockObject)
     .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
     val res = highLevelClient.update(ur, RequestOptions.DEFAULT)
     res.result != DocWriteResponse.Result.NOOP

```

Based on the [example mentioned here](https://discuss.elastic.co/t/how-to-update-a-single-field-using-the-java-api-client/306679), I have tried to come up with something, but it still does not look right to me, because:

- I am not sure, how the .script() part need to be rewritten
- I don't know how to handle the refresh policy in the new client code

Here's the code:

```auto
val response = client.update({ u: UpdateRequest.Builder<LockConfiguration, Map<String,Any>> ->
                    u.index(index).id(lockConfiguration.name).doc(lockObject) }, LockConfiguration::class.java)

```

Is there anyone who could contribute an example here doing such an update operation?

Thank you very much for your help

Best regards,  
André

---

<div class="post-metadata">

**Author:** ![aga31432432](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@aga31432432](https://discuss.elastic.co/u/aga31432432)\
**Post date:** [August 15, 2022, 5:12pm UTC](https://discuss.elastic.co/t/new-java-api-client-rewrite-an-updaterequest-with-script-part/310661/2 "2022-08-15T17:12:06Z")

</div>

@swallez Could you maybe provide an example or some access to preliminary documentation on this one? Thank you very much!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 12, 2022, 5:12pm UTC](https://discuss.elastic.co/t/new-java-api-client-rewrite-an-updaterequest-with-script-part/310661/3 "2022-09-12T17:12:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
