---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    test_runner_features: "headers"
    cluster_features:
    - "gte_v7.10.1"
    reason: "version field is added to 7.10"
- do:
    indices.create:
      index: "test_index"
      body:
        settings:
          number_of_shards: 1
        mappings:
          properties:
            version:
              type: "version"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    bulk:
      refresh: true
      body:
      - "{ \"index\" : { \"_index\" : \"test_index\", \"_id\" : \"1\" } }"
      - "{\"version\": \"1.1.12\" }"
      - "{ \"index\" : { \"_index\" : \"test_index\", \"_id\" : \"2\" } }"
      - "{\"version\": \"2.0.0-beta\" }"
      - "{ \"index\" : { \"_index\" : \"test_index\", \"_id\" : \"3\" } }"
      - "{\"version\": \"3.1.0\" }"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Filter script:
- do:
    search:
      index: "test_index"
      body:
        query:
          script:
            script:
              source: "doc['version'].value.length() > 5"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 2
- match:
    hits.hits.0._source.version: "1.1.12"
- match:
    hits.hits.1._source.version: "2.0.0-beta"
---
Sort script:
- do:
    search:
      index: "test_index"
      body:
        sort:
          _script:
            type: "number"
            script:
              source: "doc['version'].value.length()"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 3
- match:
    hits.hits.0._source.version: "3.1.0"
- match:
    hits.hits.1._source.version: "1.1.12"
- match:
    hits.hits.2._source.version: "2.0.0-beta"
---
Sort script returning version value:
- requires:
    cluster_features:
    - "gte_v8.3.0"
    reason: "order by script returning version supported in v 8.3"
- do:
    search:
      index: "test_index"
      body:
        sort:
          _script:
            type: "version"
            script:
              source: "doc['version'].value"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 3
- match:
    hits.hits.0._source.version: "1.1.12"
- match:
    hits.hits.1._source.version: "2.0.0-beta"
- match:
    hits.hits.2._source.version: "3.1.0"
---
Sort script with field() returning version value:
- requires:
    cluster_features:
    - "gte_v8.3.0"
    reason: "order by script returning version supported in v 8.3"
- do:
    search:
      index: "test_index"
      body:
        sort:
          _script:
            type: "version"
            script:
              source: "field('version').get(new Version('0'))"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 3
- match:
    hits.hits.0._source.version: "1.1.12"
- match:
    hits.hits.1._source.version: "2.0.0-beta"
- match:
    hits.hits.2._source.version: "3.1.0"
