# Can we script common objects in painless scripts

**URL:** <https://discuss.elastic.co/t/can-we-script-common-objects-in-painless-scripts/129869>\
**Category:** Elasticsearch\
**Created:** [April 27, 2018, 6:04pm UTC](https://discuss.elastic.co/t/can-we-script-common-objects-in-painless-scripts/129869 "2018-04-27T18:04:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fullar\_Senjit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@Fullar\_Senjit](https://discuss.elastic.co/u/Fullar_Senjit)\
**Post date:** [April 27, 2018, 6:04pm UTC](https://discuss.elastic.co/t/can-we-script-common-objects-in-painless-scripts/129869/1 "2018-04-27T18:04:36Z")

</div>

Considering the following painless script.  
"dateadddays" :{  
"script" :{  
"lang": "painless",  
"source" : "int days = 5; long millis = 5 \* 24 \* 3600 \* 1000; long totalmillis = doc.born.value.millis + millis; SimpleDateFormat sdf = new SimpleDateFormat('dd-M-yyyy hh:mm:ss'); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(totalmillis); return sdf.format(cal.getTime());"  
}  
}

is there a way to store objects such as  
SimpleDateFormat sdf = new SimpleDateFormat('dd-M-yyyy hh:mm:ss');

And use it in the multiple scripts.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [April 27, 2018, 6:40pm UTC](https://discuss.elastic.co/t/can-we-script-common-objects-in-painless-scripts/129869/2 "2018-04-27T18:40:08Z")

</div>

Unfortunately there is no way to share anything across invocations (per document) in a scoring script.

Also, note that that your example can be done much more simply with the java time api. Here I used an existing ISO format, but you could also use `DateTimeFormatter.ofPattern('dd-M-yyyy hh:mm:ss')` to get your exact format if it is important.

```auto

Instant time = Instant.ofEpochMill(doc.born.value.millis).plus(Duration.ofDays(5));
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(time);

```

---

<div class="post-metadata">

**Author:** ![Fullar\_Senjit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@Fullar\_Senjit](https://discuss.elastic.co/u/Fullar_Senjit)\
**Post date:** [April 27, 2018, 6:46pm UTC](https://discuss.elastic.co/t/can-we-script-common-objects-in-painless-scripts/129869/3 "2018-04-27T18:46:43Z")

</div>

thanks.  
Can we share using stored scripts ?

storing the following script  
SimpleDateFormat sdf = new SimpleDateFormat('dd-M-yyyy hh:mm:ss');

and reading as params ?  
i am getting the following exception  
"lang": "painless",  
"caused\_by": {  
"type": "class\_cast\_exception",  
"reason": "java.util.HashMap cannot be cast to java.text.SimpleDateFormat"  
}

can i specify the params type ?

---

<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:** [May 25, 2018, 6:48pm UTC](https://discuss.elastic.co/t/can-we-script-common-objects-in-painless-scripts/129869/4 "2018-05-25T18:48:47Z")

</div>

Closed on May 25, 2018, 6:48pm UTC

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