---
setup:
  - do:
      indices.create:
        index: sensor
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            runtime:
              voltage_times_ten:
                type: long
                script:
                  source: |
                    for (double v : doc['voltage']) {
                      emit((long)(v * params.multiplier));
                    }
                  params:
                    multiplier: 10
              # Test fetching from _source
              voltage_times_ten_from_source:
                type: long
                script:
                  source: |
                    emit((long)(params._source.voltage * params.multiplier));
                  params:
                    multiplier: 10
              # Test fetching many values
              temperature_digits:
                type: long
                script:
                  source: |
                    for (long temperature : doc['temperature']) {
                      long t = temperature;
                      while (t != 0) {
                        emit(t % 10);
                        t /= 10;
                      }
                    }
              # Test now
              millis_ago:
                type: date
                script:
                  source: |
                    for (def dt : doc['timestamp']) {
                      emit(System.currentTimeMillis() - dt.toInstant().toEpochMilli());
                    }
            properties:
              timestamp:
                type: date
              temperature:
                type: long
              voltage:
                type: double
              node:
                type: keyword

  - do:
      bulk:
        index: sensor
        refresh: true
        body: |
          {"index":{}}
          {"timestamp": 1516729294000, "temperature": 200, "voltage": 5.2, "node": "a"}
          {"index":{}}
          {"timestamp": 1516642894000, "temperature": 201, "voltage": 5.8, "node": "b"}
          {"index":{}}
          {"timestamp": 1516556494000, "temperature": 202, "voltage": 5.1, "node": "a"}
          {"index":{}}
          {"timestamp": 1516470094000, "temperature": 198, "voltage": 5.6, "node": "b"}
          {"index":{}}
          {"timestamp": 1516383694000, "temperature": 200, "voltage": 4.2, "node": "c"}
          {"index":{}}
          {"timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c"}

---
"get mapping":
  - do:
      indices.get_mapping:
        index: sensor
  - match: {sensor.mappings.runtime.voltage_times_ten.type: long }
  - match:
      sensor.mappings.runtime.voltage_times_ten.script.source: |
        for (double v : doc['voltage']) {
          emit((long)(v * params.multiplier));
        }
  - match: {sensor.mappings.runtime.voltage_times_ten.script.params: {multiplier: 10} }
  - match: {sensor.mappings.runtime.voltage_times_ten.script.lang: painless }

---
"fetch fields":
  - do:
      search:
        index: sensor
        body:
          sort: timestamp
          fields:
            - voltage_times_ten
            - voltage_times_ten_from_source
            - temperature_digits
            - field: millis_ago
              format: epoch_millis
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.voltage_times_ten: [40] }
  - match: {hits.hits.0.fields.voltage_times_ten_from_source: [40] }
  - match: {hits.hits.0.fields.temperature_digits: [0, 2, 2] }
  - match: {hits.hits.0.fields.voltage_times_ten: [40] }
  - match: {hits.hits.1.fields.voltage_times_ten: [42] }
  - match: {hits.hits.2.fields.voltage_times_ten: [56] }
  - match: {hits.hits.3.fields.voltage_times_ten: [51] }
  - match: {hits.hits.4.fields.voltage_times_ten: [58] }
  - match: {hits.hits.5.fields.voltage_times_ten: [52] }
  # We can't check the value because it is constantly increasing. If `gt` worked on strings we could do it, but it doens't.
  - is_true: hits.hits.0.fields.millis_ago.0

---
"docvalue_fields":
  - do:
      search:
        index: sensor
        body:
          sort: timestamp
          docvalue_fields:
            - voltage_times_ten
            - voltage_times_ten_from_source
            - temperature_digits
            - field: millis_ago
              format: epoch_millis
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.voltage_times_ten: [40] }
  - match: {hits.hits.0.fields.voltage_times_ten_from_source: [40] }
  - match: {hits.hits.0.fields.temperature_digits: [0, 2, 2] }
  - match: {hits.hits.0.fields.voltage_times_ten: [40] }
  - match: {hits.hits.1.fields.voltage_times_ten: [42] }
  - match: {hits.hits.2.fields.voltage_times_ten: [56] }
  - match: {hits.hits.3.fields.voltage_times_ten: [51] }
  - match: {hits.hits.4.fields.voltage_times_ten: [58] }
  - match: {hits.hits.5.fields.voltage_times_ten: [52] }
  # We can't check the value because it is constantly increasing. If `gt` worked on strings we could do it, but it doens't.
  - is_true: hits.hits.0.fields.millis_ago.0

