---
setup:
  - do:
      indices.create:
        index: sensor
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            runtime:
              tomorrow:
                type: date
                script:
                  source: |
                    for (def dt : doc['timestamp']) {
                      emit(dt.plus(params.days, ChronoUnit.DAYS).toEpochMilli());
                    }
                  params:
                    days: 1
              # Test fetching from _source and parsing
              tomorrow_from_source:
                type: date
                script:
                  source: |
                    Instant instant = Instant.ofEpochMilli(parse(params._source.timestamp));
                    ZonedDateTime dt = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
                    emit(dt.plus(1, ChronoUnit.DAYS).toEpochMilli());
              # Test returning millis
              the_past:
                type: date
                script:
                  source: |
                    for (def dt : doc['timestamp']) {
                      emit(dt.toInstant().toEpochMilli() - 1000);
                    }
              # Test fetching many values
              all_week:
                type: date
                script:
                  source: |
                    for (def dt : doc['timestamp']) {
                      for (int i = 0; i < 7; i++) {
                        emit(dt.plus(i, ChronoUnit.DAYS).toEpochMilli());
                      }
                    }
              # Test format parameter
              formatted_tomorrow:
                type: date
                format: yyyy-MM-dd
                script: |
                  for (def dt : doc['timestamp']) {
                    emit(dt.plus(1, ChronoUnit.DAYS).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": "2018-01-18T17:41:34.000Z", "temperature": 202, "voltage": 4.0, "node": "c"}

---
"get mapping":
  - do:
      indices.get_mapping:
        index: sensor
  - match: {sensor.mappings.runtime.tomorrow.type: date }
  - match:
      sensor.mappings.runtime.tomorrow.script.source: |
        for (def dt : doc['timestamp']) {
          emit(dt.plus(params.days, ChronoUnit.DAYS).toEpochMilli());
        }
  - match: {sensor.mappings.runtime.tomorrow.script.params: {days: 1} }
  - match: {sensor.mappings.runtime.tomorrow.script.lang: painless }

  - match: {sensor.mappings.runtime.formatted_tomorrow.type: date }
  - match:
      sensor.mappings.runtime.formatted_tomorrow.script.source: |
        for (def dt : doc['timestamp']) {
          emit(dt.plus(1, ChronoUnit.DAYS).toEpochMilli());
        }
  - match: {sensor.mappings.runtime.formatted_tomorrow.script.lang: painless }
  - match: {sensor.mappings.runtime.formatted_tomorrow.format: yyyy-MM-dd }

---
"fetch fields":
  - do:
      search:
        index: sensor
        body:
          sort: timestamp
          fields: [tomorrow, tomorrow_from_source, the_past, all_week, formatted_tomorrow]
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.tomorrow: ["2018-01-19T17:41:34.000Z"] }
  - match: {hits.hits.0.fields.tomorrow_from_source: ["2018-01-19T17:41:34.000Z"] }
  - match: {hits.hits.0.fields.the_past: ["2018-01-18T17:41:33.000Z"] }
  - match:
      hits.hits.0.fields.all_week:
        - 2018-01-18T17:41:34.000Z
        - 2018-01-19T17:41:34.000Z
        - 2018-01-20T17:41:34.000Z
        - 2018-01-21T17:41:34.000Z
        - 2018-01-22T17:41:34.000Z
        - 2018-01-23T17:41:34.000Z
        - 2018-01-24T17:41:34.000Z
  - match: {hits.hits.0.fields.formatted_tomorrow: [2018-01-19] }

---
"docvalue_fields":
  - do:
      search:
        index: sensor
        body:
          sort: timestamp
          docvalue_fields: [tomorrow, tomorrow_from_source, the_past, all_week, formatted_tomorrow]
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.tomorrow: ["2018-01-19T17:41:34.000Z"] }
  - match: {hits.hits.0.fields.tomorrow_from_source: ["2018-01-19T17:41:34.000Z"] }
  - match: {hits.hits.0.fields.the_past: ["2018-01-18T17:41:33.000Z"] }
  - match:
      hits.hits.0.fields.all_week:
        - 2018-01-18T17:41:34.000Z
        - 2018-01-19T17:41:34.000Z
        - 2018-01-20T17:41:34.000Z
        - 2018-01-21T17:41:34.000Z
        - 2018-01-22T17:41:34.000Z
        - 2018-01-23T17:41:34.000Z
        - 2018-01-24T17:41:34.000Z
  - match: {hits.hits.0.fields.formatted_tomorrow: [2018-01-19] }

---
"terms agg":
  - do:
      search:
        index: sensor
        body:
          aggs:
            v10:
              terms:
                field: tomorrow
                format: strict_date_optional_time
  - match: {hits.total.value: 6}
  - match: {aggregations.v10.buckets.0.key_as_string: "2018-01-19T17:41:34.000Z"}
  - match: {aggregations.v10.buckets.0.doc_count: 1}
  - match: {aggregations.v10.buckets.1.key_as_string: "2018-01-20T17:41:34.000Z"}
  - match: {aggregations.v10.buckets.1.doc_count: 1}

---
"term query":
  - do:
      search:
        index: sensor
        body:
          query:
            term:
              tomorrow: 2018-01-19T17:41:34Z
  - match: {hits.total.value: 1}
  - match: {hits.hits.0._source.voltage: 4.0}

---
"explain term query":
  - do:
      search:
        index: sensor
        explain: true
        body:
          query:
            term:
              tomorrow: 2018-01-19T17:41:34Z
  - match: {hits.hits.0._explanation.value: 1.0}
  - match: {hits.hits.0._explanation.description: "tomorrow:1516383694000"}
  - 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'}
