anchor.diff:  [[painless-time-features]]
   ==== Getting time features as scripted fields
   
  -This snippet shows how to extract time based features by using Painless. The 
  -snippet uses an index where `@timestamp` is defined as a `date` type field.
  +This snippet shows how to extract time based features by using Painless in a 
  +{transform}. The snippet uses an index where `@timestamp` is defined as a `date` 
  +type field.
   
   [source,js]
   --------------------------------------------------
  -"script_fields": {
  -    "hour_of_day": { <1>
  -      "script": {
  -        "lang": "painless",
  -        "source": """
  -          ZonedDateTime date =  doc['@timestamp'].value; <2>
  -          return date.getHour(); <3>
  -        """
  -      }
  -    },
  -    "month_of_year": { <4>
  -      "script": {
  -        "lang": "painless",
  -        "source": """
  -          ZonedDateTime date =  doc['@timestamp'].value; <5>
  -          return date.getMonthValue(); <6>
  -        """
  -      }
  -    }
  -  }
  +"aggregations": {
  +  "script_fields": {
  +      "hour_of_day": { <1>
  +        "script": {
  +          "lang": "painless",
  +          "source": """
  +            ZonedDateTime date =  doc['@timestamp'].value; <2>
  +            return date.getHour(); <3>
  +          """
  +        }
  +      },
  +      "month_of_year": { <4>
  +        "script": {
  +          "lang": "painless",
  +          "source": """
  +            ZonedDateTime date =  doc['@timestamp'].value; <5>
  +            return date.getMonthValue(); <6>
  +          """
  +        }
  +      }
  +    },
  +  ...
  +}
   --------------------------------------------------
   // NOTCONSOLE
   
  
positive.code: `max` are variables mapped to `time_frame.gte.value` and `time_frame.lte.value`.
  <3> Finally, the script substracts the start date of the session from the end 
  date which results in the duration of the session.
  
negative.code: 
  [source,js]
  --------------------------------------------------
  "aggregations": {
    "latest_value": {
      "scripted_metric": {
        "init_script": "state.timestamp_latest = 0L; state.last_value = ''",
        "map_script": """
          def current_date = doc['date'].getValue().toInstant().toEpochMilli(); 
          if (current_date > state.timestamp_latest) 
          {state.timestamp_latest = current_date;
          state.last_value = params['_source']['value'];}
        """,
        "combine_script": "return state",
        "reduce_script": """
          def last_value = '';
          def timestamp_latest = 0L; 
          for (s in states) {if (s.timestamp_latest > (timestamp_latest)) 
          {timestamp_latest = s.timestamp_latest; last_value = s.last_value;}} 
          return last_value
        """
      }
    }
  }
  --------------------------------------------------
  