-1

my groovy script is something like this 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 am trying to update the value of a field in my elasticsearch index like

._source.doc[''+Fieldvalue+''].value=prod

where AVG_PRICE_PER_UNIT is a field in my index.

But when i am exceuting it from java i get

  "reason" : {
            "type" : "script_exception",
            "reason" : "failed to run file script [fieldScript] using lang [groovy]",
            "caused_by" : {
              "type" : "illegal_argument_exception",
              "reason" : "argument type mismatch"
            }

is there any 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 ]; at org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:198) at org.elasticsearch.script.ScriptService$ScriptChangesListener.onFileInit(ScriptService.java:549) at org.elasticsearch.script.ScriptService$ScriptChangesListener.onFileChanged(ScriptService.java:580) at org.elasticsearch.watcher.FileWatcher$FileObserver.onFileChanged(FileWatcher.java:279) at org.elasticsearch.watcher.FileWatcher$FileObserver.checkAndNotify(FileWatcher.java:131) at org.elasticsearch.watcher.FileWatcher$FileObserver.updateChildren(FileWatcher.java:215) at org.elasticsearch.watcher.FileWatcher$FileObserver.checkAndNotify(FileWatcher.java:117) at org.elasticsearch.watcher.FileWatcher.doCheckAndNotify(FileWatcher.java:70) at org.elasticsearch.watcher.AbstractResourceWatcher.checkAndNotify(AbstractResourceWatcher.java:44) at org.elasticsearch.watcher.ResourceWatcherService$ResourceMonitor.run(ResourceWatcherService.java:187) at org.elasticsearch.threadpool.ThreadPool$LoggingRunnable.run(ThreadPool.java:640) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.runAndReset(Unknown Source) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 895843a31627edca5a53c198e26b4f4b13aa65c5: 26: unexpected token: . @ line 26, column 7.

Val
  • 207,596
  • 13
  • 358
  • 360
Dhirish
  • 31
  • 1
  • 1
  • 9

1 Answers1

0

You have a typo in your script, change it to 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
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks @Val,but this was the previous case for which i was getting 'argument mismatch exception'. Forget about this , how do you change the value of a field using a script? can you please tell me? – Dhirish Sep 23 '16 at 09:55
  • What do you mean by "previous case"? This should answer what you have in your question. If you have a different question, feel free to ask another one, but let's not mix two different things together. – Val Sep 23 '16 at 11:20
  • i changed the code ,but still i am getting illegal argument exception – Dhirish Sep 25 '16 at 11:31
  • What error do you get now after changing the script? Update your question with the ES log you're receiving. – Val Sep 26 '16 at 04:43
  • I just get "illegal_argument_exception" , the log seems to be empty, Val, Can you suggest me on how to update the field? – Dhirish Sep 26 '16 at 05:33
  • If you get an illegal_argument_exception your ES logs contains more info. Make sure you check the right log. Try again, I have edited my answer. – Val Sep 26 '16 at 08:56
  • Why have you [created a new question](http://stackoverflow.com/questions/39700449/inline-script-error) instead of continuing the discussion here? – Val Sep 26 '16 at 12:13
  • Sorry @Val , i thought you wanted the other case that is why, by the way i am using the inline scripting now and i have raised a question on it ,can you help me?? – Dhirish Sep 26 '16 at 12:24
  • What about this question? It is no longer current? – Val Sep 26 '16 at 12:24
  • Yes,Shall i remove it? – Dhirish Sep 26 '16 at 12:29
  • It has no big value if you never validated that the answer worked for you. – Val Sep 26 '16 at 12:30
  • It didnt work for me thats why i moved on with update with inline scripting – Dhirish Sep 26 '16 at 12:32
  • I did update my answer, though, after you first comments. The above should work. – Val Sep 26 '16 at 12:33