# Doc Variable not defined in Painless Script

**URL:** <https://discuss.elastic.co/t/doc-variable-not-defined-in-painless-script/132136>\
**Category:** Elasticsearch\
**Created:** [May 16, 2018, 1:31pm UTC](https://discuss.elastic.co/t/doc-variable-not-defined-in-painless-script/132136 "2018-05-16T13:31:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [May 16, 2018, 1:31pm UTC](https://discuss.elastic.co/t/doc-variable-not-defined-in-painless-script/132136/1 "2018-05-16T13:31:40Z")

</div>

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."}}}]}`

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [May 16, 2018, 4:42pm UTC](https://discuss.elastic.co/t/doc-variable-not-defined-in-painless-script/132136/2 "2018-05-16T16:42:34Z")

</div>

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](https://insomnia.rest/) or [Postman](https://www.getpostman.com/).

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;
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 13, 2018, 4:42pm UTC](https://discuss.elastic.co/t/doc-variable-not-defined-in-painless-script/132136/3 "2018-06-13T16:42:34Z")

</div>

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