Doc Variable not defined in Painless Script


(Flavio Tom) #1

Using script to generate data and using a doc variable, it generates the error "doc [column] is not defined".

This error occurs in ElasticSearch 6.2.4 (production environment), when I use 6.2.3 in Docker (developer environment) this error does not happen.

I checked the index mapping and the column is in it.

How i can solve the problem?

Edit
Checking the local script against the remote, I noticed that the remote removed the quotation marks between the doc [].

Could that be the problem?

curl 'http://localhost:9200/_scripts/deadline'
{"_id":"deadline","found":true,"script":{"lang":"painless","source":"if ( (doc[deadline].empty) && (doc[deadline_reply_counter].empty) ) { /*NÃO POSSUI*/ return 2; }"}}

This is the cURL to create the script:

curl -X POST "localhost:9200/_scripts/deadline" -H 'Content-Type: application/json' -d'
{
  "script": {
    "lang": "painless",
    "source": "if ( (doc['deadline'].empty) && (doc['deadline_reply_counter'].empty) ) { return 2; } "
  }
}
'

This is the query:

{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "censored_column":1234
                    }
                }
            ]
        }
    },
    "_source":[

    ],
    "script_fields":{
        "deadline_ok":{
            "script":{
                "id":"deadline",
                "params":{
                    "current_timestamp":1526476514389
                }
            }
        },
        "start_deadline_ok":{
            "script":{
                "id":"start_deadline",
                "params":{
                    "current_timestamp":1526476514389
                }
            }
        }
    },
    "sort":{
        "censored_column_2":{
            "order":"asc"
        }
    }
}

This is the error:
"lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"Variable [deadline] is not defined."}}}]}


(Flavio Tom) #2

Even executing the cURL through macOS and Linux Terminal the error happened.

This error does not happen if you run the call through software such as Insomnia or Postman.

To solve this, i use a php script to execute the cURL.

Example below:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_PORT => "9200",
  CURLOPT_URL => "http://localhost:9200/_scripts/script_name",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{ \"script\": { \"lang\": \"painless\", \"source\": \"<painless_script_here>\" } }",
  CURLOPT_HTTPHEADER => array(
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

(system) #3

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