setup:

  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: "version script field support was added in 8.1.0"

  - do:
      indices.create:
        index:  test1
        body:
          mappings:
            properties:
              ver:
                type: version

  - do:
      bulk:
        index: test1
        refresh: true
        body: |
          { "index": {"_id" : "1"} }
          { "ver": "1.0.0" }
          { "index": {"_id" : "2"} }
          { "ver": "1.2.3-abc+def" }
          { "index": {"_id" : "3"} }
          { "ver": "1.2.3.4.5" }
          { "index": {"_id" : "4"} }
          { "ver": ["6.7.8", "5.4.3"] }

---
"Scripted fields version values return Version":
  - do:
      search:
        index: test1
        body:
          sort: [ { ver: desc } ]
          script_fields:
            field:
              script:
                source: "field('ver').get(new Version(''))"

  - match: { hits.hits.0.fields.field.0: "5.4.3" }
  - match: { hits.hits.1.fields.field.0: "1.2.3.4.5" }
  - match: { hits.hits.2.fields.field.0: "1.2.3-abc+def" }
  - match: { hits.hits.3.fields.field.0: "1.0.0" }
