# Unexpected type's in Painless update script

**URL:** <https://discuss.elastic.co/t/unexpected-types-in-painless-update-script/156547>\
**Category:** Elasticsearch\
**Created:** [November 13, 2018, 9:00pm UTC](https://discuss.elastic.co/t/unexpected-types-in-painless-update-script/156547 "2018-11-13T21:00:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![winder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/winder/32/54090_2.png) [@winder](https://discuss.elastic.co/u/winder)\
**Post date:** [November 14, 2018, 4:37pm UTC](https://discuss.elastic.co/t/unexpected-types-in-painless-update-script/156547/2 "2018-11-14T16:37:25Z")

</div>

Found a similar question here: [Painless dates in update script](https://discuss.elastic.co/t/painless-dates-in-update-script/84849)

I've rewritten my script to cache any `Date` fields when they are initialized/updated and assume that they are `String` otherwise.

I'm really hoping there is an alternative to this, but in case this is the expected behavior here is one solution for anyone else who comes across this problem:

```auto
//Copy doc source to local variable with shorter name
def docSrc = ctx._source;

// Initialize if new document
if("create".equals(ctx.op)){
  docSrc.eventCount = 1;
  docSrc.duplicateEventsSkipped = 0;
}

// Cache date fields.
def activityInstant = null;
if (docSrc.activity != null) {
  activityInstant = Instant.parse(docSrc.activity);
}

def otherDateField = null;
if (docSrc.otherDateField != null) {
  otherDateField = Instant.parse(docSrc.activity);
}

// Consolidate latest batch of events
for (event in params.events) {
  def timestamp = Instant.parse( event['@timestamp'] );

  // Skip duplicate
  if (activityInstant != null && timestamp.isBefore(activityInstant)) {
    docSrc.duplicateEventsSkipped ++;
    continue;
  }

  docSrc.eventCount ++; 
  docSrc.activity = Date.from(timestamp);
  docSrc.otherDateField = Date.from(Instant.parse(event['otherDateField']))
}                                                                                                                                   

// Compute any additional data using the cached dates rather than the values updated in ctx._source.

```

---

_[View the full topic](https://discuss.elastic.co/t/unexpected-types-in-painless-update-script/156547)._
