Java API - Update - Append to object array

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.