# 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:** 3\
**Page:** 1

<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 13, 2018, 9:00pm UTC](https://discuss.elastic.co/t/unexpected-types-in-painless-update-script/156547/1 "2018-11-13T21:00:02Z")

</div>

I'm using a Painless update-script in conjunction with some entity-centric aggregation scripts as [recommended here](https://discuss.elastic.co/t/distinct-count-with-filter/155795/2).

The code isn't working the way I expect it to. In this example the @timestamp is a Date, with the following mapping:

```auto
  "@timestamp": {
    "type": "date"
  }

```

When I launch my script for the first time to update documents, it run's perfectly and creates my aggregated documents.

If I launch it a second time to grab new events and update the documents with new events, I get an error because one of the `Date` fields is now recognized as a `String`.

Here is a simplified version of the code with some comments inline about where I see issues:

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

// Consolidate latest batch of events
for (event in params.events) {
   // @timestamp is a Date, but event['@timestamp'].date gives this error:
   // "Unable to find dynamic field [date] for class [java.lang.String]."
  def timestamp = Date.from( Instant.parse( event['@timestamp'] ));

  // Skip duplicate events.
  // This code run's fine for the first iteration, the second gives this error:
  // "Unable to find dynamic method [getTime] with [0] arguments for class [java.lang.String]."
  if (docSrc.activity != null && timestamp.getTime() < docSrc.activity.getTime()) {
    docSrc.duplicateEventsSkipped ++;
    continue;
  }
}                                                                                                                                                

```

---

<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.

```

---

<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 12, 2018, 4:37pm UTC](https://discuss.elastic.co/t/unexpected-types-in-painless-update-script/156547/3 "2018-12-12T16:37:35Z")

</div>

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