Help making query

I have a document, pageTransition that has the following mapping.
{
"survale": {
"mappings": {
"pageTransition": {
"properties": {
"applicantCookie": {
"type": "string",
"index": "not_analyzed"
},
"clientId": {
"type": "string",
"index": "not_analyzed"
},
"cookie": {
"type": "string"
},
"created": {
"type": "date",
"format": "dateOptionalTime"
},
"fromPageId": {
"type": "string",
"index": "not_analyzed"
},
"fromUrl": {
"type": "string",
"index": "not_analyzed"
},
"jobCookie": {
"type": "string",
"index": "not_analyzed"
},
"retryCounter": {
"type": "long"
},
"toPageId": {
"type": "string",
"index": "not_analyzed"
},
"toUrl": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}

the records look like the following
"hits": [
{
"_index": "survale",
"_type": "pageTransition",
"_id": "AVMuRLqpRm51S9az_qKD",
"_score": 1,
"_source": {
"clientId": 0,
"fromUrl": "http://ats.survale.com/Apply/AdditionalInfo.aspx",
"toUrl": "http://ats.survale.com/Apply/ConfirmInfo.aspx",
"fromPageId": "183",
"toPageId": "184",
"created": "2016-02-29T18:22:49.509Z"
}
},
{
"_index": "survale",
"_type": "pageTransition",
"_id": "AVMuRKlGRm51S9az_qKC",
"_score": 1,
"_source": {
"clientId": 0,
"fromUrl": "http://ats.survale.com/Jobs/Default.aspx",
"toUrl": "http://ats.survale.com/Apply/BasicInfo.aspx",
"fromPageId": "177",
"toPageId": "180",
"created": "2016-02-29T18:22:45.047Z"
}
},
{
"_index": "survale",
"_type": "pageTransition",
"_id": "AVMwO4dzRm51S9az_qKH",
"_score": 1,
"_source": {
"clientId": 0,
"fromUrl": "http://ats.survale.com/Login.aspx",
"toUrl": "http://ats.survale.com/jobs/",
"fromPageId": "178",
"toPageId": "185",
"created": "2016-03-01T03:32:01.001Z"
}
},

say user reached from page 176 to page 177 at time t1. And from page 177 to page 180 at time t2. I am interested in the time t2-t1, which is the time user spent on page 177.
there may be many records for page 177. I want to get all the times and then compute the average..
Can someone advice me on how to approach?

Regards
Vadiraj

I think you would need to use a script to calculate this difference, then run the aggregation with that.