Unable to create very simple runtime mapping

Hi. I'm trying to define a mapping for a transform. But any attempt to read the field I want to examine results in "Bad Request: [script_exception] runtime error".

    {
        "target_id": {
            "type": "keyword",
            "script": {
                "source": "if (doc.containsKey('user_id') && doc['user_id'] != null) {emit('foo')}"
            }
        }
    }

The first part of the boolean expression run but the second part:

doc['user_id'] != null

Causes the error. Have also tried doc['user_id'].value != null. Basically I seem to be unable to read this field at all except check if the map contains it. I have nothing else to go other than "runtime error". Its mapped as a keyword.

The stdout log was

{
   "type":"response",
   "@timestamp":"2021-04-27T01:34:44+00:00",
   "tags":[
      
   ],
   "pid":6,
   "method":"post",
   "statusCode":400,
   "req":{
      "url":"/api/transform/es_search",
      "method":"post",
      "headers":{
         "host":"localhost:5601",
         "connection":"keep-alive",
         "content-length":"345",
         "sec-ch-ua":"\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"",
         "sec-ch-ua-mobile":"?0",
         "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
         "kbn-version":"7.12.0",
         "content-type":"application/json",
         "accept":"*/*",
         "origin":"https://localhost:5601",
         "sec-fetch-site":"same-origin",
         "sec-fetch-mode":"cors",
         "sec-fetch-dest":"empty",
         "referer":"https://localhost:5601/app/management/data/transform/create_transform/0cc2c930-a662-11eb-9bdb-a75937356f08",
         "accept-encoding":"gzip, deflate, br",
         "accept-language":"en-AU,en;q=0.9,en-US;q=0.8,en-GB;q=0.7,fr;q=0.6"
      },
      "remoteAddress":"127.0.0.1",
      "userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
      "referer":"https://localhost:5601/app/management/data/transform/create_transform/0cc2c930-a662-11eb-9bdb-a75937356f08"
   },
   "res":{
      "statusCode":400,
      "responseTime":2770,
      "contentLength":85
   },
   "message":"POST /api/transform/es_search 400 2770ms - 85.0B"
}

I needed to check the field first with if (doc['xxx').size() > 0)

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.