setup:
  - requires:
      cluster_features: [ "mapper.source.mode_from_index_setting" ]
      reason: "Source mode configured through index setting"
  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
          mappings:
            properties:
              boolean:
                type: boolean
              boolean_no_doc_values:
                type: boolean
                doc_values: false
              date:
                type: date
              date_no_doc_values:
                type: date
                doc_values: false
              nanos:
                type: date_nanos
              nanos_no_doc_values:
                type: date_nanos
                doc_values: false
              geo_point:
                type: geo_point
              geo_point_no_doc_values:
                type: geo_point
                doc_values: false
              ip:
                type: ip
              keyword:
                type: keyword
              keyword_no_doc_values:
                type: keyword
                doc_values: false
              long:
                type: long
              long_no_doc_values:
                type: long
                doc_values: false
              integer:
                type: integer
              integer_no_doc_values:
                type: integer
                doc_values: false
              short:
                type: short
              short_no_doc_values:
                type: short
                doc_values: false
              byte:
                type: byte
              byte_no_doc_values:
                type: byte
                doc_values: false
              double:
                type: double
              double_no_doc_values:
                type: double
                doc_values: false
              float:
                type: float
              float_no_doc_values:
                type: float
                doc_values: false
              half_float:
                type: half_float
              half_float_no_doc_values:
                type: half_float
                doc_values: false
              scaled_float:
                type: scaled_float
                scaling_factor: 100
              scaled_float_no_doc_values:
                type: scaled_float
                scaling_factor: 100
                doc_values: false
              text:
                type: text
                fielddata: true
              text_no_field_data:
                type: text
              match_only_text:
                type: match_only_text
              token_count:
                type: token_count
                analyzer: standard
              rank:
                type: integer
              flattended:
                type: flattened


  - do:
      index:
        index: test
        id: "1"
        body:
          rank: 1
          boolean: true
          boolean_no_doc_values: true
          date: 2017-01-01T12:11:12
          date_no_doc_values: 2017-01-01T12:11:12
          nanos: 2015-01-01T12:10:30.123456789Z
          nanos_no_doc_values: 2015-01-01T12:10:30.123456789Z
          geo_point: 41.12,-71.34
          geo_point_no_doc_values: 41.12,-71.34
          ip: 192.168.0.19
          keyword: not split at all
          keyword_no_doc_values: no doc values
          long: 12348732141234
          long_no_doc_values: 12348732141234
          integer: 134134566
          integer_no_doc_values: 134134566
          short: 1324
          short_no_doc_values: 1324
          byte: 12
          byte_no_doc_values: 12
          double: 3.14159265358979
          double_no_doc_values: 3.14159265358979
          float: 3.141592654
          float_no_doc_values: 3.141592654
          half_float: 3.140625
          half_float_no_doc_values: 3.140625
          scaled_float: 3.14
          scaled_float_no_doc_values: 3.14
          text: "Lots of text."
          text_no_field_data: "Lots of text."
          match_only_text: "Lots of text."
          token_count: count all these words please

  - do:
      index:
        index: test
        id: "2"
        body:
          rank: 2

  - do:
      index:
        index: test
        id: "3"
        body:
          rank: 3
          boolean: [ true, false, true ]
          boolean_no_doc_values: [ true, false, true ]
          ip: [ "10.1.2.3", "2001:db8::2:1" ]
          date: [ 2017-01-01T12:11:12, 2018-01-01T12:11:12 ]
          date_no_doc_values: [ 2017-01-01T12:11:12, 2018-01-01T12:11:12 ]
          nanos: [ 2015-01-01T12:10:30.123456789Z, 2015-01-01T12:10:30.987654321Z ]
          nanos_no_doc_values: [ 2015-01-01T12:10:30.123456789Z, 2015-01-01T12:10:30.987654321Z ]
          geo_point: [ [ -71.34,41.12 ],[ 60.32,21.25 ] ]
          geo_point_no_doc_values: [ [ 60.32,21.25 ],[ -71.34,41.12 ] ]
          keyword: [ "one string", "another string" ]
          keyword_no_doc_values: [ "no doc values 1", "no doc values 0", "no doc values 2" ]
          long: [ 1152921504606846976, 576460752303423488 ]
          long_no_doc_values: [ 576460752303423488, 1152921504606846976, -1152921504606846976 ]
          integer: [ 5, 17, 29 ]
          integer_no_doc_values: [ 17, 29, 5 ]
          short: [ 6, 18, 30, 45 ]
          short_no_doc_values: [ 30, 45, 18, 6 ]
          byte: [ 16, 32, 64, 8, 4 ]
          byte_no_doc_values: [ 16, 8, 32, 4, 64 ]
          double: [ 3.141592653588, 2.141592653587 ]
          double_no_doc_values: [ 3.141592653588, 2.141592653587 ]
          float: [ 1.123, 2.234 ]
          float_no_doc_values: [ 2.234, 1.123 ]
          half_float: [ 1.123, 2.234 ]
          half_float_no_doc_values: [ 2.234, 1.123 ]
          scaled_float: [ -3.5, 2.5 ]
          scaled_float_no_doc_values: [ 2.5, -3.5 ]
          text: [ "Lots of text.", "even more text", "SOOOOO much text" ]
          text_no_field_data: [ "Lots of text.", "even more text", "SOOOOO much text" ]
          match_only_text: [ "Lots of text.", "even more text", "SOOOOO much text" ]

  - do:
      indices.create:
        index: test_synthetic
        body:
          settings:
            number_of_shards: 1
            index:
              mapping.source.mode: synthetic
          mappings:
            properties:
              boolean:
                type: boolean
              date:
                type: date
              nanos:
                type: date_nanos
              geo_point:
                type: geo_point
              ip:
                type: ip
              keyword:
                type: keyword
              keyword_stored:
                type: keyword
                doc_values: false
                store: true
              long:
                type: long
              integer:
                type: integer
              short:
                type: short
              byte:
                type: byte
              double:
                type: double
              float:
                type: float
              half_float:
                type: half_float
              scaled_float:
                type: scaled_float
                scaling_factor: 100
              text:
                type: text
                fielddata: true
                fields:
                  raw:
                    type: keyword
              text_sub_stored_keyword:
                type: text
                fields:
                  raw:
                    type: keyword
                    doc_values: false
                    store: true
              text_stored:
                type: text
                store: true
              match_only_text:
                type: match_only_text
              rank:
                type: integer

  - do:
      index:
        index: test_synthetic
        id: "1"
        body:
          rank: 1
          boolean: true
          date: 2017-01-01T12:11:12
          nanos: 2015-01-01T12:10:30.123456789Z
          geo_point: 41.12,-71.34
          ip: 192.168.0.19
          keyword: not split at all
          keyword_stored: no doc values
          long: 12348732141234
          integer: 134134566
          short: 1324
          byte: 12
          double: 3.14159265358979
          float: 3.141592654
          half_float: 3.140625
          scaled_float: 3.14
          text: "Lots of text."
          text_sub_stored_keyword: "Lots of text."
          text_stored: "Lots of text."
          match_only_text: "Lots of text."

  - do:
      index:
        index: test_synthetic
        id: "2"
        body:
          rank: 2

  - do:
      index:
        index: test_synthetic
        id: "3"
        body:
          rank: 3
          boolean: [ true, false, true ]
          ip: [ "10.1.2.3", "2001:db8::2:1" ]
          date: [ 2017-01-01T12:11:12, 2018-01-01T12:11:12 ]
          nanos: [ 2015-01-01T12:10:30.123456789Z, 2015-01-01T12:10:30.987654321Z ]
          geo_point: [ [ -71.34,41.12 ],[ 60.32,21.25 ] ]
          keyword: [ "one string", "another string" ]
          keyword_stored: [ "no doc values 1", "no doc values 0", "no doc values 2" ]
          long: [ 1152921504606846976, 576460752303423488 ]
          integer: [ 5, 17, 29 ]
          short: [ 6, 18, 30, 45 ]
          byte: [ 16, 32, 64, 8, 4 ]
          double: [ 3.141592653588, 2.141592653587 ]
          float: [ 1.123, 2.234 ]
          half_float: [ 1.123, 2.234 ]
          scaled_float: [ -3.5, 2.5 ]
          text: [ "Lots of text.", "even more text", "SOOOOO much text" ]
          text_sub_stored_keyword: [ "Lots of text.", "even more text", "SOOOOO much text" ]
          text_stored: [ "Lots of text.", "even more text", "SOOOOO much text" ]
          match_only_text: [ "Lots of text.", "even more text", "SOOOOO much text" ]

  - do:
      indices.refresh: { }

