java - Groovy Scripting with elasticsearch -
my groovy script code:
def value=dynamicvalue def nvalue=newvalue def field=fieldvalue def prod newvalue=newvalue.tointeger() prod=doc[''+dynamicvalue+''].value*newvalue if(._source.doc[''+fieldvalue+''].value != null) { ._source.doc[''+fieldvalue+''].value=prod }
i trying update value of field in elasticsearch index like
._source.doc[''+fieldvalue+''].value=prod
where avg_price_per_unit field in index.
but when exceuting java
"reason" : { "type" : "script_exception", "reason" : "failed run file script [fieldscript] using lang [groovy]", "caused_by" : { "type" : "illegal_argument_exception", "reason" : "argument type mismatch" } there solution??
my java code:
map<string, object> params = immutablemap.of("dynamicvalue",aggregatevalue_first, "newvalue", aggregatevalue_second, "fieldvalue",hash); try { searchresponse se = client.preparesearch(indexname) .addscriptfield("checkvalue", new script("fieldscript", scripttype.file, "groovy", params)) .execute().actionget(); system.out.println(se.tostring()); } catch(runtimeexception e) { e.printstacktrace(); }
my log shows
if (._source.doc[''+fieldvalue+''] != null) {_source.doc[''+fieldvalue+''].value=prod} ^
1 error ]; @ org.elasticsearch.script.groovy.groovyscriptengineservice.compile(groovyscriptengineservice.java:198) @ org.elasticsearch.script.scriptservice$scriptchangeslistener.onfileinit(scriptservice.java:549) @ org.elasticsearch.script.scriptservice$scriptchangeslistener.onfilechanged(scriptservice.java:580) @ org.elasticsearch.watcher.filewatcher$fileobserver.onfilechanged(filewatcher.java:279) @ org.elasticsearch.watcher.filewatcher$fileobserver.checkandnotify(filewatcher.java:131) @ org.elasticsearch.watcher.filewatcher$fileobserver.updatechildren(filewatcher.java:215) @ org.elasticsearch.watcher.filewatcher$fileobserver.checkandnotify(filewatcher.java:117) @ org.elasticsearch.watcher.filewatcher.docheckandnotify(filewatcher.java:70) @ org.elasticsearch.watcher.abstractresourcewatcher.checkandnotify(abstractresourcewatcher.java:44) @ org.elasticsearch.watcher.resourcewatcherservice$resourcemonitor.run(resourcewatcherservice.java:187) @ org.elasticsearch.threadpool.threadpool$loggingrunnable.run(threadpool.java:640) @ java.util.concurrent.executors$runnableadapter.call(unknown source) @ java.util.concurrent.futuretask.runandreset(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$301(unknown source) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(unknown source) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) caused by: org.codehaus.groovy.control.multiplecompilationerrorsexception: startup failed: 895843a31627edca5a53c198e26b4f4b13aa65c5: 26: unexpected token: . @ line 26, column 7.
you have typo in script, change this:
def value=dynamicvalue def nvalue=newvalue def field=fieldvalue def prod newvalue=newvalue.tointeger() prod=ctx._source[dynamicvalue].value * newvalue if(ctx._source[fieldvalue].value != null) { ctx._source[fieldvalue].value=prod }
Comments
Post a Comment