title: How to change cast of elasticsearch field in scripted metric from Long to Double?
body: <p>In my aggregators for an Elasticsearch query, I am trying to use a field that happens to be cast as type long, called <code>my_field</code> for this example:</p>
  
  <pre><code>"test-metric": {
      "scripted_metric": {
          "init_script": "_agg['test'] = []",
          "map_script": "_agg['test'].add(doc.['my_field'].value));
      }
  }
  </code></pre>
  
  <p>In some instances, it so happens that 'my_field' actually contains a double value, such as <code>1.5</code>. When outputting the actual hits, I can see that <code>1.5</code> is being output.</p>
  
  <p>My expectation would be that <code>_agg['test']</code> will contain that same value of <code>1.5</code>. However, it is immediately being cast as type <code>org.elasticsearch.index.fielddata.ScriptDocValues$Longs</code> and all calculations thereafter are wrong.</p>
  
  <p>I have tried modifying the schema and that appears to "fix" this, however in my current setup, it is not a viable solution to modify the schema for every instance where this comes up.</p>
  
  <p>Instead, I am looking for a solution that will give me consistent values in the scripted metrics as what I am getting from the matched hits.</p>
  
  <p>I have tried casting like this:</p>
  
  <pre><code>(org.elasticsearch.index.fielddata.ScriptDocValues$Doubles) doc.get('averageSessionsPerWeek')
  </code></pre>
  
  <p>But, I get this error:</p>
  
  <pre><code>GroovyScriptExecutionException[GroovyCastException[Cannot cast object '[2]' with class 'org.elasticsearch.index.fielddata.ScriptDocValues$Longs' to class 'org.elasticsearch.index.fielddata.ScriptDocValues$Doubles' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.elasticsearch.index.fielddata.ScriptDocValues$Doubles(java.lang.Long)]];
  </code></pre>
  
  <p>It seems like the cast is applied at the time that the <code>doc</code> object is created.</p>
  
  <p>Is this even possible?</p>
  
  <p>Thanks!</p>