---
"terms agg":
  - do:
      search:
        index: sensor
        body:
          aggs:
            v10:
              terms:
                field: voltage_times_ten
  - match: {hits.total.value: 6}
  - match: {aggregations.v10.buckets.0.key: 40.0}
  - match: {aggregations.v10.buckets.0.doc_count: 1}
  - match: {aggregations.v10.buckets.1.key: 42.0}
  - match: {aggregations.v10.buckets.1.doc_count: 1}

---
"term query":
  - do:
      search:
        index: sensor
        body:
          query:
            term:
              voltage_times_ten: 58
  - match: {hits.total.value: 1}
  - match: {hits.hits.0._source.voltage: 5.8}

---
"explain term query":
  - do:
      search:
        index: sensor
        explain: true
        body:
          query:
            term:
              voltage_times_ten: 58
  - match: {hits.hits.0._explanation.value: 1.0}
  - match: {hits.hits.0._explanation.description: "voltage_times_ten:58"}
  - match: {hits.hits.0._explanation.details.0.value: 1.0}
  - match: {hits.hits.0._explanation.details.0.description: 'boost * runtime_field_score'}
  - match: {hits.hits.0._explanation.details.0.details.0.value: 1.0}
  - match: {hits.hits.0._explanation.details.0.details.0.description: 'boost'}
  - match: {hits.hits.0._explanation.details.0.details.1.value: 1.0}
  - match: {hits.hits.0._explanation.details.0.details.1.description: 'runtime_field_score is always 1'}

---
"nested":
  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            runtime:
              users.first_script:
                type: long
                script: emit(doc['users.first'].value.length())
              users.last_script:
                type:   long
                script: emit(doc['users.last'].value.length())
            properties:
              users:
                type: nested
                properties:
                  first:
                    type: keyword
                  last:
                    type: keyword
  - do:
      bulk:
        index: test
        refresh: true
        body: |
          {"index":{"_id": 1}}
          {"group" : "fans", "users" : [{"first" : "John", "last" : "Smith"}, {"first": "Alice", "last": "Whighte"}]}
          {"index":{"_id": 2}}
          {"group" : "fans", "users" : [{"first" : "Mark", "last" : "Doe"}]}

  - do:
      search:
        index: test
        body:
          query:
            nested:
              path: users
              query:
                bool:
                  must:
                    - match:
                        users.first_script: 4
                    - match:
                        users.last_script: 5
  - match: {hits.total.value: 1}

  - do:
      search:
        index: test
        body:
          query:
            nested:
              path: users
              query:
                bool:
                  must:
                    - match:
                        users.first_script: 4
                    - match:
                        users.last_script: 7
  - match: {hits.total.value: 0}

  - do:
      search:
        body:
          aggs:
            to-users:
              nested:
                path: users
              aggs:
                users:
                  top_hits:
                    sort: users.last_script
  - match: { hits.total.value: 2 }
  - length: { aggregations.to-users.users.hits.hits: 3 }
  - match: { aggregations.to-users.users.hits.hits.0._id: "2" }
  - match: { aggregations.to-users.users.hits.hits.0._index: test }
  - match: { aggregations.to-users.users.hits.hits.0._nested.field: users }
  - match: { aggregations.to-users.users.hits.hits.0._nested.offset: 0 }
  - match: { aggregations.to-users.users.hits.hits.1._id: "1" }
  - match: { aggregations.to-users.users.hits.hits.1._index: test }
  - match: { aggregations.to-users.users.hits.hits.1._nested.field: users }
  - match: { aggregations.to-users.users.hits.hits.1._nested.offset: 0 }
  - match: { aggregations.to-users.users.hits.hits.2._id: "1" }
  - match: { aggregations.to-users.users.hits.hits.2._index: test }
  - match: { aggregations.to-users.users.hits.hits.2._nested.field: users }
  - match: { aggregations.to-users.users.hits.hits.2._nested.offset: 1 }

