0

I am trying to use a filescript for an update operation for elastic search. The filescript I am using is a javascript and I am not able to understand why its compilation fails. Following is the file:

update-htext.js:

import  org.elasticsearch.common.logging.*; 
var level = org.apache.log4j.Level.INFO;
ESLogger logger=ESLoggerFactory.getLogger('myscript');
if(ctx._source.hTexts == null){
    ctx._source.hTexts = [sourcehtext];
    return;
}else{
    var i = 0;
    for(it in ctx._source.hTexts){
        if(it.htext.contains(sourcehtext.htext)){
            logger.logger.log(level,"Already Contains "+it.htext);
            return;
        }
        if(sourcehtext.htext.contains(it.htext)){
            logger.logger.log(level,"Updating "+it.htext);
            break;
        }
        i++;
    }
    ctx._source.hTexts[i] = sourcehtext;
}

And I am invoking it as follows:

{
    "script" :{  
        "script_id" : "update-htext",
        "params" : {
            "sourcehtext":{
                "aid":1000,
                "htext" : "and"
            }
        },
        "lang" : "javascript"
    }

}

I get the following error when elasticsearch starts and tries to index this js file:

org.mozilla.javascript.EvaluatorException: missing ; before statement (Script2.j
s#1)
        at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultError
Reporter.java:77)
        at org.mozilla.javascript.DefaultErrorReporter.error(DefaultErrorReporte
r.java:64)
labyrinth
  • 1,104
  • 3
  • 11
  • 32

1 Answers1

2
import  org.elasticsearch.common.logging.*; 

...is a Java import statement, not JavaScript code. Until ES6, JavaScript didn't have imports; as of ES6, it does, they look like this:

import myModule from "my-module.js";
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Ohh I copied that from the groovy code without editing, so the issue I guess. Any other way to log in javascript? – labyrinth Sep 14 '15 at 08:01
  • @labyrinth: Depends on the environment, but most environments support `console.log`. Apparently ElastiSearch [has various other options](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/logging.html), such as Bunyan. – T.J. Crowder Sep 14 '15 at 08:02