# Integration tests for the version field
#
---
setup:

  - requires:
      test_runner_features: headers
      cluster_features: ["gte_v7.10.1"]
      reason: "version field is added to 7.10"

  - do:
        indices.create:
          index: test_index
          body:
              settings:
                number_of_shards: 1
              mappings:
                properties:
                  version:
                    type: version

  - do:
        bulk:
          refresh: true
          body:
            - '{ "index" : { "_index" : "test_index", "_id" : "1" } }'
            - '{"version": "1.1.12" }'
            - '{ "index" : { "_index" : "test_index", "_id" : "2" } }'
            - '{"version": "2.0.0-beta" }'
            - '{ "index" : { "_index" : "test_index", "_id" : "3" } }'
            - '{"version": "3.1.0" }'

---
"Filter script":
  - do:
        search:
          index: test_index
          body:
            query: { "script" : { "script" : { "source": "doc['version'].value.length() > 5"} } }

  - match: { hits.total.value: 2 }
  - match: { hits.hits.0._source.version: "1.1.12" }
  - match: { hits.hits.1._source.version: "2.0.0-beta" }

---
"Sort script":
  - do:
        search:
          index: test_index
          body:
            sort: { "_script" : { "type" : "number", "script" : { "source": "doc['version'].value.length()" } } }

  - match: { hits.total.value: 3 }
  - match: { hits.hits.0._source.version: "3.1.0" }
  - match: { hits.hits.1._source.version: "1.1.12" }
  - match: { hits.hits.2._source.version: "2.0.0-beta" }


---
"Sort script returning version value":
  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason: "order by script returning version supported in v 8.3"
  - do:
      search:
        index: test_index
        body:
          sort: { "_script" : { "type" : "version", "script" : { "source": "doc['version'].value" } } }

  - match: { hits.total.value: 3 }
  - match: { hits.hits.0._source.version: "1.1.12" }
  - match: { hits.hits.1._source.version: "2.0.0-beta" }
  - match: { hits.hits.2._source.version: "3.1.0" }

---
"Sort script with field() returning version value":
  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason: "order by script returning version supported in v 8.3"
  - do:
      search:
        index: test_index
        body:
          sort: { "_script" : { "type" : "version", "script" : { "source": "field('version').get(new Version('0'))" } } }

  - match: { hits.total.value: 3 }
  - match: { hits.hits.0._source.version: "1.1.12" }
  - match: { hits.hits.1._source.version: "2.0.0-beta" }
  - match: { hits.hits.2._source.version: "3.1.0" }
