# Java API - Update - Append to object array

**URL:** <https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287>\
**Category:** Elasticsearch\
**Created:** [March 25, 2013, 9:21am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287 "2013-03-25T09:21:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JayTee](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JayTee](https://discuss.elastic.co/u/JayTee)\
**Post date:** [March 25, 2013, 9:21am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/1 "2013-03-25T09:21:12Z")

</div>

Hello,

I have some code in Java that adds a new document as follows...

{"types":[{"label":"key","value":"value"}],"name":"Foobar"}

This uses...  
IndexResponse response = client.prepareIndex("typesindex", "type")  
.setSource(json)  
.execute()  
.actionGet();

And works absolutely fine. However, if I want to add to the types list  
using newobject =  
{"label":"key2","value":"test2"}  
and the script  
ctx.\_source.types += newobject

Then I get the following error...

> object mapping [types] trying to serialize a value with no field  
> associated with it, current value [{"label":"key2","value":"test2"}]

My code worked absolutely fine when it was just an array of strings as  
opposed to objects.

Any help much appreciated,  
J

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 25, 2013, 11:27pm UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/2 "2013-03-25T23:27:20Z")

</div>

You are probably trying to add a string '{"label":"key2","value":"test2"}'  
instead of adding an object {"label":"key2","value":"test2"}. With an  
object everything should work just  
fine: [https://github.com/imotov/elasticsearch-test-scripts/blob/master/append\_object.sh](https://github.com/imotov/elasticsearch-test-scripts/blob/master/append_object.sh)

On Monday, March 25, 2013 5:21:12 AM UTC-4, JayTee wrote:

> Hello,
> 
> I have some code in Java that adds a new document as follows...
> 
> {"types":[{"label":"key","value":"value"}],"name":"Foobar"}
> 
> This uses...  
> IndexResponse response = client.prepareIndex("typesindex", "type")  
> .setSource(json)  
> .execute()  
> .actionGet();
> 
> And works absolutely fine. However, if I want to add to the types list  
> using newobject =  
> {"label":"key2","value":"test2"}  
> and the script  
> ctx.\_source.types += newobject
> 
> Then I get the following error...
> 
> > object mapping [types] trying to serialize a value with no field  
> > associated with it, current value [{"label":"key2","value":"test2"}]
> 
> My code worked absolutely fine when it was just an array of strings as  
> opposed to objects.
> 
> Any help much appreciated,  
> J

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![JayTee](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JayTee](https://discuss.elastic.co/u/JayTee)\
**Post date:** [March 26, 2013, 7:10am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/3 "2013-03-26T07:10:02Z")

</div>

Thanks for the quick response, but that is exactly what I am doing. It  
works for me from the command line, just not from Java.  
The following code does not work, but works fine as per your example from  
command line.

//json built by jsonBuilder() and equals {"label":"key3","value":"test3"}

response = client.prepareUpdate("test", "doc", "1")  
.addScriptParam("newobject", json)  
.setScript("ctx.\_source.types += newobject")  
.execute()  
.actionGet();

Throws the exception:  
org.elasticsearch.index.mapper.MapperParsingException: object mapping  
[types] trying to serialize a value with no field associated with it,  
current value [{"label":"key3","value":"test3"}]  
at  
org.elasticsearch.index.mapper.object.ObjectMapper.serializeValue(ObjectMapper.java:583)

On Tuesday, March 26, 2013 3:27:20 AM UTC+4, Igor Motov wrote:

> You are probably trying to add a string '{"label":"key2","value":"test2"}'  
> instead of adding an object {"label":"key2","value":"test2"}. With an  
> object everything should work just fine:  
> [https://github.com/imotov/elasticsearch-test-scripts/blob/master/append\_object.sh](https://github.com/imotov/elasticsearch-test-scripts/blob/master/append_object.sh)
> 
> On Monday, March 25, 2013 5:21:12 AM UTC-4, JayTee wrote:
> 
> > Hello,
> > 
> > I have some code in Java that adds a new document as follows...
> > 
> > {"types":[{"label":"key","value":"value"}],"name":"Foobar"}
> > 
> > This uses...  
> > IndexResponse response = client.prepareIndex("typesindex", "type")  
> > .setSource(json)  
> > .execute()  
> > .actionGet();
> > 
> > And works absolutely fine. However, if I want to add to the types list  
> > using newobject =  
> > {"label":"key2","value":"test2"}  
> > and the script  
> > ctx.\_source.types += newobject
> > 
> > Then I get the following error...
> > 
> > > object mapping [types] trying to serialize a value with no field  
> > > associated with it, current value [{"label":"key2","value":"test2"}]
> > 
> > My code worked absolutely fine when it was just an array of strings as  
> > opposed to objects.
> > 
> > Any help much appreciated,  
> > J

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![JayTee](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JayTee](https://discuss.elastic.co/u/JayTee)\
**Post date:** [March 26, 2013, 7:26am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/4 "2013-03-26T07:26:48Z")

</div>

Ok solved it, the problem occurs when you use the Java  
.addScriptParam(name, value).

To fix, simply craft the script yourself rather than using parameters.  
(Only an issue in Java API, Curl works fine).  
.setScript("ctx.\_source."+FIELD\_NAME+" += "+json)

On Monday, March 25, 2013 1:21:12 PM UTC+4, JayTee wrote:

> Hello,
> 
> I have some code in Java that adds a new document as follows...
> 
> {"types":[{"label":"key","value":"value"}],"name":"Foobar"}
> 
> This uses...  
> IndexResponse response = client.prepareIndex("typesindex", "type")  
> .setSource(json)  
> .execute()  
> .actionGet();
> 
> And works absolutely fine. However, if I want to add to the types list  
> using newobject =  
> {"label":"key2","value":"test2"}  
> and the script  
> ctx.\_source.types += newobject
> 
> Then I get the following error...
> 
> > object mapping [types] trying to serialize a value with no field  
> > associated with it, current value [{"label":"key2","value":"test2"}]
> 
> My code worked absolutely fine when it was just an array of strings as  
> opposed to objects.
> 
> Any help much appreciated,  
> J

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 26, 2013, 10:38am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/5 "2013-03-26T10:38:50Z")

</div>

That solution will cause script to be recompiled on every request. It will  
also open your application for script injection attacks. Try this:

```
    Map<String, Object> newObject = newHashMap();
    newObject.put("label1", "key1");
    newObject.put("value", "test3");
    client.prepareUpdate("test", "doc", "1")
            .addScriptParam("newobject", newObject)
            .setScript("ctx._source.types += newobject")
            .execute()
            .actionGet();

```

On Tuesday, March 26, 2013 3:26:48 AM UTC-4, JayTee wrote:

> Ok solved it, the problem occurs when you use the Java  
> .addScriptParam(name, value).
> 
> To fix, simply craft the script yourself rather than using parameters.  
> (Only an issue in Java API, Curl works fine).  
> .setScript("ctx.\_source."+FIELD\_NAME+" += "+json)
> 
> On Monday, March 25, 2013 1:21:12 PM UTC+4, JayTee wrote:
> 
> > Hello,
> > 
> > I have some code in Java that adds a new document as follows...
> > 
> > {"types":[{"label":"key","value":"value"}],"name":"Foobar"}
> > 
> > This uses...  
> > IndexResponse response = client.prepareIndex("typesindex", "type")  
> > .setSource(json)  
> > .execute()  
> > .actionGet();
> > 
> > And works absolutely fine. However, if I want to add to the types list  
> > using newobject =  
> > {"label":"key2","value":"test2"}  
> > and the script  
> > ctx.\_source.types += newobject
> > 
> > Then I get the following error...
> > 
> > > object mapping [types] trying to serialize a value with no field  
> > > associated with it, current value [{"label":"key2","value":"test2"}]
> > 
> > My code worked absolutely fine when it was just an array of strings as  
> > opposed to objects.
> > 
> > Any help much appreciated,  
> > J

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![JayTee](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JayTee](https://discuss.elastic.co/u/JayTee)\
**Post date:** [March 26, 2013, 11:17am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/6 "2013-03-26T11:17:27Z")

</div>

Excellent, the solution below works and allows me to use parameters  
correctly.

Many thanks for your help!  
J

On Tuesday, March 26, 2013 2:38:50 PM UTC+4, Igor Motov wrote:

> That solution will cause script to be recompiled on every request. It will  
> also open your application for script injection attacks. Try this:
> 
> ```
> Map<String, Object> newObject = newHashMap();
> newObject.put("label1", "key1");
> newObject.put("value", "test3");
> client.prepareUpdate("test", "doc", "1")
> .addScriptParam("newobject", newObject)
> .setScript("ctx._source.types += newobject")
> .execute()
> .actionGet();
> 
> ```
> 
> On Tuesday, March 26, 2013 3:26:48 AM UTC-4, JayTee wrote:
> 
> > Ok solved it, the problem occurs when you use the Java  
> > .addScriptParam(name, value).
> > 
> > To fix, simply craft the script yourself rather than using parameters.  
> > (Only an issue in Java API, Curl works fine).  
> > .setScript("ctx.\_source."+FIELD\_NAME+" += "+json)
> > 
> > On Monday, March 25, 2013 1:21:12 PM UTC+4, JayTee wrote:
> > 
> > > Hello,
> > > 
> > > I have some code in Java that adds a new document as follows...
> > > 
> > > {"types":[{"label":"key","value":"value"}],"name":"Foobar"}
> > > 
> > > This uses...  
> > > IndexResponse response = client.prepareIndex("typesindex", "type")  
> > > .setSource(json)  
> > > .execute()  
> > > .actionGet();
> > > 
> > > And works absolutely fine. However, if I want to add to the types list  
> > > using newobject =  
> > > {"label":"key2","value":"test2"}  
> > > and the script  
> > > ctx.\_source.types += newobject
> > > 
> > > Then I get the following error...
> > > 
> > > > object mapping [types] trying to serialize a value with no field  
> > > > associated with it, current value [{"label":"key2","value":"test2"}]
> > > 
> > > My code worked absolutely fine when it was just an array of strings as  
> > > opposed to objects.
> > > 
> > > Any help much appreciated,  
> > > J

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:44am UTC](https://discuss.elastic.co/t/java-api-update-append-to-object-array/11287/7 "2017-07-06T02:44:26Z")

</div>


