title: Scripted Metric Aggregation fails on unexpected = character
body: <p>I'm trying to have some object containing all time deltas between two logs with the same recordId.</p>
  <p>In order to do so, I'm executing the following query (Scripted Metric Aggregation) on elastic search, where the logs are, using fiddler:</p>
  <pre><code>{
      &quot;query&quot;: {
          &quot;exists&quot;: {
              &quot;field&quot;: &quot;recordId&quot;
          }
      },
      &quot;aggs&quot;: {
          &quot;deltas&quot;: {
              &quot;scripted_metric&quot;: {
                  &quot;init_script&quot;: {
                      &quot;source&quot;: &quot;state.deltas = {};&quot;,
                      &quot;lang&quot;: &quot;expression&quot;
                  },
                  &quot;map_script&quot;: {
                      &quot;source&quot;: &quot; if (!(doc['topic'].value in state.deltas)) { state.deltas[doc['topic'].value] = {} } state.deltas[doc['topic'].value][doc['recordId'].value] = !(doc['recordId'].value in state.deltas[doc['topic'].value]) ? doc['@timestamp'].date.millisOfDay : Math.abs(state.deltas[doc['topic'].value][doc['recordId'].value] - doc['@timestamp'].date.millisOfDay) &quot;,
                      &quot;lang&quot;: &quot;expression&quot;
                  },
                  &quot;reduce_script&quot;: {
                      &quot;source&quot;: &quot; res = {}; for (s in states) { for(topic in Object.keys(s.deltas)) { if (!(topic in res)) { res[topic] = {} } for(recordId in Object.keys(s.deltas[topic])) { res[topic][recordId] = !(recordId in res[topic]) ? s.deltas[topic][recordId] : Math.abs(res[topic][recordId] - s.deltas[topic][recordId]) } } } return res;&quot;,
                      &quot;lang&quot;: &quot;expression&quot;
                  }
              }
          }
      }
  }
  </code></pre>
  <p>But it fails because of unexpected '=' character...
  Tried other ways but it always throws the same error.</p>
  <p>What am I missing?</p>
  <pre><code>{
    &quot;error&quot;: {
      &quot;root_cause&quot;: [
        {
          &quot;type&quot;: &quot;lexer_no_viable_alt_exception&quot;,
          &quot;reason&quot;: &quot;lexer_no_viable_alt_exception: null&quot;
        }
      ],
      &quot;type&quot;: &quot;search_phase_execution_exception&quot;,
      &quot;reason&quot;: &quot;all shards failed&quot;,
      &quot;phase&quot;: &quot;query&quot;,
      &quot;grouped&quot;: true,
      &quot;failed_shards&quot;: [
        {
          &quot;shard&quot;: 0,
          &quot;index&quot;: &quot;il0:il1-sys-logs-2020.10.07&quot;,
          &quot;node&quot;: &quot;5bmIYI_iSVCtx41qEEbASg&quot;,
          &quot;reason&quot;: {
            &quot;type&quot;: &quot;script_exception&quot;,
            &quot;reason&quot;: &quot;compile error&quot;,
            &quot;script_stack&quot;: [
              &quot;state.deltas = {};&quot;,
              &quot;             ^---- HERE&quot;
            ],
            &quot;script&quot;: &quot;state.deltas = {};&quot;,
            &quot;lang&quot;: &quot;expression&quot;,
            &quot;caused_by&quot;: {
              &quot;type&quot;: &quot;parse_exception&quot;,
              &quot;reason&quot;: &quot;parse_exception: unexpected character '= ' on line (1) position (13)&quot;,
              &quot;caused_by&quot;: {
                &quot;type&quot;: &quot;lexer_no_viable_alt_exception&quot;,
                &quot;reason&quot;: &quot;lexer_no_viable_alt_exception: null&quot;
              }
            }
          }
        }
      ],
      &quot;caused_by&quot;: {
        &quot;type&quot;: &quot;lexer_no_viable_alt_exception&quot;,
        &quot;reason&quot;: &quot;lexer_no_viable_alt_exception: null&quot;
      }
    },
    &quot;status&quot;: 500
  }
  </code></pre>