How to use json in update script

Hello

I'm trying to update a document whose root object contains a list of nested
objects. I need to send an object of the nested type as a script parameter
to append to the list

How can I append the json (a string type) to the nested objects list of the
root object using Groovy? or should I use another script lang?

I tried using JsonSlurper http://groovy-lang.org/json.html in Groovy,
that converts between json and Groovy objects, but I always get:

Caused by:

org.elasticsearch.script.groovy.GroovyScriptCompilationException:
MultipleCompilationErrorsException[startup failed:
Script3.groovy: 2: unable to resolve class JsonSlurper
@ line 2, column 19.
def jsonSlurper = new JsonSlurper();
^
1 error
]
at
org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:117)
at
org.elasticsearch.script.ScriptService.getCompiledScript(ScriptService.java:368)
at org.elasticsearch.script.ScriptService.compile(ScriptService.java:354)
at
org.elasticsearch.script.ScriptService.executable(ScriptService.java:497)
at
org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:149)
... 8 more

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAJp2531Qm2GZbvM7CMZSd8sqjUF-VQ%3DN6YUKQam5EOPd9pBvRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ok, I found out that I can send a JSON as a script parameter and just
append it to the nested objects list (with list += newObject or
list.add(newObject) ) using groovy and it works

But it is not working with the Java API, I can only get it to work using
the REST API.

When using Java the JSON is treated as a string, then I get the error:

object mapping [objectsList] trying to serialize a value with no field

associated with it, current value [{"field":"value"}]

I can reproduce the error in the REST API by wrapping the JSON parameter
with quotes:

Works (using REST API):

{

"script": "ctx._source.objectsList += newObject",
"params": {
"newObject": {"field": "value"}
},
"lang": "groovy"
}

Does not work (using REST API):

{

"script": "ctx._source.objectsList += newObject",
"params": {
"newObject": "{"field": "value"}"
},
"lang": "groovy"
}

Does not work (using JAVA API):

String script = "ctx._source.objectsList += newObject";

2014-12-16 13:04 GMT-02:00 Roger de Cordova Farias <
roger.farias@fontec.inf.br>:

Hello

I'm trying to update a document whose root object contains a list of
nested objects. I need to send an object of the nested type as a script
parameter to append to the list

How can I append the json (a string type) to the nested objects list of
the root object using Groovy? or should I use another script lang?

I tried using JsonSlurper http://groovy-lang.org/json.html in Groovy,
that converts between json and Groovy objects, but I always get:

Caused by:

org.elasticsearch.script.groovy.GroovyScriptCompilationException:
MultipleCompilationErrorsException[startup failed:
Script3.groovy: 2: unable to resolve class JsonSlurper
@ line 2, column 19.
def jsonSlurper = new JsonSlurper();
^
1 error
]
at
org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:117)
at
org.elasticsearch.script.ScriptService.getCompiledScript(ScriptService.java:368)
at org.elasticsearch.script.ScriptService.compile(ScriptService.java:354)
at
org.elasticsearch.script.ScriptService.executable(ScriptService.java:497)
at
org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:149)
... 8 more

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAJp2531fsDup%2B0%3DtSR48ugsVkphLG%2B1s4QbOjLP7GjrMncBbTA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Does not work (using JAVA API):

String script = "ctx._source.objectsList += newObject";

UpdateRequestBuilder prepareUpdate = client.prepareUpdate("indexName",
"typeName", "id");
prepareUpdate.setScriptLang("groovy");
prepareUpdate.setScript(script, ScriptType.INLINE);
prepareUpdate.addScriptParam("newObject", "{"status":"aasdsd"}");
prepareUpdate.get();

Is there a way to reproduce the working REST API behavior with the Java
API?

2014-12-16 15:17 GMT-02:00 Roger de Cordova Farias <
roger.farias@fontec.inf.br>:

Ok, I found out that I can send a JSON as a script parameter and just
append it to the nested objects list (with list += newObject or
list.add(newObject) ) using groovy and it works

But it is not working with the Java API, I can only get it to work using
the REST API.

When using Java the JSON is treated as a string, then I get the error:

object mapping [objectsList] trying to serialize a value with no field

associated with it, current value [{"field":"value"}]

I can reproduce the error in the REST API by wrapping the JSON parameter
with quotes:

Works (using REST API):

{

"script": "ctx._source.objectsList += newObject",
"params": {
"newObject": {"field": "value"}
},
"lang": "groovy"
}

Does not work (using REST API):

{

"script": "ctx._source.objectsList += newObject",
"params": {
"newObject": "{"field": "value"}"
},
"lang": "groovy"
}

Does not work (using JAVA API):

String script = "ctx._source.objectsList += newObject";

2014-12-16 13:04 GMT-02:00 Roger de Cordova Farias <
roger.farias@fontec.inf.br>:

Hello

I'm trying to update a document whose root object contains a list of
nested objects. I need to send an object of the nested type as a script
parameter to append to the list

How can I append the json (a string type) to the nested objects list of
the root object using Groovy? or should I use another script lang?

I tried using JsonSlurper http://groovy-lang.org/json.html in Groovy,
that converts between json and Groovy objects, but I always get:

Caused by:

org.elasticsearch.script.groovy.GroovyScriptCompilationException:
MultipleCompilationErrorsException[startup failed:
Script3.groovy: 2: unable to resolve class JsonSlurper
@ line 2, column 19.
def jsonSlurper = new JsonSlurper();
^
1 error
]
at
org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:117)
at
org.elasticsearch.script.ScriptService.getCompiledScript(ScriptService.java:368)
at org.elasticsearch.script.ScriptService.compile(ScriptService.java:354)
at
org.elasticsearch.script.ScriptService.executable(ScriptService.java:497)
at
org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:149)
... 8 more

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAJp2530t5o9jcpgGRsJo1zV%2BaSvD7Uk8QyTKha6VR-RoHQuqsQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

See this