# 7.16 Java Client - UpdateRequest example

**URL:** <https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 31, 2022, 1:40pm UTC](https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846 "2022-01-31T13:40:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [January 31, 2022, 1:40pm UTC](https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846/1 "2022-01-31T13:40:33Z")

</div>

Hi, I'm struggling to understand how to do an UpdateRequest with the latest java client.

For example, this:

```auto
        client.update(new UpdateRequest.Builder<>()
                .index(index)
                .id(id)
                .doc(document)
                .build());

```

Won't compile, error:

```auto
Cannot resolve method 'update(co.elastic.clients.elasticsearch.core.UpdateRequest<java.lang.Object,java.lang.Object>)'

```

Can I do partial document updates using the IndexRequest instead?

An example for how to do a script in an UpdateRequest would be really great as well.

Hoping someone can point me in the right direction!

Cheers

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [January 31, 2022, 1:51pm UTC](https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846/2 "2022-01-31T13:51:37Z")

</div>

Trying this for scripted update:

```auto
        client().update(new UpdateRequest.Builder<>()
                .index(index)
                .id(id)
                .lang("painless")
                .script(new Script.Builder()
                        .inline(new InlineScript.Builder()
                                .source("ctx._source.status = '"+ status +"'")
                                .build())
                        .build())
                .build());

```

but nothing compiles, so I don't know what's supposed to work. 🤷

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [January 31, 2022, 3:22pm UTC](https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846/3 "2022-01-31T15:22:16Z")

</div>

Thanks for the feedback @ndtreviv. The update API requires an additional `Class` parameter.

The reason for this is the `source` parameter that allows retrieving the modified document. In your case you're not requesting it, so we can use the `Void` class.

Assuming the class of `document` is `SomeDoc`:

```java
client.update(new UpdateRequest.Builder<Void, SomeDoc>()
    .index(index)
    .id(id)
    .doc(document)
    .build(),
    Void.class // <-- additional parameter
);

```

You can also write it in a more compact form as:

```java
client.update(b -> b
    .index(index)
    .id(id)
    .doc(appData),
    Void.class
);

```

and similarly for the script update:

```java
client.update(u -> u
    .index(index)
    .id(id)
    .lang("painless")
    .script(s -> s
        .inline(i -> i
            .source("ctx._source.status = '"+ status +"'")
        )
    ),
    Void.class
);

```

Hope this helps. We're currently improving the docs, a section on the update request will be added there.

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [January 31, 2022, 4:25pm UTC](https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846/4 "2022-01-31T16:25:35Z")

</div>

Thank you! I'm getting the hang of it now 😅

---

<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:** [February 28, 2022, 4:26pm UTC](https://discuss.elastic.co/t/7-16-java-client-updaterequest-example/295846/5 "2022-02-28T16:26:03Z")

</div>

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