Accessing an array in the _source from a javascript script


(Jettro Coenradie) #1

Hi I am using a javascript based script in the config/script folder using the javascript plugin. Than I use an update request with a script based update. There is something strange happening when using a field containing an array. The following code fails with the message:

{
   "error": "ElasticsearchIllegalArgumentException[failed to execute script]; nested: EcmaError[TypeError: Cannot find function push in object [/logo.png]. (Script23.js#38)]; ",
   "status": 400
}

This is the script that gets executed:

var logger = org.elasticsearch.common.logging.ESLoggerFactory.getLogger('jsplugin');
logger.info(ctx._source.numPages);
var doc = ctx._source;

if (doc.numPages != null) {
    logger.info("EXISTING DOCUMENT");
    doc.numPages++;
} else {
    logger.info("NEW DOCUMENT");
    doc.numPages=1;
    doc.requests=[];
}

doc.requests.push(log.request);

I simplified the script, but the error is in the last line with the push. If it is a new document there is no error, but an existing document gives an error. The simplified request I do is below:

POST /logsbyip/unique/94.31.7.101/_update
{
  "script_file": "upsertlog",
  "lang": "js", 
  "scripted_upsert": true,
  "params": {
    "log": {
      "request": "/logo.png",
    }
  },
  "upsert": {}
}

There seems to be happening something with the _source document that is provided to me in the script. Does anyone have an idea what I should do to add an item to the array?


(system) #2