# Calculating the avg time between two time stamps

**URL:** https://discuss.elastic.co/t/calculating-the-avg-time-between-two-time-stamps/18833
**Category:** Elasticsearch
**Created:** [July 23, 2014, 4:13pm UTC](https://discuss.elastic.co/t/calculating-the-avg-time-between-two-time-stamps/18833 "2014-07-23T16:13:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![javadevmtl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javadevmtl/32/45613_2.png) [@javadevmtl](https://discuss.elastic.co/u/javadevmtl)
#### Post date: [July 23, 2014, 4:13pm UTC](https://discuss.elastic.co/t/calculating-the-avg-time-between-two-time-stamps/18833/1 "2014-07-23T16:13:09Z")

</div>

Using ES 1.2.2

The below aggregation will give me my average requests per second and the  
average response times for each second.  
Is this the only way to do it or is there better way? Since scripting is a  
bit slow?

"aggs": {  
"tps": {  
"date\_histogram": {  
"field": "stampStart",  
"interval": "second",  
"order": {  
"\_key": "asc"  
}  
},  
"aggs": {  
"avg\_resp": {  
"avg": {  
"script": "doc.stampEnd.value - doc.stampStart.value"  
}  
}  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a618c94b-f79a-494a-9d12-0327cb9eff5c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a618c94b-f79a-494a-9d12-0327cb9eff5c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 24, 2014, 7:59am UTC](https://discuss.elastic.co/t/calculating-the-avg-time-between-two-time-stamps/18833/2 "2014-07-24T07:59:12Z")

</div>

The best option could be to compute that at index time.  
On a client level or by using the new transform feature: [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-transform.html#mapping-transform](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-transform.html#mapping-transform)

## HTH

David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 23 juil. 2014 à 18:13, John Smith [java.dev.mtl@gmail.com](mailto:java.dev.mtl@gmail.com) a écrit :

Using ES 1.2.2

The below aggregation will give me my average requests per second and the average response times for each second.  
Is this the only way to do it or is there better way? Since scripting is a bit slow?

## "aggs": { "tps": { "date\_histogram": { "field": "stampStart", "interval": "second", "order": { "\_key": "asc" } }, "aggs": { "avg\_resp": { "avg": { "script": "doc.stampEnd.value - doc.stampStart.value" } } } } }

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a618c94b-f79a-494a-9d12-0327cb9eff5c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a618c94b-f79a-494a-9d12-0327cb9eff5c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5E2032FE-9D5C-4F94-8613-B47C276BAA64%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/5E2032FE-9D5C-4F94-8613-B47C276BAA64%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![javadevmtl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javadevmtl/32/45613_2.png) [@javadevmtl](https://discuss.elastic.co/u/javadevmtl)
#### Post date: [July 24, 2014, 1:14pm UTC](https://discuss.elastic.co/t/calculating-the-avg-time-between-two-time-stamps/18833/3 "2014-07-24T13:14:09Z")

</div>

Cool! So for the new feature...

All I need is an empty field called "avgRespTime" and in my mapping define a transform for this field which compute the time difference between thw two stamp times?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/48baa73b-38a5-41dd-a192-4f0d6b470c50%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/48baa73b-38a5-41dd-a192-4f0d6b470c50%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [July 6, 2017, 1:13am UTC](https://discuss.elastic.co/t/calculating-the-avg-time-between-two-time-stamps/18833/4 "2017-07-06T01:13:29Z")

</div>


