---
setup:
  - do:
      indices.create:
        index: sensor
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            dynamic: false
            runtime:
              history.installed:
                type: date
                format: yyyy-MM-dd
            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", "history" : { "installed" : "2020-11-10" } }
          {"index":{}}
          {"timestamp": 1516642894000, "temperature": 201, "voltage": 5.8, "node": "b", "history" : { "installed" : "2020-14-10" } }
          {"index":{}}
          {"timestamp": 1516556494000, "temperature": 202, "voltage": 5.1, "node": "a", "history" : { "installed" : [ "2020-11-10", 2020 ] } }
          {"index":{}}
          {"timestamp": 1516470094000, "temperature": 198, "voltage": 5.6, "node": "b", "history" : [ { "installed" : "2020-10-10" } ] }
          {"index":{}}
          {"timestamp": 1516383694000, "temperature": 200, "voltage": 4.2, "node": "c", "history" : { "installed" : "foo" } }
          {"index":{}}
          {"timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c", "history" : { "installed" : "2020-11-10" } }

---
"get mapping":
  - do:
      indices.get_mapping:
        index: sensor
  - match: { sensor.mappings.runtime.history\.installed.type: date }
  - match: { sensor.mappings.runtime.history\.installed.format: yyyy-MM-dd }
  - is_false: sensor.mappings.runtime.history\.installed.script

---
"test matching":
  - do:
      search:
        index: sensor
        body:
          query:
            range:
              history.installed:
                gte: "2020-11-01"
                lte: "2020-11-30"
                format: "yyyy-MM-dd"
  - match: { hits.total.value: 3 }
