# Elasticsearch bucket\_script to subtract two timestamps

**URL:** <https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309>\
**Category:** Elasticsearch\
**Created:** [March 23, 2018, 6:22am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309 "2018-03-23T06:22:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siddaram\_Jamagav](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@Siddaram\_Jamagav](https://discuss.elastic.co/u/Siddaram_Jamagav)\
**Post date:** [March 23, 2018, 6:22am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309/1 "2018-03-23T06:22:45Z")

</div>

Unable to subtract two time stamps using bucket\_script aggregation:

> Below is my query:

POST xxxxxxxxxxxxxxxxxxxxxxxxxx/\_search?size=0  
{  
"query": {  
"bool": {  
"must": [  
{  
"match\_phrase": {  
"projectId.keyword": "xxxxxxxxxxxxxxxxxxxxxx"  
}  
}  
],  
"filter": {  
"range": {  
"page.time": {  
"gte": "now-w",  
"lte": "now"  
}  
}  
}  
}  
},  
"aggs": {  
"sessionLengthData": {  
"terms": {  
"field": "sId.keyword",  
"size": 10  
},  
"aggs": {  
"firstUrl":{  
"terms":{  
"field":"page.time",  
"size":1,  
"order":{  
"\_term":"asc"  
}  
}  
},  
"lastUrl":{  
"terms":{  
"field":"page.time",  
"size":1,  
"order":{  
"\_term":"desc"  
}  
}  
},  
"sessionLength": {  
"bucket\_script": {  
"buckets\_path": {  
"startTime": "firstUrl.\_bucket\_key",  
"endTime": "lastUrl.\_bucket\_count"  
},  
"script": "params.endTime - params.startTime"

```
                        }
                    }
        }
      }
    }
    }

```

In bucket\_script query i am taking time stamp values from upper aggregation query.

**Below are my sample records:**

```
"_source": {
          "projectId": " *************************",
          "sId": "6b7xisp971521692338655",
          "pId": "6b7xisp971521692338655.1",
          "startTime": 1521692338655,
          "page": {
            "sid": "6b7xisp971521692338655",
            "time": 1521700149947,
            "url": " ********************************",
            "pageMinUrl": " ************************",
            "host": " **************************************",
            "start": 0,
            "pageLoad": 1523,
            "network": 668,
            "dom": 669,
          }
}

 "_source": {
          "projectId": " *****************************",
          "sId": "6b7xisp971521692338655",
          "pId": "6b7xisp971521692338655.2",
          "startTime": 1521692338655,
          "page": {
            "sid": "6b7xisp971521692338655",
            "time": 1521700349782,
            "url": " ********************************",
            "pageMinUrl": " ******************************",
            "host": " ****************************************",
            "start": 0,
            "pageLoad": 1537,
            "network": 212,
            "dom": 1109,
            
          }
} 

```

Give me some suggestions how to use bucket\_script to subtract to time stamps values

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [March 23, 2018, 10:24am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309/2 "2018-03-23T10:24:57Z")

</div>

Do you need those `firstUrl` and `lastUrl` aggregations to be `terms` aggregations? Because if you change those to `min` and `max` aggregations the solution is simple:

```auto
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "page.time": {
            "gte": "now-w",
            "lte": "now"
          }
        }
      }
    }
  },
  "aggs": {
    "sessionLengthData": {
      "terms": {
        "field": "sId.keyword",
        "size": 10
      },
      "aggs": {
        "firstUrl": {
          "min": {
            "field": "page.time"
          }
        },
        "lastUrl": {
          "max": {
            "field": "page.time"
          }
        },
        "sessionLength": {
          "bucket_script": {
            "buckets_path": {
              "startTime": "firstUrl",
              "endTime": "lastUrl"
            },
            "script": "params.endTime - params.startTime"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Siddaram\_Jamagav](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@Siddaram\_Jamagav](https://discuss.elastic.co/u/Siddaram_Jamagav)\
**Post date:** [March 23, 2018, 10:28am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309/3 "2018-03-23T10:28:40Z")

</div>

@abdon, Thanks for the reply. I need firstUrl and lastUrl aggregation to be terms aggregations only.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [March 23, 2018, 10:31am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309/4 "2018-03-23T10:31:02Z")

</div>

Alright. Well, you can still retrieve those `terms` aggs in addition to the `min` and `max` metric aggregations:

```auto
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "page.time": {
            "gte": "now-w",
            "lte": "now"
          }
        }
      }
    }
  },
  "aggs": {
    "sessionLengthData": {
      "terms": {
        "field": "sId.keyword",
        "size": 10
      },
      "aggs": {
        "firstUrl": {
          "terms": {
            "field": "page.time",
            "size": 1,
            "order": {
              "_term": "asc"
            }
          }
        },
        "lastUrl": {
          "terms": {
            "field": "page.time",
            "size": 1,
            "order": {
              "_term": "desc"
            }
          }
        },
        "min": {
          "min": {
            "field": "page.time"
          }
        },
        "max": {
          "max": {
            "field": "page.time"
          }
        },
        "sessionLength": {
          "bucket_script": {
            "buckets_path": {
              "startTime": "min",
              "endTime": "max"
            },
            "script": "params.endTime - params.startTime"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Siddaram\_Jamagav](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@Siddaram\_Jamagav](https://discuss.elastic.co/u/Siddaram_Jamagav)\
**Post date:** [March 23, 2018, 10:38am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309/5 "2018-03-23T10:38:14Z")

</div>

@abdon, Thank you . It worked 😀

---

<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:** [April 20, 2018, 10:38am UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-to-subtract-two-timestamps/125309/6 "2018-04-20T10:38:16Z")

</div>

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