# Range aggregation Scripts

**URL:** <https://discuss.elastic.co/t/range-aggregation-scripts/297147>\
**Category:** Elasticsearch\
**Created:** [February 14, 2022, 1:41pm UTC](https://discuss.elastic.co/t/range-aggregation-scripts/297147 "2022-02-14T13:41:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)\
**Post date:** [February 14, 2022, 1:41pm UTC](https://discuss.elastic.co/t/range-aggregation-scripts/297147/1 "2022-02-14T13:41:25Z")

</div>

Hi All,  
Below is my problem statement.  
I want to calculate aging stage based on below requirement.  
If Stage='INT' then today() - 'INT1 Date' else  
If Stage='MCH' then today() - 'Applied Date (WEB/MCH)' else  
If Stage='ASG' then today() - 'Assigned Date (ASG)' else  
If Stage='OFR' then today() - 'Offer Date' else  
If Stage='SUB' then today() - 'Submitted Date(SUB)' else  
else today() - Placement Date

After above calculation it should give combine result in below groups  
If Ageing in Stage \>= 0 & \<=2 then "0-2 Days" else  
If Ageing in Stage \>2 & \<15 then "3-14 Days" else  
If Ageing in Stage \>= 15 & \<=30 then "15-30 Days" else  
If Ageing in Stage \>30 & \<=60 then "30-60 Days" else

I am trying using range aggregation. but how to achieve it with 1 query.

---

<div class="post-metadata">

**Author:** ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)\
**Post date:** [February 14, 2022, 5:25pm UTC](https://discuss.elastic.co/t/range-aggregation-scripts/297147/2 "2022-02-14T17:25:31Z")

</div>

> Blockquote

GET client\_supply\_ta\_alias/\_search  
{  
"size": 0,  
"query": {  
"match\_all": {}  
},

```
  "aggs": {
    "avg time": {
      "range": {
        "script": {
          "lang": "painless",
          "source": """
     long toTime = 0; 
      if(doc['candidate_stage.keyword'] == "INT"){
       toTime=(System.currentTimeMillis()-doc['int1_date_epoch'].value.toInstant().toEpochMilli())/86400000;
      }
      else if(doc['candidate_stage.keyword'] == "WEB"){
       toTime=(System.currentTimeMillis()-doc['applied_date_epoch'].value.toInstant().toEpochMilli())/86400000;
      }
       else if(doc['candidate_stage.keyword'] == "MCH"){
       toTime=(System.currentTimeMillis()-doc['applied_date_epoch'].value.toInstant().toEpochMilli())/86400000;
      }
      else if(doc['candidate_stage.keyword'] == "ASG"){
       toTime=(System.currentTimeMillis()-doc['assigned_date_epoch'].value.toInstant().toEpochMilli())/86400000;
      }
       else if(doc['candidate_stage.keyword'] == "OFR"){
       toTime=((System.currentTimeMillis()-doc['offer_date_epoch'].value.toInstant().toEpochMilli())/86400000);
      }
       else if(doc['candidate_stage.keyword'] == "SUB"){
       toTime=(System.currentTimeMillis()-doc['submitted_date_epoch'].value.toInstant().toEpochMilli())/86400000;
      }
        else if(doc['candidate_stage.keyword'] == "SCR"){
       toTime=(System.currentTimeMillis()-doc['scr_datetime_epoc'].value.toInstant().toEpochMilli())/86400000;
      }
         else if(doc['candidate_stage.keyword'] == "PES"){
       toTime=(System.currentTimeMillis()-doc['offer_date_epoch'].value.toInstant().toEpochMilli())/86400000;
      }
      else
      {
        toTime=(System.currentTimeMillis()-doc['placement_date_epoch'].value.toInstant().toEpochMilli())/86400000; 
      }
       return (toTime)
      """
      
        },
         "value_type":"double",
    "ranges": [
      
       {"key":"50","from":0, "to" : 50},

```

{"key":"100","from":51, "to" : 100},

```
       {"key":"200","from":101, "to" : 200},

```

{"key":"300","from":201, "to" : 300},

```
       {"key":"400","from":301, "to" : 400},

```

{"key":"500","from":401, "to" : 500},

```
       {"key":"600","from":501, "to" : 600},

```

{"key":"More","from":601}  
]

```
  }
}

```

}  
}

> Blockquote

This is the query which i tried.  
But i get below response.  
{  
"key" : "50",  
"from" : 0.0,  
"to" : 50.0,  
"doc\_count" : 0  
},  
{  
"key" : "100",  
"from" : 51.0,  
"to" : 100.0,  
"doc\_count" : 0  
},  
{  
"key" : "200",  
"from" : 101.0,  
"to" : 200.0,  
"doc\_count" : 0  
},  
{  
"key" : "300",  
"from" : 201.0,  
"to" : 300.0,  
"doc\_count" : 0  
},  
{  
"key" : "400",  
"from" : 301.0,  
"to" : 400.0,  
"doc\_count" : 0  
},  
{  
"key" : "500",  
"from" : 401.0,  
"to" : 500.0,  
"doc\_count" : 0  
},  
{  
"key" : "600",  
"from" : 501.0,  
"to" : 600.0,  
"doc\_count" : 0  
},  
{  
"key" : "More",  
"from" : 601.0,  
"doc\_count" : 499476  
}  
Which is not correct.

---

<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:** [March 14, 2022, 5:26pm UTC](https://discuss.elastic.co/t/range-aggregation-scripts/297147/3 "2022-03-14T17:26:02Z")

</div>

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