setup:
  - requires:
      cluster_features: ["gte_v7.10.0"]
      reason: "unsigned_long was added in 7.10"

  - do:
      indices.create:
        index: test_longs
        body:
          mappings:
            properties:
              my_counter:
                type: long

  - do:
      indices.create:
        index: test_unsigned_longs
        body:
          mappings:
            properties:
              my_counter:
                type: unsigned_long

  - do:
      bulk:
        index: test_longs
        refresh: true
        body: |
          { "index": {"_id" : "1"} }
          { "my_counter": 0 }
          { "index": {"_id" : "2"} }
          { "my_counter": 1000000 }
          { "index": {"_id" : "3"} }
          { "my_counter": 9223372036854775807 }

  - do:
      bulk:
        index: test_unsigned_longs
        refresh: true
        body: |
          { "index": {"_id" : "1"} }
          { "my_counter": 0 }
          { "index": {"_id" : "2"} }
          { "my_counter": 1000000 }
          { "index": {"_id" : "3"} }
          { "my_counter": 9223372036854775807 }
          { "index": {"_id" : "4"} }
          { "my_counter": 18446744073709551615 }


---
"Querying of different numeric types is supported":
  - do:
      search:
        index: test*
        body:
          size: 0
          query:
            range:
              my_counter:
                gte: 0
  - match: { "hits.total.value": 7 }

  - do:
      search:
        index: test*
        body:
          size: 0
          query:
            range:
              my_counter:
                gt: 0
                lt: 9223372036854775807
  - match: { "hits.total.value": 2 }

  - do:
      search:
        index: test*
        body:
          size: 0
          query:
            range:
              my_counter:
                gte: 9223372036854775807
  - match: { "hits.total.value": 3 }


---
"Aggregation of different numeric types is supported":
  # sum agg
  - do:
      search:
        index: test*
        body:
          size: 0
          aggs:
            my_counter_sum:
              sum:
                field: my_counter
  - match: { aggregations.my_counter_sum.value: 3.68934881474211E19 }

  # histogram agg
  - do:
      search:
        index: test*
        body:
          size: 0
          aggs:
            my_counter_histo:
              histogram:
                field: my_counter
                interval: 9223372036854775807
  - length: { aggregations.my_counter_histo.buckets: 3 }
  - match: { aggregations.my_counter_histo.buckets.0.key: 0.0 }
  - match: { aggregations.my_counter_histo.buckets.0.doc_count: 4 }
  - match: { aggregations.my_counter_histo.buckets.1.key: 9.223372036854776E18 }
  - match: { aggregations.my_counter_histo.buckets.1.doc_count: 2 }
  - match: { aggregations.my_counter_histo.buckets.2.key: 1.8446744073709552E19 }
  - match: { aggregations.my_counter_histo.buckets.2.doc_count: 1 }

  # terms agg bucket values are converted to double
  - do:
      search:
        index: test*
        body:
          size: 0
          aggs:
            my_counter_terms:
              terms:
                field: my_counter
  - length: { aggregations.my_counter_terms.buckets: 4 }
  - match: { aggregations.my_counter_terms.buckets.0.key: 0.0 }
  - match: { aggregations.my_counter_terms.buckets.0.doc_count: 2 }
  - match: { aggregations.my_counter_terms.buckets.1.key: 1000000.0 }
  - match: { aggregations.my_counter_terms.buckets.1.doc_count: 2 }
  - match: { aggregations.my_counter_terms.buckets.2.key: 9.223372036854776E18 }
  - match: { aggregations.my_counter_terms.buckets.2.doc_count: 2 }
  - match: { aggregations.my_counter_terms.buckets.3.key: 1.8446744073709552E19 }
  - match: { aggregations.my_counter_terms.buckets.3.doc_count: 1 }
