# Partial update to a document with java api

**URL:** https://discuss.elastic.co/t/partial-update-to-a-document-with-java-api/22511
**Category:** Elasticsearch
**Created:** [March 4, 2015, 10:56am UTC](https://discuss.elastic.co/t/partial-update-to-a-document-with-java-api/22511 "2015-03-04T10:56:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jakub\_Stransky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakub_stransky/32/732_2.png) [@Jakub\_Stransky](https://discuss.elastic.co/u/Jakub_Stransky)
#### Post date: [March 4, 2015, 10:56am UTC](https://discuss.elastic.co/t/partial-update-to-a-document-with-java-api/22511/1 "2015-03-04T10:56:01Z")

</div>

Hello,

I am new to elasticsearch and I am trying to update just a portion of the  
document via java api. From the rest client e.g. I am successfull but I am  
really struggling with java api - UpdateRequest. I am updating a second  
level source field with a "json object".

POST /ho/B/3278778460/\_update{  
"script":"ctx.\_source.adresa = upd\_adr",  
"params":{  
"upd\_adr" :{ some\_json }  
}}

This works fine and "some\_json" is updated correctly at correct possition  
etc. However when I tried the same in java/scala

val json\_adresa = "{ some\_json}"

val ss = XContentFactory.jsonBuilder()  
val x = XContentFactory.xContent(XContentType.JSON).createParser(json\_adresa)  
ss.field(address\_key).copyCurrentStructure(x)  
x.close()

val i = new UpdateRequest(storage\_folder,"BYT",adId)  
i.script("ctx.\_source.adresa = upd\_adr")  
i.addScriptParam("upd\_adr",ss)

.. and then send it to update API

I am getting following error:

Caused by: java.io.IOException: Can't write type [class org.elasticsearch.common.xcontent.XContentBuilder]  
at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:407)  
at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:374)  
at org.elasticsearch.common.io.stream.StreamOutput.writeMap(StreamOutput.java:319)  
at org.elasticsearch.action.update.UpdateRequest.writeTo(UpdateRequest.java:662)  
at org.elasticsearch.transport.netty.NettyTransport.sendRequest(NettyTransport.java:601)  
at org.elasticsearch.transport.TransportService.sendRequest(TransportService.java:206)  
... 26 more

Can someone more experienced help?

Thanks a lot  
Jakub

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![savioteles](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/savioteles/32/851_2.png) [@savioteles](https://discuss.elastic.co/u/savioteles)
#### Post date: [March 4, 2015, 2:35pm UTC](https://discuss.elastic.co/t/partial-update-to-a-document-with-java-api/22511/2 "2015-03-04T14:35:09Z")

</div>

Is the json object correct? Print it here please.

2015-03-04 7:56 GMT-03:00 Jakub Stransky [stransky.ja@gmail.com](mailto:stransky.ja@gmail.com):

> Hello,
> 
> I am new to elasticsearch and I am trying to update just a portion of the  
> document via java api. From the rest client e.g. I am successfull but I am  
> really struggling with java api - UpdateRequest. I am updating a second  
> level source field with a "json object".
> 
> POST /ho/B/3278778460/\_update{  
> "script":"ctx.\_source.adresa = upd\_adr",  
> "params":{  
> "upd\_adr" :{ some\_json }  
> }}
> 
> This works fine and "some\_json" is updated correctly at correct possition  
> etc. However when I tried the same in java/scala
> 
> val json\_adresa = "{ some\_json}"
> 
> val ss = XContentFactory.jsonBuilder()  
> val x = XContentFactory.xContent(XContentType.JSON).createParser(json\_adresa)  
> ss.field(address\_key).copyCurrentStructure(x)  
> x.close()
> 
> val i = new UpdateRequest(storage\_folder,"BYT",adId)  
> i.script("ctx.\_source.adresa = upd\_adr")  
> i.addScriptParam("upd\_adr",ss)
> 
> .. and then send it to update API
> 
> I am getting following error:
> 
> Caused by: java.io.IOException: Can't write type [class org.elasticsearch.common.xcontent.XContentBuilder]  
> at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:407)  
> at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:374)  
> at org.elasticsearch.common.io.stream.StreamOutput.writeMap(StreamOutput.java:319)  
> at org.elasticsearch.action.update.UpdateRequest.writeTo(UpdateRequest.java:662)  
> at org.elasticsearch.transport.netty.NettyTransport.sendRequest(NettyTransport.java:601)  
> at org.elasticsearch.transport.TransportService.sendRequest(TransportService.java:206)  
> ... 26 more
> 
> Can someone more experienced help?
> 
> Thanks a lot  
> Jakub
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Regards,

Sávio S. Teles de Oliveira