---
boolean:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['boolean'].get(0)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['boolean'].value"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('boolean').get(false)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash for '$' */ $('boolean', false)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('boolean').get(false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash for '$' */ $('boolean', false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('boolean').get(1, false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "int total = 0; for (boolean b : field('boolean')) { total += b ? 1 : 0; } total;"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "int total = 0; for (boolean b : field('boolean')) { total += b ? 1 : 0; } total + field('boolean').size();"
  - match: { hits.hits.0.fields.field.0: 5 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('boolean').size()"
  - match: { hits.hits.0.fields.field.0: 0 }

---
boolean_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['boolean_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['boolean_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('boolean_no_doc_values').get(false)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash for '$' */ $('boolean_no_doc_values', false)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('boolean_no_doc_values').get(false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash for '$' */ $('boolean_no_doc_values', false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('boolean_no_doc_values').get(1, false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "int total = 0; for (boolean b : field('boolean_no_doc_values')) { total += b ? 1 : 0; } total;"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "int total = 0; for (boolean b : field('boolean_no_doc_values')) { total += b ? 1 : 0; } total + field('boolean').size();"
  - match: { hits.hits.0.fields.field.0: 5 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('boolean_no_doc_values').size()"
  - match: { hits.hits.0.fields.field.0: 0 }

---
boolean_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['boolean'].get(0)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['boolean'].value"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('boolean').get(false)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash for '$' */ $('boolean', false)"
  - match: { hits.hits.0.fields.field.0: true }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('boolean').get(false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash for '$' */ $('boolean', false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('boolean').get(1, false)"
  - match: { hits.hits.0.fields.field.0: false }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "int total = 0; for (boolean b : field('boolean')) { total += b ? 1 : 0; } total;"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "int total = 0; for (boolean b : field('boolean')) { total += b ? 1 : 0; } total + field('boolean').size();"
  - match: { hits.hits.0.fields.field.0: 5 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('boolean').size()"
  - match: { hits.hits.0.fields.field.0: 0 }

---
date:
  - skip:
      features: "warnings"

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc.date.get(0)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc.date.value"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('date').get(null)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', null)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.get(0).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.value.getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "field('date').get(null).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', null).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.get(0).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.value.millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "field('date').get(null).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', null).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('date').get(ZonedDateTime.parse('2018-01-01T12:11:12.000Z'))"
  - match: { hits.hits.0.fields.field.0: '2018-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', ZonedDateTime.parse('2018-01-01T12:11:12.000Z'))"
  - match: { hits.hits.0.fields.field.0: '2018-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['nanos'].value"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(null)"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos', null)"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(ZonedDateTime.parse('2016-01-01T12:10:30.123Z'))"
  - match: { hits.hits.0.fields.field.0: '2016-01-01T12:10:30.123Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos', ZonedDateTime.parse('2016-01-01T12:10:30.123Z'))"
  - match: { hits.hits.0.fields.field.0: '2016-01-01T12:10:30.123Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['nanos'].value.getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(null).getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos', null).getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(ZonedDateTime.parse('2016-01-01T12:10:30.123Z')).getNano()"
  - match: { hits.hits.0.fields.field.0: 123000000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('date').get(1, null)"
  - match: { hits.hits.0.fields.field.0: "2018-01-01T12:11:12.000Z" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(1, null)"
  - match: { hits.hits.0.fields.field.0: "2015-01-01T12:10:30.987654321Z" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "List times = new ArrayList(); for (ZonedDateTime zdt : field('date')) times.add(zdt); times"
  - match: { hits.hits.0.fields.field: [ "2017-01-01T12:11:12.000Z", "2018-01-01T12:11:12.000Z" ] }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "List times = new ArrayList(); for (ZonedDateTime zdt : field('nanos')) times.add(zdt); times"
  - match: { hits.hits.0.fields.field: [ "2015-01-01T12:10:30.123456789Z", "2015-01-01T12:10:30.987654321Z" ] }

---
date_no_doc_values:
  - skip:
      features: "warnings"

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc.date_no_doc_values.get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc.date_no_doc_values.value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('date_no_doc_values').get(null)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date_no_doc_values', null)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "field('date_no_doc_values').get(null).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date_no_doc_values', null).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "field('date_no_doc_values').get(null).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date_no_doc_values', null).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('date_no_doc_values').get(ZonedDateTime.parse('2018-01-01T12:11:12.000Z'))"
  - match: { hits.hits.0.fields.field.0: '2018-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date_no_doc_values', ZonedDateTime.parse('2018-01-01T12:11:12.000Z'))"
  - match: { hits.hits.0.fields.field.0: '2018-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('nanos_no_doc_values').get(null)"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos_no_doc_values', null)"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('nanos_no_doc_values').get(ZonedDateTime.parse('2016-01-01T12:10:30.123Z'))"
  - match: { hits.hits.0.fields.field.0: '2016-01-01T12:10:30.123Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos_no_doc_values', ZonedDateTime.parse('2016-01-01T12:10:30.123Z'))"
  - match: { hits.hits.0.fields.field.0: '2016-01-01T12:10:30.123Z' }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('nanos_no_doc_values').get(null).getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos_no_doc_values', null).getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('nanos_no_doc_values').get(ZonedDateTime.parse('2016-01-01T12:10:30.123Z')).getNano()"
  - match: { hits.hits.0.fields.field.0: 123000000 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('date_no_doc_values').get(1, null)"
  - match: { hits.hits.0.fields.field.0: "2018-01-01T12:11:12.000Z" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('nanos_no_doc_values').get(1, null)"
  - match: { hits.hits.0.fields.field.0: "2015-01-01T12:10:30.987654321Z" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "List times = new ArrayList(); for (ZonedDateTime zdt : field('date_no_doc_values')) times.add(zdt); times"
  - match: { hits.hits.0.fields.field: [ "2017-01-01T12:11:12.000Z", "2018-01-01T12:11:12.000Z" ] }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "List times = new ArrayList(); for (ZonedDateTime zdt : field('nanos_no_doc_values')) times.add(zdt); times"
  - match: { hits.hits.0.fields.field: [ "2015-01-01T12:10:30.123456789Z", "2015-01-01T12:10:30.987654321Z" ] }

---
date_synthetic:
  - skip:
      features: "warnings"

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc.date.get(0)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc.date.value"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('date').get(null)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', null)"
  - match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.get(0).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.value.getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "field('date').get(null).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', null).getMillis()"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.get(0).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "doc.date.value.millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "field('date').get(null).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', null).millis"
  - match: { hits.hits.0.fields.field.0: 1483272672000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('date').get(ZonedDateTime.parse('2018-01-01T12:11:12.000Z'))"
  - match: { hits.hits.0.fields.field.0: '2018-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('date', ZonedDateTime.parse('2018-01-01T12:11:12.000Z'))"
  - match: { hits.hits.0.fields.field.0: '2018-01-01T12:11:12.000Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['nanos'].value"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(null)"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos', null)"
  - match: { hits.hits.0.fields.field.0: '2015-01-01T12:10:30.123456789Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(ZonedDateTime.parse('2016-01-01T12:10:30.123Z'))"
  - match: { hits.hits.0.fields.field.0: '2016-01-01T12:10:30.123Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos', ZonedDateTime.parse('2016-01-01T12:10:30.123Z'))"
  - match: { hits.hits.0.fields.field.0: '2016-01-01T12:10:30.123Z' }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['nanos'].value.getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(null).getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('nanos', null).getNano()"
  - match: { hits.hits.0.fields.field.0: 123456789 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(ZonedDateTime.parse('2016-01-01T12:10:30.123Z')).getNano()"
  - match: { hits.hits.0.fields.field.0: 123000000 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('date').get(1, null)"
  - match: { hits.hits.0.fields.field.0: "2018-01-01T12:11:12.000Z" }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('nanos').get(1, null)"
  - match: { hits.hits.0.fields.field.0: "2015-01-01T12:10:30.987654321Z" }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "List times = new ArrayList(); for (ZonedDateTime zdt : field('date')) times.add(zdt); times"
  - match: { hits.hits.0.fields.field: [ "2017-01-01T12:11:12.000Z", "2018-01-01T12:11:12.000Z" ] }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "List times = new ArrayList(); for (ZonedDateTime zdt : field('nanos')) times.add(zdt); times"
  - match: { hits.hits.0.fields.field: [ "2015-01-01T12:10:30.123456789Z", "2015-01-01T12:10:30.987654321Z" ] }

---
geo_point:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['geo_point'].get(0)"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['geo_point'].value"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('geo_point').get(new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point', new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('geo_point').get(new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 21.249999990686774 }
  - match: { hits.hits.0.fields.field.0.lon: 60.319999968633056 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point', new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 21.249999990686774 }
  - match: { hits.hits.0.fields.field.0.lon: 60.319999968633056 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('geo_point').get(1, new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point', new GeoPoint(1.0, 2.0))"
  - match: { hits.hits.0.fields.field.0.lat: 1.0 }
  - match: { hits.hits.0.fields.field.0.lon: 2.0 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            centroid:
              script:
                source: "doc['geo_point'].getCentroid()"
  - match: { hits.hits.0.fields.centroid.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.centroid.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            bbox:
              script:
                source: "doc['geo_point'].getBoundingBox()"
  - match: { hits.hits.0.fields.bbox.0.top_left.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.bbox.0.top_left.lon: -71.34000004269183 }
  - match: { hits.hits.0.fields.bbox.0.bottom_right.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.bbox.0.bottom_right.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            label_position:
              script:
                source: "doc['geo_point'].getLabelPosition()"
  - match: { hits.hits.0.fields.label_position.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.label_position.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            topLeft:
              script:
                source: "doc['geo_point'].getBoundingBox().topLeft()"
            bottomRight:
              script:
                source: "doc['geo_point'].getBoundingBox().bottomRight()"
  - match: { hits.hits.0.fields.topLeft.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.topLeft.0.lon: -71.34000004269183 }
  - match: { hits.hits.0.fields.bottomRight.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.bottomRight.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            type:
              script:
                source: "doc['geo_point'].getDimensionalType()"
  - match: { hits.hits.0.fields.type.0: 0 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            width:
              script:
                source: "doc['geo_point'].getMercatorWidth()"
            height:
              script:
                source: "doc['geo_point'].getMercatorHeight()"
  - match: { hits.hits.0.fields.width.0: 0.0 }
  - match: { hits.hits.0.fields.height.0: 0.0 }

---
geo_point_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['geo_point_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['geo_point_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('geo_point_no_doc_values').get(new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point_no_doc_values', new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('geo_point_no_doc_values').get(new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 21.249999990686774 }
  - match: { hits.hits.0.fields.field.0.lon: 60.319999968633056 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point_no_doc_values', new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 21.249999990686774 }
  - match: { hits.hits.0.fields.field.0.lon: 60.319999968633056 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('geo_point_no_doc_values').get(1, new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point_no_doc_values', new GeoPoint(1.0, 2.0))"
  - match: { hits.hits.0.fields.field.0.lat: 1.0 }
  - match: { hits.hits.0.fields.field.0.lon: 2.0 }

---
geo_point_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['geo_point'].get(0)"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['geo_point'].value"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "field('geo_point').get(new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point', new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('geo_point').get(new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 21.249999990686774 }
  - match: { hits.hits.0.fields.field.0.lon: 60.319999968633056 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point', new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 21.249999990686774 }
  - match: { hits.hits.0.fields.field.0.lon: 60.319999968633056 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "3" } }
          script_fields:
            field:
              script:
                source: "field('geo_point').get(1, new GeoPoint())"
  - match: { hits.hits.0.fields.field.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.field.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "2" } }
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('geo_point', new GeoPoint(1.0, 2.0))"
  - match: { hits.hits.0.fields.field.0.lat: 1.0 }
  - match: { hits.hits.0.fields.field.0.lon: 2.0 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            centroid:
              script:
                source: "doc['geo_point'].getCentroid()"
  - match: { hits.hits.0.fields.centroid.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.centroid.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            bbox:
              script:
                source: "doc['geo_point'].getBoundingBox()"
  - match: { hits.hits.0.fields.bbox.0.top_left.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.bbox.0.top_left.lon: -71.34000004269183 }
  - match: { hits.hits.0.fields.bbox.0.bottom_right.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.bbox.0.bottom_right.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            label_position:
              script:
                source: "doc['geo_point'].getLabelPosition()"
  - match: { hits.hits.0.fields.label_position.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.label_position.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            topLeft:
              script:
                source: "doc['geo_point'].getBoundingBox().topLeft()"
            bottomRight:
              script:
                source: "doc['geo_point'].getBoundingBox().bottomRight()"
  - match: { hits.hits.0.fields.topLeft.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.topLeft.0.lon: -71.34000004269183 }
  - match: { hits.hits.0.fields.bottomRight.0.lat: 41.1199999647215 }
  - match: { hits.hits.0.fields.bottomRight.0.lon: -71.34000004269183 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            type:
              script:
                source: "doc['geo_point'].getDimensionalType()"
  - match: { hits.hits.0.fields.type.0: 0 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            width:
              script:
                source: "doc['geo_point'].getMercatorWidth()"
            height:
              script:
                source: "doc['geo_point'].getMercatorHeight()"
  - match: { hits.hits.0.fields.width.0: 0.0 }
  - match: { hits.hits.0.fields.height.0: 0.0 }

---
ip:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['ip'].get(0)"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['ip'].value"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('ip').get(new IPAddress('127.0.0.1'))"
            field_string:
              script:
                source: "field('ip').asString('127.0.0.1')"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }
  - match: { hits.hits.0.fields.field_string.0: "192.168.0.19" }
  - match: { hits.hits.1.fields.field.0: "127.0.0.1" }
  - match: { hits.hits.1.fields.field_string.0: "127.0.0.1" }
  - match: { hits.hits.2.fields.field.0: "10.1.2.3" }
  - match: { hits.hits.2.fields.field_string.0: "10.1.2.3" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('ip').get(1, new IPAddress('127.0.0.1'))"
            field_string:
              script:
                source: "field('ip').asString(1, '127.0.0.1')"
  - match: { hits.hits.0.fields.field.0: "127.0.0.1" }
  - match: { hits.hits.0.fields.field_string.0: "127.0.0.1" }
  - match: { hits.hits.1.fields.field.0: "127.0.0.1" }
  - match: { hits.hits.1.fields.field_string.0: "127.0.0.1" }
  - match: { hits.hits.2.fields.field.0: "2001:db8::2:1" }
  - match: { hits.hits.2.fields.field_string.0: "2001:db8::2:1" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String.join(',', field('ip').asStrings())"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "10.1.2.3,2001:db8::2:1" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          runtime_mappings:
            ip_script_field:
              type: ip
              script:
                source: "for (IPAddress addr : field('ip')) { String ip = addr.toString(); emit(ip.substring(0, ip.length() - 1) + field('ip').size()) }"
          script_fields:
            field:
              script:
                source: "field('ip_script_field').get(new IPAddress('1.2.3.4'))"
  - match: { hits.hits.0.fields.field.0: "192.168.0.11" }
  - match: { hits.hits.1.fields.field.0: "1.2.3.4" }
  - match: { hits.hits.2.fields.field.0: "10.1.2.2" }

---
ip_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['ip'].get(0)"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['ip'].value"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('ip').get(new IPAddress('127.0.0.1'))"
            field_string:
              script:
                source: "field('ip').asString('127.0.0.1')"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }
  - match: { hits.hits.0.fields.field_string.0: "192.168.0.19" }
  - match: { hits.hits.1.fields.field.0: "127.0.0.1" }
  - match: { hits.hits.1.fields.field_string.0: "127.0.0.1" }
  - match: { hits.hits.2.fields.field.0: "10.1.2.3" }
  - match: { hits.hits.2.fields.field_string.0: "10.1.2.3" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('ip').get(1, new IPAddress('127.0.0.1'))"
            field_string:
              script:
                source: "field('ip').asString(1, '127.0.0.1')"
  - match: { hits.hits.0.fields.field.0: "127.0.0.1" }
  - match: { hits.hits.0.fields.field_string.0: "127.0.0.1" }
  - match: { hits.hits.1.fields.field.0: "127.0.0.1" }
  - match: { hits.hits.1.fields.field_string.0: "127.0.0.1" }
  - match: { hits.hits.2.fields.field.0: "2001:db8::2:1" }
  - match: { hits.hits.2.fields.field_string.0: "2001:db8::2:1" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String.join(',', field('ip').asStrings())"
  - match: { hits.hits.0.fields.field.0: "192.168.0.19" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "10.1.2.3,2001:db8::2:1" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          runtime_mappings:
            ip_script_field:
              type: ip
              script:
                source: "for (IPAddress addr : field('ip')) { String ip = addr.toString(); emit(ip.substring(0, ip.length() - 1) + field('ip').size()) }"
          script_fields:
            field:
              script:
                source: "field('ip_script_field').get(new IPAddress('1.2.3.4'))"
  - match: { hits.hits.0.fields.field.0: "192.168.0.11" }
  - match: { hits.hits.1.fields.field.0: "1.2.3.4" }
  - match: { hits.hits.2.fields.field.0: "10.1.2.2" }

---
keyword:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword'].get(0)"
  - match: { hits.hits.0.fields.field.0: "not split at all" }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword'].value"
  - match: { hits.hits.0.fields.field.0: "not split at all" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword').get('missing')"
  - match: { hits.hits.0.fields.field.0: "not split at all" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "another string" } # doc values are sorted

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('keyword', 'missing')"
  # same as `field('keyword').get('missing')`
  - match: { hits.hits.0.fields.field.0: "not split at all" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "another string" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword').get(1, 'dne')"
  - match: { hits.hits.0.fields.field.0: "dne" }
  - match: { hits.hits.1.fields.field.0: "dne" }
  - match: { hits.hits.2.fields.field.0: "one string" } # doc values are sorted

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String.join(', ', field('keyword'))"
  - match: { hits.hits.0.fields.field.0: "not split at all" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "another string, one string" }

---
keyword_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword_no_doc_values').get('missing')"
  - match: { hits.hits.0.fields.field.0: "no doc values" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "no doc values 0" } # doc values are sorted

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('keyword_no_doc_values', 'missing')"
  # same as `field('keyword').get('missing')`
  - match: { hits.hits.0.fields.field.0: "no doc values" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "no doc values 0" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword_no_doc_values').get(1, 'dne')"
  - match: { hits.hits.0.fields.field.0: "dne" }
  - match: { hits.hits.1.fields.field.0: "dne" }
  - match: { hits.hits.2.fields.field.0: "no doc values 1" } # doc values are sorted

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String.join(', ', field('keyword_no_doc_values'))"
  - match: { hits.hits.0.fields.field.0: "no doc values" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "no doc values 0, no doc values 1, no doc values 2" }

---
keyword_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword'].get(0)"
  - match: { hits.hits.0.fields.field.0: "not split at all" }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword'].value"
  - match: { hits.hits.0.fields.field.0: "not split at all" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword').get('missing')"
  - match: { hits.hits.0.fields.field.0: "not split at all" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "another string" } # doc values are sorted

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('keyword', 'missing')"
  # same as `field('keyword').get('missing')`
  - match: { hits.hits.0.fields.field.0: "not split at all" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "another string" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword').get(1, 'dne')"
  - match: { hits.hits.0.fields.field.0: "dne" }
  - match: { hits.hits.1.fields.field.0: "dne" }
  - match: { hits.hits.2.fields.field.0: "one string" } # doc values are sorted

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String.join(', ', field('keyword'))"
  - match: { hits.hits.0.fields.field.0: "not split at all" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "another string, one string" }

---
keyword_stored_synthetic:
  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword_stored'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['keyword_stored'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword_stored').get('missing')"
  - match: { hits.hits.0.fields.field.0: "no doc values" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "no doc values 0" } # doc values are sorted

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('keyword_stored', 'missing')"
  # same as `field('keyword').get('missing')`
  - match: { hits.hits.0.fields.field.0: "no doc values" }
  - match: { hits.hits.1.fields.field.0: "missing" }
  - match: { hits.hits.2.fields.field.0: "no doc values 0" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('keyword_stored').get(1, 'dne')"
  - match: { hits.hits.0.fields.field.0: "dne" }
  - match: { hits.hits.1.fields.field.0: "dne" }
  - match: { hits.hits.2.fields.field.0: "no doc values 1" } # doc values are sorted

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String.join(', ', field('keyword_stored'))"
  - match: { hits.hits.0.fields.field.0: "no doc values" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "no doc values 0, no doc values 1, no doc values 2" }

---
long:
  - skip:
      features: "warnings"

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['long'].get(0)"
  - match: { hits.hits.0.fields.field.0: 12348732141234 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['long'].value"
  - match: { hits.hits.0.fields.field.0: 12348732141234 }

---
long_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['long_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['long_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('long_no_doc_values').get(-1)"
  - match: { hits.hits.0.fields.field.0: 12348732141234 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: -1152921504606846976 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('long_no_doc_values', -1)"
  - match: { hits.hits.0.fields.field.0: 12348732141234 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: -1152921504606846976 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('long_no_doc_values').get(2, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 1152921504606846976 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "long total = 0; for (long i : field('long_no_doc_values')) { total += i; } total + field('long_no_doc_values').size();"
  - match: { hits.hits.0.fields.field.0: 12348732141235 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 576460752303423491 }

---
long_synthetic:
  - skip:
      features: "warnings"

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['long'].get(0)"
  - match: { hits.hits.0.fields.field.0: 12348732141234 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['long'].value"
  - match: { hits.hits.0.fields.field.0: 12348732141234 }

---
integer:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['integer'].get(0)"
  - match: { hits.hits.0.fields.field.0: 134134566 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['integer'].value"
  - match: { hits.hits.0.fields.field.0: 134134566 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('integer').get(-1)"
  - match: { hits.hits.0.fields.field.0: 134134566 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('integer', -1)"
  - match: { hits.hits.0.fields.field.0: 134134566 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('integer').get(1, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 17 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (int i : field('integer')) { total += i; } total + field('integer').size();"
  - match: { hits.hits.0.fields.field.0: 134134567 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 54 }

---
integer_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['integer_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['integer_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('integer_no_doc_values').get(-1)"
  - match: { hits.hits.0.fields.field.0: 134134566 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('integer_no_doc_values', -1)"
  - match: { hits.hits.0.fields.field.0: 134134566 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('integer_no_doc_values').get(1, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 17 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (int i : field('integer_no_doc_values')) { total += i; } total + field('integer_no_doc_values').size();"
  - match: { hits.hits.0.fields.field.0: 134134567 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 54 }

---
integer_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['integer'].get(0)"
  - match: { hits.hits.0.fields.field.0: 134134566 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['integer'].value"
  - match: { hits.hits.0.fields.field.0: 134134566 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('integer').get(-1)"
  - match: { hits.hits.0.fields.field.0: 134134566 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('integer', -1)"
  - match: { hits.hits.0.fields.field.0: 134134566 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('integer').get(1, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 17 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (int i : field('integer')) { total += i; } total + field('integer').size();"
  - match: { hits.hits.0.fields.field.0: 134134567 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 54 }

---
short:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['short'].get(0)"
  - match: { hits.hits.0.fields.field.0: 1324 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['short'].value"
  - match: { hits.hits.0.fields.field.0: 1324 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('short').get(-1)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('short', -1)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -1; field('short').get(defaultShort)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -1; $('short', defaultShort)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('short').get(1, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 18 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -3; field('short').get(1, defaultShort)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 18 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (short s : field('short')) { total += s; } total + field('short').size();"
  - match: { hits.hits.0.fields.field.0: 1325 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 103 }

---
short_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['short_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['short_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('short_no_doc_values').get(-1)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 6 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('short_no_doc_values', -1)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 6 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -1; field('short_no_doc_values').get(defaultShort)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 6 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -1; $('short_no_doc_values', defaultShort)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 6 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('short_no_doc_values').get(1, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 18 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -3; field('short_no_doc_values').get(1, defaultShort)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 18 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (short s : field('short_no_doc_values')) { total += s; } total + field('short_no_doc_values').size();"
  - match: { hits.hits.0.fields.field.0: 1325 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 103 }

---
short_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['short'].get(0)"
  - match: { hits.hits.0.fields.field.0: 1324 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['short'].value"
  - match: { hits.hits.0.fields.field.0: 1324 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('short').get(-1)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('short', -1)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -1; field('short').get(defaultShort)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -1; $('short', defaultShort)"
  - match: { hits.hits.0.fields.field.0: 1324 }
  - match: { hits.hits.1.fields.field.0: -1 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('short').get(1, -3)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 18 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "short defaultShort = -3; field('short').get(1, defaultShort)"
  - match: { hits.hits.0.fields.field.0: -3 }
  - match: { hits.hits.1.fields.field.0: -3 }
  - match: { hits.hits.2.fields.field.0: 18 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (short s : field('short')) { total += s; } total + field('short').size();"
  - match: { hits.hits.0.fields.field.0: 1325 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 103 }

---
byte:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['byte'].get(0)"
  - match: { hits.hits.0.fields.field.0: 12 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['byte'].value"
  - match: { hits.hits.0.fields.field.0: 12 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get((byte) 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('byte', (byte) 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 5; field('byte').get(defaultByte)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 5; $('byte', defaultByte)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get(5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('byte', 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get(1, (byte) 7)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 7; field('byte').get(1, defaultByte)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get(1, 7)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (byte s : field('byte')) { total += s; } total + field('byte').size();"
  - match: { hits.hits.0.fields.field.0: 13 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 129 }

---
byte_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['byte_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['byte_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte_no_doc_values').get((byte) 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('byte_no_doc_values', (byte) 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 5; field('byte_no_doc_values').get(defaultByte)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 5; $('byte_no_doc_values', defaultByte)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte_no_doc_values').get(5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('byte_no_doc_values', 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte_no_doc_values').get(1, (byte) 7)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 7; field('byte_no_doc_values').get(1, defaultByte)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte_no_doc_values').get(1, 7)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (byte s : field('byte_no_doc_values')) { total += s; } total + field('byte_no_doc_values').size();"
  - match: { hits.hits.0.fields.field.0: 13 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 129 }

---
byte_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['byte'].get(0)"
  - match: { hits.hits.0.fields.field.0: 12 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['byte'].value"
  - match: { hits.hits.0.fields.field.0: 12 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get((byte) 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('byte', (byte) 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 5; field('byte').get(defaultByte)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 5; $('byte', defaultByte)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get(5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('byte', 5)"
  - match: { hits.hits.0.fields.field.0: 12 }
  - match: { hits.hits.1.fields.field.0: 5 }
  - match: { hits.hits.2.fields.field.0: 4 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get(1, (byte) 7)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "byte defaultByte = 7; field('byte').get(1, defaultByte)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('byte').get(1, 7)"
  - match: { hits.hits.0.fields.field.0: 7 }
  - match: { hits.hits.1.fields.field.0: 7 }
  - match: { hits.hits.2.fields.field.0: 8 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "int total = 0; for (byte s : field('byte')) { total += s; } total + field('byte').size();"
  - match: { hits.hits.0.fields.field.0: 13 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 129 }

---
double:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['double'].get(0)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['double'].value"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('double').get(-1)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 2.141592653587 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('double', -1)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 2.141592653587 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "double defaultDouble = 7.8; field('double').get(1, defaultDouble)"
  - match: { hits.hits.0.fields.field.0: 7.8 }
  - match: { hits.hits.1.fields.field.0: 7.8 }
  - match: { hits.hits.2.fields.field.0: 3.141592653588 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('double').get(1, 9.2)"
  - match: { hits.hits.0.fields.field.0: 9.2 }
  - match: { hits.hits.1.fields.field.0: 9.2 }
  - match: { hits.hits.2.fields.field.0: 3.141592653588 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "double total = 0; for (double d : field('double')) { total += d; } total + field('double').size();"
  - match: { hits.hits.0.fields.field.0: 4.14159265358979 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 7.283185307175 }

---
double_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['double_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['double_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('double_no_doc_values').get(-1)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 2.141592653587 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('double_no_doc_values', -1)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 2.141592653587 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "double defaultDouble = 7.8; field('double_no_doc_values').get(1, defaultDouble)"
  - match: { hits.hits.0.fields.field.0: 7.8 }
  - match: { hits.hits.1.fields.field.0: 7.8 }
  - match: { hits.hits.2.fields.field.0: 3.141592653588 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('double_no_doc_values').get(1, 9.2)"
  - match: { hits.hits.0.fields.field.0: 9.2 }
  - match: { hits.hits.1.fields.field.0: 9.2 }
  - match: { hits.hits.2.fields.field.0: 3.141592653588 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "double total = 0; for (double d : field('double_no_doc_values')) { total += d; } total + field('double_no_doc_values').size();"
  - match: { hits.hits.0.fields.field.0: 4.14159265358979 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 7.283185307175 }

---
double_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['double'].get(0)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['double'].value"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('double').get(-1)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 2.141592653587 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('double', -1)"
  - match: { hits.hits.0.fields.field.0: 3.14159265358979 }
  - match: { hits.hits.1.fields.field.0: -1 }
  - match: { hits.hits.2.fields.field.0: 2.141592653587 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "double defaultDouble = 7.8; field('double').get(1, defaultDouble)"
  - match: { hits.hits.0.fields.field.0: 7.8 }
  - match: { hits.hits.1.fields.field.0: 7.8 }
  - match: { hits.hits.2.fields.field.0: 3.141592653588 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('double').get(1, 9.2)"
  - match: { hits.hits.0.fields.field.0: 9.2 }
  - match: { hits.hits.1.fields.field.0: 9.2 }
  - match: { hits.hits.2.fields.field.0: 3.141592653588 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "double total = 0; for (double d : field('double')) { total += d; } total + field('double').size();"
  - match: { hits.hits.0.fields.field.0: 4.14159265358979 }
  - match: { hits.hits.1.fields.field.0: 0 }
  - match: { hits.hits.2.fields.field.0: 7.283185307175 }

---
float:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float'].get(0)"
  - match: { hits.hits.0.fields.field.0: 3.1415927410125732 } # this ends up as a double

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float'].value"
  - match: { hits.hits.0.fields.field.0: 3.1415927410125732 } # this ends up as a double

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('float').get(-1).toString()" # toString to avoid making this a double
  - match: { hits.hits.0.fields.field.0: "3.1415927" }
  - match: { hits.hits.1.fields.field.0: "-1.0" }
  - match: { hits.hits.2.fields.field.0: "1.123" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "return $('float', -1).toString()" # toString to avoid making this a double
  - match: { hits.hits.0.fields.field.0: "3.1415927" }
  - match: { hits.hits.1.fields.field.0: "-1.0" }
  - match: { hits.hits.2.fields.field.0: "1.123" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "float defaultFloat = 7.8f; field('float').get(1, defaultFloat).toString()"
  - match: { hits.hits.0.fields.field.0: "7.8" }
  - match: { hits.hits.1.fields.field.0: "7.8" }
  - match: { hits.hits.2.fields.field.0: "2.234" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('float').get(1, 9.2f).toString()"
  - match: { hits.hits.0.fields.field.0: "9.2" }
  - match: { hits.hits.1.fields.field.0: "9.2" }
  - match: { hits.hits.2.fields.field.0: "2.234" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "float total = 0; for (float f : field('float')) { total += f; } Float.toString(total + field('float').size());"
  - match: { hits.hits.0.fields.field.0: "4.141593" }
  - match: { hits.hits.1.fields.field.0: "0.0" }
  - match: { hits.hits.2.fields.field.0: "5.357" }

---
float_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('float_no_doc_values').get(-1).toString()" # toString to avoid making this a double
  - match: { hits.hits.0.fields.field.0: "3.1415927" }
  - match: { hits.hits.1.fields.field.0: "-1.0" }
  - match: { hits.hits.2.fields.field.0: "1.123" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "return $('float_no_doc_values', -1).toString()" # toString to avoid making this a double
  - match: { hits.hits.0.fields.field.0: "3.1415927" }
  - match: { hits.hits.1.fields.field.0: "-1.0" }
  - match: { hits.hits.2.fields.field.0: "1.123" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "float defaultFloat = 7.8f; field('float_no_doc_values').get(1, defaultFloat).toString()"
  - match: { hits.hits.0.fields.field.0: "7.8" }
  - match: { hits.hits.1.fields.field.0: "7.8" }
  - match: { hits.hits.2.fields.field.0: "2.234" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('float_no_doc_values').get(1, 9.2f).toString()"
  - match: { hits.hits.0.fields.field.0: "9.2" }
  - match: { hits.hits.1.fields.field.0: "9.2" }
  - match: { hits.hits.2.fields.field.0: "2.234" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "float total = 0; for (float f : field('float_no_doc_values')) { total += f; } Float.toString(total + field('float_no_doc_values').size());"
  - match: { hits.hits.0.fields.field.0: "4.141593" }
  - match: { hits.hits.1.fields.field.0: "0.0" }
  - match: { hits.hits.2.fields.field.0: "5.357" }

---
float_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float'].get(0)"
  - match: { hits.hits.0.fields.field.0: 3.1415927410125732 } # this ends up as a double

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float'].value"
  - match: { hits.hits.0.fields.field.0: 3.1415927410125732 } # this ends up as a double

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('float').get(-1).toString()" # toString to avoid making this a double
  - match: { hits.hits.0.fields.field.0: "3.1415927" }
  - match: { hits.hits.1.fields.field.0: "-1.0" }
  - match: { hits.hits.2.fields.field.0: "1.123" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "return $('float', -1).toString()" # toString to avoid making this a double
  - match: { hits.hits.0.fields.field.0: "3.1415927" }
  - match: { hits.hits.1.fields.field.0: "-1.0" }
  - match: { hits.hits.2.fields.field.0: "1.123" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "float defaultFloat = 7.8f; field('float').get(1, defaultFloat).toString()"
  - match: { hits.hits.0.fields.field.0: "7.8" }
  - match: { hits.hits.1.fields.field.0: "7.8" }
  - match: { hits.hits.2.fields.field.0: "2.234" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('float').get(1, 9.2f).toString()"
  - match: { hits.hits.0.fields.field.0: "9.2" }
  - match: { hits.hits.1.fields.field.0: "9.2" }
  - match: { hits.hits.2.fields.field.0: "2.234" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "float total = 0; for (float f : field('float')) { total += f; } Float.toString(total + field('float').size());"
  - match: { hits.hits.0.fields.field.0: "4.141593" }
  - match: { hits.hits.1.fields.field.0: "0.0" }
  - match: { hits.hits.2.fields.field.0: "5.357" }

---
half_float:
  - skip:
      features: close_to

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['half_float'].get(0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['half_float'].value"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').get(0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('half_float', 0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').get(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 2.234, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').asDouble(0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.0001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').asDouble(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 2.234, error: 0.001 } }

---
half_float_no_doc_values:
  - skip:
      features: close_to

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['half_float_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['float_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float_no_doc_values').get(0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('half_float_no_doc_values', 0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float_no_doc_values').get(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 2.234, error: 0.001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float_no_doc_values').asDouble(0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.0001 } }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float_no_doc_values').asDouble(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 2.234, error: 0.001 } }

---
half_float_synthetic:
  - skip:
      features: close_to

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['half_float'].get(0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['half_float'].value"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').get(0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.001 } }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('half_float', 0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.001 } }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').get(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 2.234, error: 0.001 } }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').asDouble(0.0)"
  - close_to: { hits.hits.0.fields.field.0: { value: 3.140625, error: 0.001 } }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 1.123, error: 0.0001 } }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('half_float').asDouble(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - close_to: { hits.hits.2.fields.field.0: { value: 2.234, error: 0.001 } }

---
scaled_float:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['scaled_float'].get(0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('scaled_float').get(0.0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: -3.5 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('scaled_float', 0.0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: -3.5 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('scaled_float').get(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: 2.5 }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['scaled_float'].value"
  - match: { hits.hits.0.fields.field.0: 3.14 }

---
scaled_float_no_doc_values:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['scaled_float_no_doc_values'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['scaled_float_no_doc_values'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('scaled_float_no_doc_values', 0.0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: -3.5 }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('scaled_float_no_doc_values').get(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: 2.5 }

---
scaled_float_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['scaled_float'].get(0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('scaled_float').get(0.0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: -3.5 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('scaled_float', 0.0)"
  - match: { hits.hits.0.fields.field.0: 3.14 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: -3.5 }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('scaled_float').get(1, 0.0)"
  - match: { hits.hits.0.fields.field.0: 0.0 }
  - match: { hits.hits.1.fields.field.0: 0.0 }
  - match: { hits.hits.2.fields.field.0: 2.5 }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['scaled_float'].value"
  - match: { hits.hits.0.fields.field.0: 3.14 }

---
token_count:
  - do:
      search:
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['token_count'].get(0)"
  - match: { hits.hits.0.fields.field.0: 5 }

  - do:
      search:
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['token_count'].value"
  - match: { hits.hits.0.fields.field.0: 5 }

---
empty:
  - do:
      search:
        body:
          script_fields:
            field:
              script:
                source: "int value = field('dne').get(1); value"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      search:
        body:
          script_fields:
            field:
              script:
                source: "int value = $('dne', 1); value"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      search:
        body:
          script_fields:
            field:
              script:
                source: "int value = field('dne').get(1, 1); value"
  - match: { hits.hits.0.fields.field.0: 1 }

---
text:
  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text'].get(0)"
  - match: { hits.hits.0.fields.field.0: lots }

  - do:
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text'].value"
  - match: { hits.hits.0.fields.field.0: lots }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('text', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('text', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('text')) { cat += s; } cat + field('text').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
text_no_field_data:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text_no_field_data'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text_no_field_data'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_no_field_data').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('text_no_field_data', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text_no_field_data').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('text_no_field_data', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_no_field_data').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text_no_field_data').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_no_field_data').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('text_no_field_data')) { cat += s; } cat + field('text_no_field_data').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
text_synthetic:
  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text'].get(0)"
  - match: { hits.hits.0.fields.field.0: lots }

  - do:
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text'].value"
  - match: { hits.hits.0.fields.field.0: lots }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('text', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('text', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('text')) { cat += s; } cat + field('text').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
text_sub_stored_keyword_synthetic:
  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text_sub_stored_keyword'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text_sub_stored_keyword'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_sub_stored_keyword').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('text_sub_stored_keyword', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text_sub_stored_keyword').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('text_sub_stored_keyword', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_sub_stored_keyword').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text_sub_stored_keyword').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_sub_stored_keyword').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('text_sub_stored_keyword')) { cat += s; } cat + field('text_sub_stored_keyword').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
text_stored_synthetic:
  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text_stored'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['text_stored'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_stored').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('text_stored', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text_stored').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('text_stored', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_stored').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('text_stored').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('text_stored').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('text_stored')) { cat += s; } cat + field('text_stored').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
match_only_text:
  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['match_only_text'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['match_only_text'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('match_only_text').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('match_only_text', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('match_only_text').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('match_only_text', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('match_only_text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('match_only_text').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('match_only_text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('match_only_text')) { cat += s; } cat + field('match_only_text').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
match_only_text_synthetic:
  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['match_only_text'].get(0)"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      catch: bad_request
      search:
        index: test_synthetic
        body:
          query: { term: { _id: "1" } }
          script_fields:
            field:
              script:
                source: "doc['match_only_text'].value"
  - match: { error.failed_shards.0.reason.caused_by.type: "illegal_argument_exception" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('match_only_text').get('')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "/* avoid yaml stash */ $('match_only_text', '')"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('match_only_text').get(defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; $('match_only_text', defaultText)"
  - match: { hits.hits.0.fields.field.0: "Lots of text." }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "Lots of text." }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('match_only_text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String defaultText = 'default text'; field('match_only_text').get(1, defaultText)"
  - match: { hits.hits.0.fields.field.0: "default text" }
  - match: { hits.hits.1.fields.field.0: "default text" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "field('match_only_text').get(1, '')"
  - match: { hits.hits.0.fields.field.0: "" }
  - match: { hits.hits.1.fields.field.0: "" }
  - match: { hits.hits.2.fields.field.0: "SOOOOO much text" }

  - do:
      search:
        index: test_synthetic
        body:
          sort: [ { rank: asc } ]
          script_fields:
            field:
              script:
                source: "String cat = ''; for (String s : field('match_only_text')) { cat += s; } cat + field('match_only_text').size();"
  - match: { hits.hits.0.fields.field.0: "Lots of text.1" }
  - match: { hits.hits.1.fields.field.0: "0" }
  - match: { hits.hits.2.fields.field.0: "Lots of text.SOOOOO much texteven more text3" }

---
version and sequence number:
  - do:
      indices.create:
        index: versiontest
        body:
          settings:
            number_of_shards: 1
          mappings:
            properties:
              keyword:
                type: keyword

  - do:
      index:
        index: versiontest
        id: "3000"
        version: 50
        version_type: external
        body:
          keyword: "3k"

  - do:
      indices.refresh: { }

  - do:
      search:
        index: versiontest
        body:
          query: { term: { _id: "3000" } }
          script_fields:
            ver:
              script:
                source: "field('_version').get(10000)"
            seq:
              script:
                source: "field('_seq_no').get(10000)"
  - match: { hits.hits.0.fields.ver.0: 50 }
  - match: { hits.hits.0.fields.seq.0: 0 }

  - do:
      index:
        index: versiontest
        id: "3000"
        version: 60
        version_type: external
        body:
          keyword: "3k+1"
  - do:
      indices.refresh: { }

  - do:
      catch: conflict
      index:
        index: versiontest
        id: "3000"
        version: 55
        version_type: external
        body:
          keyword: "3k+2"
  - do:
      indices.refresh: { }

  - do:
      search:
        index: versiontest
        body:
          query: { term: { _id: "3000" } }
          script_fields:
            ver:
              script:
                source: "field('_version').get(10000)"
            seq:
              script:
                source: "field('_seq_no').get(10000)"
  - match: { hits.hits.0.fields.ver.0: 60 }
  - match: { hits.hits.0.fields.seq.0: 1 }

---
version and sequence number synthetic _source:
  - do:
      indices.create:
        index: versiontest
        body:
          settings:
            number_of_shards: 1
            index:
              mapping.source.mode: synthetic
          mappings:
            properties:
              keyword:
                type: keyword

  - do:
      index:
        index: versiontest
        id: "3000"
        version: 50
        version_type: external
        body:
          keyword: "3k"

  - do:
      indices.refresh: { }

  - do:
      search:
        index: versiontest
        body:
          query: { term: { _id: "3000" } }
          script_fields:
            ver:
              script:
                source: "field('_version').get(10000)"
            seq:
              script:
                source: "field('_seq_no').get(10000)"
  - match: { hits.hits.0.fields.ver.0: 50 }
  - match: { hits.hits.0.fields.seq.0: 0 }

  - do:
      index:
        index: versiontest
        id: "3000"
        version: 60
        version_type: external
        body:
          keyword: "3k+1"
  - do:
      indices.refresh: { }

  - do:
      catch: conflict
      index:
        index: versiontest
        id: "3000"
        version: 55
        version_type: external
        body:
          keyword: "3k+2"
  - do:
      indices.refresh: { }

  - do:
      search:
        index: versiontest
        body:
          query: { term: { _id: "3000" } }
          script_fields:
            ver:
              script:
                source: "field('_version').get(10000)"
            seq:
              script:
                source: "field('_seq_no').get(10000)"
  - match: { hits.hits.0.fields.ver.0: 60 }
  - match: { hits.hits.0.fields.seq.0: 1 }

---
flattened fields api:
  - do:
      indices.create:
        index: flatindex
        body:
          settings:
            number_of_shards: 1
          mappings:
            properties:
              rank:
                type: integer
              flattened:
                type: flattened

  - do:
      index:
        index: flatindex
        id: 40
        body:
          rank: 1
          flattened:
            top: 123
            dict:
              abc: def
              hij: lmn
            list: [ true ]

  - do:
      index:
        index: flatindex
        id: 41
        body:
          rank: 2
          flattened:
            top2: 876
            dict2:
              abc: def
              opq: rst
              hij: lmn
              uvx: wyz
            list2: [ 789, 1011, 1213 ]

  - do:
      indices.refresh: { }

  - do:
      search:
        index: flatindex
        body:
          sort: [ { rank: asc } ]
          script_fields:
            f_root:
              script:
                source: "field('flattened').get('dne')"
            f_root_index:
              script:
                source: "field('flattened').get(2, 'dne')"
            f_top:
              script:
                source: "field('flattened.top').get('dne')"
            f_top2:
              script:
                source: "field('flattened.top2').get('dne')"
            f_dict:
              script:
                source: "field('flattened.dict.abc').get('dne')"
            f_dict2:
              script:
                source: "field('flattened.dict2.uvx').get('dne')"
            f_list:
              script:
                source: "field('flattened.list').get('dne')"
            f_list2:
              script:
                source: "field('flattened.list2').get(2, 'dne')"
            all:
              script:
                source: "String all = ''; for (String value : field('flattened')) { all += value } all"
  - match: { hits.hits.0.fields.f_root.0: "123" }
  - match: { hits.hits.0.fields.f_root_index.0: "lmn" }
  - match: { hits.hits.0.fields.f_top.0: "123" }
  - match: { hits.hits.0.fields.f_top2.0: "dne" }
  - match: { hits.hits.0.fields.f_dict.0: "def" }
  - match: { hits.hits.0.fields.f_dict2.0: "dne" }
  - match: { hits.hits.0.fields.f_list.0: "true" }
  - match: { hits.hits.0.fields.f_list2.0: "dne" }
  - match: { hits.hits.0.fields.all.0: "123deflmntrue" }
  - match: { hits.hits.1.fields.f_root.0: "1011" }
  - match: { hits.hits.1.fields.f_root_index.0: "789" }
  - match: { hits.hits.1.fields.f_top.0: "dne" }
  - match: { hits.hits.1.fields.f_top2.0: "876" }
  - match: { hits.hits.1.fields.f_dict.0: "dne" }
  - match: { hits.hits.1.fields.f_dict2.0: "wyz" }
  - match: { hits.hits.1.fields.f_list.0: "dne" }
  - match: { hits.hits.1.fields.f_list2.0: "789" }
  - match: { hits.hits.1.fields.all.0: "10111213789876deflmnrstwyz" }

---
unsupported date methods:
  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getCenturyOfEra()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getCenturyOfEra] is no longer available; use [get(ChronoField.YEAR_OF_ERA) / 100] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.YEAR_OF_ERA) / 100"
  - match: { hits.hits.0.fields.field.0: 20 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getEra()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getEra] is no longer available; use [get(ChronoField.ERA)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.ERA)"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getHourOfDay()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getHourOfDay] is no longer available; use [getHour()] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getHour()"
  - match: { hits.hits.0.fields.field.0: 12 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMillisOfDay()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getMillisOfDay] is no longer available; use [get(ChronoField.MILLI_OF_DAY)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.MILLI_OF_DAY)"
  - match: { hits.hits.0.fields.field.0: 43872000 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMillisOfSecond()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getMillisOfSecond] is no longer available; use [get(ChronoField.MILLI_OF_SECOND)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.MILLI_OF_SECOND)"
  - match: { hits.hits.0.fields.field.0: 0 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMinuteOfDay()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getMinuteOfDay] is no longer available; use [get(ChronoField.MINUTE_OF_DAY)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.MINUTE_OF_DAY)"
  - match: { hits.hits.0.fields.field.0: 731 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMinuteOfHour()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getMinuteOfHour] is no longer available; use [getMinute()] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMinute()"
  - match: { hits.hits.0.fields.field.0: 11 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMonthOfYear()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getMonthOfYear] is no longer available; use [getMonthValue()] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getMonthValue()"
  - match: { hits.hits.0.fields.field.0: 1 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getSecondOfDay()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getSecondOfDay] is no longer available; use [get(ChronoField.SECOND_OF_DAY)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.SECOND_OF_DAY)"
  - match: { hits.hits.0.fields.field.0: 43872 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getSecondOfMinute()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getSecondOfMinute] is no longer available; use [getSecond()] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getSecond()"
  - match: { hits.hits.0.fields.field.0: 12 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getWeekOfWeekyear()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getWeekOfWeekyear] is no longer available; use [get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)"
  - match: { hits.hits.0.fields.field.0: 52 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getWeekyear()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getWeekyear] is no longer available; use [get(IsoFields.WEEK_BASED_YEAR)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(IsoFields.WEEK_BASED_YEAR)"
  - match: { hits.hits.0.fields.field.0: 2016 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getYearOfCentury()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getYearOfCentury] is no longer available; use [get(ChronoField.YEAR_OF_ERA) % 100] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.YEAR_OF_ERA) % 100"
  - match: { hits.hits.0.fields.field.0: 17 }

  - do:
      catch: bad_request
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).getYearOfEra()"
  - match: { error.failed_shards.0.reason.caused_by.type: "unsupported_operation_exception" }
  - match: { error.failed_shards.0.reason.caused_by.reason:
               "[getYearOfEra] is no longer available; use [get(ChronoField.YEAR_OF_ERA)] instead" }

  - do:
      search:
        body:
          query: { term: { _id: 1 } }
          script_fields:
            field:
              script:
                source: "/* avoid stash */ $('date', null).get(ChronoField.YEAR_OF_ERA)"
  - match: { hits.hits.0.fields.field.0: 2017 }

