setup:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: _tsid introduced in 8.1.0

  - do:
      indices.create:
        index: test
        body:
          settings:
            index:
              mode: time_series
              routing_path: [metricset]
              time_series:
                start_time: 2021-04-28T00:00:00Z
                end_time: 2021-04-29T00:00:00Z
          mappings:
            properties:
              "@timestamp":
                type: date
              metricset:
                type: keyword
                time_series_dimension: true
              ul:
                type: unsigned_long
                time_series_dimension: true

  - do:
      bulk:
        refresh: true
        index: test
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:24.467Z", "metricset": "aa", "voltage": 7.2, "ul": 9223372036854775807}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:34.467Z", "metricset": "aa", "voltage": 7.6, "ul": 9223372036854775807}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:44.467Z", "metricset": "aa", "voltage": 7.1, "ul": 18446744073709551614}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:54.467Z", "metricset": "aa", "voltage": 7.3, "ul": 18446744073709551615}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:24.467Z", "metricset": "aa", "voltage": 3.2, "ul": 9223372036854775808}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:34.467Z", "metricset": "aa", "voltage": 3.6, "ul": 9223372036854775808}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:44.467Z", "metricset": "aa", "voltage": 3.1, "ul": 9223372036854775808}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:54.467Z", "metricset": "aa", "voltage": 3.3, "ul": 9223372036854775807}'

---
fetch the _tsid:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: _tsid support introduced in 8.1.0

  - do:
      search:
        index: test
        body:
          fields: [_tsid, metricset, ul]
          query:
            query_string:
              query: '+@timestamp:"2021-04-28T18:35:24.467Z" +metricset:aa'
          sort:   [ _tsid ]

  - match: {hits.total.value: 2}
  - match: {hits.hits.0.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62ccUzlcoJwx6Ltf34ddg5heNyQIA"]}
  - match: {hits.hits.0.fields.metricset: [aa]}
  - match: {hits.hits.0.fields.ul: [9223372036854775807]}
  - match: {hits.hits.1.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U"]}
  - match: {hits.hits.1.fields.metricset: [aa]}
  - match: {hits.hits.1.fields.ul: [9223372036854775808]}

---
fetch the _source:
  - requires:
      cluster_features: ["gte_v8.7.0"]
      reason: synthetic source support to unsigned long added in 8.7.0

  - do:
      search:
        index: test
        body:
          fields: [_tsid, metricset, ul]
          query:
            query_string:
              query: '+@timestamp:"2021-04-28T18:35:24.467Z" +metricset:aa'
          sort:   [ _tsid ]

  - match: {hits.total.value: 2}
  - match: {hits.hits.0._source.voltage: 7.2}
  - match: {hits.hits.0._source.metricset: aa}
  - match: {hits.hits.0._source.@timestamp: 2021-04-28T18:35:24.467Z}
  - match: {hits.hits.0._source.ul: 9223372036854775807}
  - match: {hits.hits.1._source.voltage: 3.2}
  - match: {hits.hits.1._source.metricset: aa}
  - match: {hits.hits.1._source.@timestamp: 2021-04-28T18:35:24.467Z}
  - match: {hits.hits.1._source.ul: 9223372036854775808}

---
# Sort order is of unsigned_long fields is not the one we would expect.
# This is caused by the encoding of unsigned_long as a signed long before
# being serialized in _tsid
aggregate the _tsid:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: _tsid support introduced in 8.1.0

  - do:
      search:
        index: test
        body:
          size: 0
          aggs:
            tsids:
              terms:
                field: _tsid
                order:
                  _key: asc

  - match: {hits.total.value: 8}
  - match: {aggregations.tsids.buckets.0.key: "KEgPmt8JTKe7WA6iB8FLYKbvei62VWzfUKafNxTRdD8v6_Z8PbKJ6TQ"}
  - match: {aggregations.tsids.buckets.0.doc_count: 1}
  - match: {aggregations.tsids.buckets.1.key: "KEgPmt8JTKe7WA6iB8FLYKbvei62ccUzlcoJwx6Ltf34ddg5heNyQIA"}
  - match: {aggregations.tsids.buckets.1.doc_count: 3}
  - match: {aggregations.tsids.buckets.2.key: "KEgPmt8JTKe7WA6iB8FLYKbvei62erJQlP1_gz9tjb7S743_9tpXyfM"}
  - match: {aggregations.tsids.buckets.2.doc_count: 1}
  - match: {aggregations.tsids.buckets.3.key: "KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U"}
  - match: {aggregations.tsids.buckets.3.doc_count: 3}


---
# Sort order is of unsigned_long fields is not the one we would expect.
# This is caused by the encoding of unsigned_long as a signed long before
# being serialized in _tsid
sort by tsid:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: _tsid support introduced in 8.1.0

  - do:
      search:
        index: test
        body:
          fields: [ _tsid ]
          sort:   [ _tsid ]

  - match: {hits.total.value: 8 }

  - match: {hits.hits.0.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62VWzfUKafNxTRdD8v6_Z8PbKJ6TQ"]}
  - match: {hits.hits.1.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62ccUzlcoJwx6Ltf34ddg5heNyQIA"]}
  - match: {hits.hits.2.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62ccUzlcoJwx6Ltf34ddg5heNyQIA"]}
  - match: {hits.hits.3.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62ccUzlcoJwx6Ltf34ddg5heNyQIA"]}
  - match: {hits.hits.4.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62erJQlP1_gz9tjb7S743_9tpXyfM"]}
  - match: {hits.hits.5.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U"]}
  - match: {hits.hits.6.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U"]}
  - match: {hits.hits.7.fields._tsid: ["KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U"]}

---
composite aggregation on tsid:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: _tsid introduced in 8.1.0

  - do:
      search:
        index: test
        body:
          size: 0
          aggregations:
            tsids:
              composite:
                sources: [
                    "tsid": {
                      "terms": {
                        "field": "_tsid"
                      }
                    }
                ]

  - match:  { hits.total.value: 8 }
  - length: { aggregations.tsids.buckets: 4 }
  - match:  { aggregations.tsids.buckets.0.key.tsid: "KEgPmt8JTKe7WA6iB8FLYKbvei62VWzfUKafNxTRdD8v6_Z8PbKJ6TQ" }
  - match:  { aggregations.tsids.buckets.0.doc_count: 1 }
  - match:  { aggregations.tsids.buckets.1.key.tsid: "KEgPmt8JTKe7WA6iB8FLYKbvei62ccUzlcoJwx6Ltf34ddg5heNyQIA" }
  - match:  { aggregations.tsids.buckets.1.doc_count: 3 }
  - match:  { aggregations.tsids.buckets.2.key.tsid: "KEgPmt8JTKe7WA6iB8FLYKbvei62erJQlP1_gz9tjb7S743_9tpXyfM" }
  - match:  { aggregations.tsids.buckets.2.doc_count: 1 }
  - match:  { aggregations.tsids.buckets.3.key.tsid: "KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U" }
  - match:  { aggregations.tsids.buckets.3.doc_count: 3 }

  - match:  { aggregations.tsids.after_key.tsid: "KEgPmt8JTKe7WA6iB8FLYKbvei62xbkMpink-I2ckSWnx-vXF67HQ-U" }