Co-Founder & Software Engineer at [www.gogeo.io](http://www.gogeo.io).  
PHD student in Computer Science focusing on High Performance Maps Platform  
and Spatial Algorithms.  
voice: +55 62 9136 6996  
[http://br.linkedin.com/in/savioteles](http://br.linkedin.com/in/savioteles)  
[https://twitter.com/savioteless](https://twitter.com/savioteless)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAFKmhPsPdXxPDecF94MG0H8fhYfgje7BeTkY4PTMCLrwJ3yu%2Bg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFKmhPsPdXxPDecF94MG0H8fhYfgje7BeTkY4PTMCLrwJ3yu%2Bg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Jakub\_Stransky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakub_stransky/32/732_2.png) [@Jakub\_Stransky](https://discuss.elastic.co/u/Jakub_Stransky)
#### Post date: [March 5, 2015, 9:13am UTC](https://discuss.elastic.co/t/partial-update-to-a-document-with-java-api/22511/3 "2015-03-05T09:13:06Z")

</div>

Hello,

I am actually fetching the object from ES before, modifing and updating  
back. I tried updating an object as String with json content or as  
XContent. Neither of these works. My complex json object I am trying to  
update follows:

{  
"adresaId": "adfasfa-5-faa",  
"souradnice": {  
"longitude": 0,  
"latitude": 0,  
"geopoint": [  
0.0,  
0.0  
]  
},  
"mesto": "adfasfa 5",  
"mestoComplete": "adfasfa 5",  
"ulice": "faa",  
"uliceComplete": "faa",  
"cisloPopisne": 12,  
"cisloOrientacni": 0,  
"adresaComplete": [  
"adfasfa 5 faa",  
"faa adfasfa 5",  
"adfasfa 5 faa",  
"faa adfasfa 5"  
],  
"adresaCompleteness": "MESTO\_ULICE"  
}

This content I am trying to update under field in main json document.

On Wednesday, 4 March 2015 15:35:42 UTC+1, Sávio Salvarino Teles de  
Oliveira wrote:

> Is the json object correct? Print it here please.
> 
> 2015-03-04 7:56 GMT-03:00 Jakub Stransky \<[stran...@gmail.com](mailto:stran...@gmail.com) \<javascript:\>
> 
> > :
> 
> > Hello,
> > 
> > I am new to elasticsearch and I am trying to update just a portion of the  
> > document via java api. From the rest client e.g. I am successfull but I am  
> > really struggling with java api - UpdateRequest. I am updating a second  
> > level source field with a "json object".
> > 
> > POST /ho/B/3278778460/\_update{  
> > "script":"ctx.\_source.adresa = upd\_adr",  
> > "params":{  
> > "upd\_adr" :{ some\_json }  
> > }}
> > 
> > This works fine and "some\_json" is updated correctly at correct possition  
> > etc. However when I tried the same in java/scala
> > 
> > val json\_adresa = "{ some\_json}"
> > 
> > val ss = XContentFactory.jsonBuilder()  
> > val x = XContentFactory.xContent(XContentType.JSON).createParser(json\_adresa)  
> > ss.field(address\_key).copyCurrentStructure(x)  
> > x.close()
> > 
> > val i = new UpdateRequest(storage\_folder,"BYT",adId)  
> > i.script("ctx.\_source.adresa = upd\_adr")  
> > i.addScriptParam("upd\_adr",ss)
> > 
> > .. and then send it to update API
> > 
> > I am getting following error:
> > 
> > Caused by: java.io.IOException: Can't write type [class org.elasticsearch.common.xcontent.XContentBuilder]  
> > at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:407)  
> > at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:374)  
> > at org.elasticsearch.common.io.stream.StreamOutput.writeMap(StreamOutput.java:319)  
> > at org.elasticsearch.action.update.UpdateRequest.writeTo(UpdateRequest.java:662)  
> > at org.elasticsearch.transport.netty.NettyTransport.sendRequest(NettyTransport.java:601)  
> > at org.elasticsearch.transport.TransportService.sendRequest(TransportService.java:206)  
> > ... 26 more
> > 
> > Can someone more experienced help?
> > 
> > Thanks a lot  
> > Jakub
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b4f0e31f-3016-4a96-af9d-c9bf4d2bcfef%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Regards,
> 
> Sávio S. Teles de Oliveira
> 
> Co-Founder & Software Engineer at [www.gogeo.io](http://www.gogeo.io).  
> PHD student in Computer Science focusing on High Performance Maps Platform  
> and Spatial Algorithms.  
> voice: +55 62 9136 6996  
> [http://br.linkedin.com/in/savioteles](http://br.linkedin.com/in/savioteles)  
> [https://twitter.com/savioteless](https://twitter.com/savioteless)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b2c74023-30ee-4ce8-9a59-3b5c21e74459%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b2c74023-30ee-4ce8-9a59-3b5c21e74459%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [July 6, 2017, 12:28am UTC](https://discuss.elastic.co/t/partial-update-to-a-document-with-java-api/22511/4 "2017-07-06T00:28:22Z")

</div>


