# Calculate the time difference between consecutive documents

**URL:** https://discuss.elastic.co/t/calculate-the-time-difference-between-consecutive-documents/128282
**Category:** Elasticsearch
**Created:** [April 17, 2018, 4:40am UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-consecutive-documents/128282 "2018-04-17T04:40:44Z")
**Posts on this page:** 1
**Showing post:** 14

<div class="post-metadata">

### Author: ![vinayakfutak](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vinayakfutak](https://discuss.elastic.co/u/vinayakfutak)
#### Post date: [April 25, 2018, 9:58am UTC](https://discuss.elastic.co/t/calculate-the-time-difference-between-consecutive-documents/128282/14 "2018-04-25T09:58:24Z")

</div>

HI @MariumHassan this is my script

```
input {
elasticsearch {
 hosts => "http://192.168.55.213:9200"
 index => "test_index"
}
}
filter {
aggregate {
task_id => "%{logTimestamp}"
code => "
     map['logTimestamp'] = event.get('logTimestamp');
     map['timeDifference']= (Time.parse(event.get('logTimestamp')).to_f - 
     Time.parse(map['previousTime']).to_f).round(4);
     map['previousTime'] = event.get('logTimestamp');
 
"
}
}
output {
elasticsearch {
document_id => "%{logTimestamp}"
document_type => "test_index1"
index => "test_index1"
codec => "json"
hosts => ["192.168.55.213:9200"]
}
}

```

When I am trying to execute this script it's giving error as

Aggregate exception occurred {:error=\>#\<TypeError: can't dup NilClass\>, :code=\>"\n\t map['logTimestamp'] = event.get('logTimestamp');\n map['timeDifference']= (Time.parse(event.get('logTimestamp')).to\_f - Time.parse(map['previousTime']).to\_f).round(4);\n\tmap['previousTime'] = event.get('logTimestamp');\n\t \n ", :map=\>{"logTimestamp"=\>"2018-02-15T08:40:10Z"}, :event\_data=\>{"logTimestamp"=\>"2018-02-15T08:40:10Z", "@timestamp"=\>2018-04-25T09:47:38.312Z, "@version"=\>"1"}}

Can U suggest what changes I have to done to run script successfully?

---

_[View the full topic](https://discuss.elastic.co/t/calculate-the-time-difference-between-consecutive-documents/128282)._
