Range aggregation Scripts

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.

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.

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