# Define a date variable with an initial value in scripted field

**URL:** <https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771>\
**Category:** Elasticsearch\
**Created:** [November 15, 2017, 2:44pm UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771 "2017-11-15T14:44:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anabella\_Cristaldi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anabella_cristaldi/32/23612_2.png) [@Anabella\_Cristaldi](https://discuss.elastic.co/u/Anabella_Cristaldi)\
**Post date:** [November 15, 2017, 2:44pm UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/1 "2017-11-15T14:44:46Z")

</div>

Hi,  
I'm working with an scripted field that computes a difference between two dates

```
 "total_duration": {
                      "max": {
                        "script": {"source": "(doc['ts_stop'].value - doc['ts_start'].value)/1000"}
                      }
                    }

```

But now instead of using the value of the field ts\_start I want to use a fixed value (for example 2017-11-11T00:28:00.000Z)  
How do I define a variable of type date holding that value in order to change the script file to something like this

`"script": {"source": "(doc['ts_stop'].value - new_var)/1000"}`

Thank you very much  
Regards  
Anna

---

<div class="post-metadata">

**Author:** ![costin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/costin/32/44950_2.png) [@costin](https://discuss.elastic.co/u/costin)\
**Post date:** [November 15, 2017, 4:14pm UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/2 "2017-11-15T16:14:05Z")

</div>

Why not use [externalized parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html):

```
   {"source": ""(doc['ts_stop'].value - new_var)/1000"",
    "params": {
      "new_var": 123
    }

```

?

---

<div class="post-metadata">

**Author:** ![Anabella\_Cristaldi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anabella_cristaldi/32/23612_2.png) [@Anabella\_Cristaldi](https://discuss.elastic.co/u/Anabella_Cristaldi)\
**Post date:** [November 15, 2017, 4:30pm UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/3 "2017-11-15T16:30:24Z")

</div>

Thank you @costin.  
It think that this approach should work for me.  
I have replaced my code with

`"script": {"source": "(doc['ts_stop'].value - new_var)/1000", "params": {"new_var": 1510358400000}}`

But I get

```
 "script": "(doc['ts_stop'].value - new_var)/1000",
          "lang": "painless",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Variable [new_var] is not defined."
          }

```

I'll try to figure out what is wrong.  
Regards  
Anna

---

<div class="post-metadata">

**Author:** ![costin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/costin/32/44950_2.png) [@costin](https://discuss.elastic.co/u/costin)\
**Post date:** [November 15, 2017, 4:34pm UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/4 "2017-11-15T16:34:39Z")

</div>

Try `(doc['ts_stop'].value - params.new_var)/1000`.

---

<div class="post-metadata">

**Author:** ![costin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/costin/32/44950_2.png) [@costin](https://discuss.elastic.co/u/costin)\
**Post date:** [November 15, 2017, 4:43pm UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/5 "2017-11-15T16:43:12Z")

</div>

By the way, the syntax is explained [here](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/modules-scripting-using.html)

---

<div class="post-metadata">

**Author:** ![Anabella\_Cristaldi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anabella_cristaldi/32/23612_2.png) [@Anabella\_Cristaldi](https://discuss.elastic.co/u/Anabella_Cristaldi)\
**Post date:** [November 16, 2017, 7:45am UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/6 "2017-11-16T07:45:27Z")

</div>

Thank you very much @costin  
Worked ok!

---

<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:** [December 14, 2017, 7:45am UTC](https://discuss.elastic.co/t/define-a-date-variable-with-an-initial-value-in-scripted-field/107771/7 "2017-12-14T07:45:27Z")

</div>

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