setup:
  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_replicas: 0
          mappings:
            properties:
              double:
                type: double
              keyword:
                type: keyword

  - do:
      cluster.health:
        wait_for_status: green

  - do:
      index:
        index: test
        id: "1"
        body:
          double: 1.0
          keyword: "first"

  - do:
      index:
        index: test
        id: "2"
        body:
          double: 1.0
          keyword: "second"

  - do:
      index:
        index: test
        id: "3"
        body:
          double: 2.0
          keyword: "third"

  - do:
      indices.refresh: {}

---
"Scripted Metric Agg Total":

  - do:
      search:
        rest_total_hits_as_int: true
        body: {
          "size": 0,
          "aggs": {
            "total": {
              "scripted_metric": {
                "init_script": "state.transactions = []",
                "map_script": "state.transactions.add(doc['double'].value)",
                "combine_script": "double total = 0.0; for (t in state.transactions) { total += t } return total",
                "reduce_script": "double total = 0; for (a in states) { total += a } return total"
              }
            }
          }
        }

  - match: { hits.total: 3 }
  - match: { aggregations.total.value: 4.0 }

---
"Scripted Metric Agg Non String Map Key":

  - do:
      search:
        rest_total_hits_as_int: true
        body: {
          "size": 0,
          "aggs": {
            "total": {
              "scripted_metric": {
                "init_script": "state.transactions = [:]",
                "map_script": "state.transactions[doc['double'].value] = doc['double'].value",
                "combine_script": "return state.transactions",
                "reduce_script": "double sum = 0; for (transactions in states) { for (entry in transactions.entrySet()) { sum += entry.getKey() - entry.getValue() } } return sum"
              }
            }
          }
        }

  - match: { hits.total: 3 }
  - match: { aggregations.total.value: 0.0 }

---
"Scripted Metric Agg String Map Key":

  - do:
      search:
        rest_total_hits_as_int: true
        body: {
          "size": 0,
          "aggs": {
            "total": {
              "scripted_metric": {
                "init_script": "state.transactions = [:]",
                "map_script": "state.transactions[doc['keyword'].value] = doc['double'].value",
                "combine_script": "return state.transactions",
                "reduce_script": "double sum = 0; for (transactions in states) { for (entry in transactions.entrySet()) { sum += entry.getValue() } } return sum"
              }
            }
          }
        }

  - match: { hits.total: 3 }
  - match: { aggregations.total.value: 4.0 }

