---
setup:
  - do:
      indices.create:
        index: sensor
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            runtime:
              over_v:
                type: boolean
                script:
                  source: |
                    for (def v : doc['voltage']) {
                      emit(v >= params.min_v);
                    }
                  params:
                    min_v: 5.0
              # Test fetching from _source
              over_v_from_source:
                type: boolean
                script:
                  source: |
                    emit(params._source.voltage >= 5.0);
              # Test many booleans
              big_vals:
                type: boolean
                script:
                  source: |
                    for (def v : doc['temperature']) {
                      emit(v >= 200);
                    }
                    for (def v : doc['voltage']) {
                      emit(v >= 5.0);
                    }
            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.over_v.type: boolean }
  - match:
      sensor.mappings.runtime.over_v.script.source: |
        for (def v : doc['voltage']) {
          emit(v >= params.min_v);
        }
  - match: {sensor.mappings.runtime.over_v.script.params: {min_v: 5.0} }
  - match: {sensor.mappings.runtime.over_v.script.lang: painless }

---
"fetch fields":
  - do:
      search:
        index: sensor
        body:
          sort: timestamp
          fields: [over_v, over_v_from_source, big_vals]
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.over_v: [false] }
  - match: {hits.hits.0.fields.over_v_from_source: [false] }
  - match: {hits.hits.0.fields.big_vals: [false, true] } # doc values are sorted with falses before trues

---
"docvalue_fields":
  - do:
      search:
        index: sensor
        body:
          sort: timestamp
          docvalue_fields: [over_v, over_v_from_source, big_vals]
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.over_v: [false] }
  - match: {hits.hits.0.fields.over_v_from_source: [false] }
  - match: {hits.hits.0.fields.big_vals: [false, true] } # doc values are sorted with falses before trues

---
"terms agg":
  - do:
      search:
        index: sensor
        body:
          aggs:
            over_v:
              terms:
                field: over_v
  - match: {hits.total.value: 6}
  - match: {aggregations.over_v.buckets.0.key_as_string: "true"}
  - match: {aggregations.over_v.buckets.0.doc_count: 4}
  - match: {aggregations.over_v.buckets.1.key_as_string: "false"}
  - match: {aggregations.over_v.buckets.1.doc_count: 2}

---
"term query":
  - do:
      search:
        index: sensor
        body:
          query:
            term:
              over_v: true
          sort:
            timestamp: asc
  - match: {hits.total.value: 4}
  - match: {hits.hits.0._source.voltage: 5.6}

---
"explain term query":
  - do:
      search:
        index: sensor
        explain: true
        body:
          query:
            term:
              over_v: true
  - match: {hits.hits.0._explanation.value: 1.0}
  - match: {hits.hits.0._explanation.description: "over_v:true"}
  - 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'}
