setup:

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

  - do:
      indices.create:
        index:  test1
        body:
          mappings:
            properties:
              ul:
                type: unsigned_long
                store: true

  - do:
      bulk:
        index: test1
        refresh: true
        body: |
          { "index": {"_id" : "1"} }
          { "ul": 0 }
          { "index": {"_id" : "2"} }
          { "ul": 9223372036854775807 }
          { "index": {"_id" : "3"} }
          { "ul": 9223372036854775808 }
          { "index": {"_id" : "4"} }
          { "ul": 18446744073709551614 }
          { "index": {"_id" : "5"} }
          { "ul": 18446744073709551615 }

---
"Exist query":

  - do:
      search:
        index: test1
        body:
          size: 0
          query:
            exists:
              field: ul

  - match: { "hits.total.value": 5 }


---
"Term query":

  - do:
      search:
        index: test1
        body:
          query:
            term:
              ul: 0
  - match: { "hits.total.value": 1 }
  - match: {hits.hits.0._id: "1" }

  - do:
      search:
        index: test1
        body:
          query:
            term:
              ul: 18446744073709551615
  - match: { "hits.total.value": 1 }
  - match: {hits.hits.0._id: "5" }

  - do:
      search:
        index: test1
        body:
          query:
            term:
              ul: 18446744073709551616
  - match: { "hits.total.value": 0 }

---
"Terms query":

  - do:
      search:
        index: test1
        body:
          size: 0
          query:
            terms:
              ul: [0, 9223372036854775808, 18446744073709551615]

  - match: { "hits.total.value": 3 }

---
"Range query":

  - do:
      search:
        index: test1
        body:
          size: 0
          query:
            range:
              ul:
                gte: 0
  - match: { "hits.total.value": 5 }

  - do:
      search:
        index: test1
        body:
          size: 0
          query:
            range:
              ul:
                gte: 0.5
  - match: { "hits.total.value": 4 }

  - do:
      search:
        index: test1
        body:
          size: 0
          query:
            range:
              ul:
                lte: 18446744073709551615
  - match: { "hits.total.value": 5 }

  - do:
      search:
        index: test1
        body:
          query:
            range:
              ul:
                lte: "18446744073709551614.5" # this must be string, as number gets converted to double with loss of precision
  - match: { "hits.total.value": 4 }

---
"Sort":

  - do:
      search:
        index: test1
        body:
          sort: [ { ul: asc } ]

  - match: { "hits.total.value": 5 }
  - match: {hits.hits.0._id: "1" }
  - match: {hits.hits.0.sort: [0] }
  - match: {hits.hits.1._id: "2" }
  - match: {hits.hits.1.sort: [9223372036854775807] }
  - match: {hits.hits.2._id: "3" }
  - match: {hits.hits.2.sort: [9223372036854775808] }
  - match: {hits.hits.3._id: "4" }
  - match: {hits.hits.3.sort: [18446744073709551614] }
  - match: {hits.hits.4._id: "5" }
  - match: {hits.hits.4.sort: [18446744073709551615] }

  - do:
      search:
        index: test1
        body:
          sort: [ { ul: asc } ]
          search_after: [9223372036854775808]

  - length: { hits.hits: 2 }
  - match: {hits.hits.0._id: "4" }
  - match: {hits.hits.0.sort: [18446744073709551614] }
  - match: {hits.hits.1._id: "5" }
  - match: {hits.hits.1.sort: [18446744073709551615] }

  - do:
      search:
        index: test1
        body:
          sort: [ { ul: desc } ]

  - match: { "hits.total.value": 5 }
  - match: {hits.hits.0._id: "5" }
  - match: {hits.hits.0.sort: [18446744073709551615] }
  - match: {hits.hits.1._id: "4" }
  - match: {hits.hits.1.sort: [18446744073709551614] }
  - match: {hits.hits.2._id: "3" }
  - match: {hits.hits.2.sort: [9223372036854775808] }
  - match: {hits.hits.3._id: "2" }
  - match: {hits.hits.3.sort: [9223372036854775807] }
  - match: {hits.hits.4._id: "1" }
  - match: {hits.hits.4.sort: [0] }

---
"Aggs":

  - do:
      search:
        index: test1
        body:
          size: 0
          aggs:
            ul_terms:
              terms:
                field: ul
  - length: { aggregations.ul_terms.buckets: 5 }
  - match: { aggregations.ul_terms.buckets.0.key: 0 }
  - match: { aggregations.ul_terms.buckets.1.key: 9223372036854775807 }
  - match: { aggregations.ul_terms.buckets.2.key: 9223372036854775808 }
  - match: { aggregations.ul_terms.buckets.3.key: 18446744073709551614 }
  - match: { aggregations.ul_terms.buckets.4.key: 18446744073709551615 }

  - do:
      search:
        index: test1
        body:
          size: 0
          aggs:
            ul_histogram:
              histogram:
                field: ul
                interval: 9223372036854775807
  - length: { aggregations.ul_histogram.buckets: 3 }
  - match: { aggregations.ul_histogram.buckets.0.key: 0.0 }
  - match: { aggregations.ul_histogram.buckets.0.doc_count: 1 }
  - match: { aggregations.ul_histogram.buckets.1.key: 9.223372036854776E18 }
  - match: { aggregations.ul_histogram.buckets.1.doc_count: 2 }
  - match: { aggregations.ul_histogram.buckets.2.key: 1.8446744073709552E19 }
  - match: { aggregations.ul_histogram.buckets.2.doc_count: 2 }

  - do:
      search:
        index: test1
        body:
          size: 0
          aggs:
            ul_range:
              range:
                field: ul
                ranges: [
                  { "from": null, "to": 9223372036854775807 },
                  { "from": 9223372036854775807, "to" : 18446744073709551614},
                  { "from": 18446744073709551614}
                ]
  - length: { aggregations.ul_range.buckets: 3 }
  - match: { aggregations.ul_range.buckets.0.doc_count: 1 }
  - match: { aggregations.ul_range.buckets.1.doc_count: 2 }
  - match: { aggregations.ul_range.buckets.2.doc_count: 2 }


---
"Fields retrieval":

  # fields API
  - do:
      search:
        index: test1
        body:
          query:
            match_all: {}
          fields : [ "ul" ]
          sort: [ { ul: asc } ]
          _source : false

  - match: { hits.hits.0.fields.ul.0 : 0 }
  - match: { hits.hits.1.fields.ul.0 : 9223372036854775807 }
  - match: { hits.hits.2.fields.ul.0 : 9223372036854775808 }
  - match: { hits.hits.3.fields.ul.0 : 18446744073709551614 }
  - match: { hits.hits.4.fields.ul.0 : 18446744073709551615 }

  # doc values
  - do:
      search:
        index: test1
        body:
          query:
            match_all: {}
          docvalue_fields: [ "ul" ]
          sort: [ { ul: asc } ]
          _source : false

  - match: { hits.hits.0.fields.ul.0 : 0 }
  - match: { hits.hits.1.fields.ul.0 : 9223372036854775807 }
  - match: { hits.hits.2.fields.ul.0 : 9223372036854775808 }
  - match: { hits.hits.3.fields.ul.0 : 18446744073709551614 }
  - match: { hits.hits.4.fields.ul.0 : 18446744073709551615 }

  # stored fields
  - do:
      search:
        index: test1
        body:
          query:
            match_all: {}
          stored_fields: [ "ul" ]
          sort: [ { ul: asc } ]
          _source : false

  - match: { hits.hits.0.fields.ul.0 : "0" }
  - match: { hits.hits.1.fields.ul.0 : "9223372036854775807" }
  - match: { hits.hits.2.fields.ul.0 : "9223372036854775808" }
  - match: { hits.hits.3.fields.ul.0 : "18446744073709551614" }
  - match: { hits.hits.4.fields.ul.0 : "18446744073709551615" }

---
"Composite aggregations":
  - requires:
      cluster_features: ["gte_v7.11.0"]
      reason: "unsigned_long support for composite aggs was fixed in 7.11"

  - do:
      search:
        index: test1
        body:
          size: 0
          aggs:
            test:
              composite:
                 size: 3
                 sources: [{
                  "ul": {
                     "terms": {
                       "field": "ul"
                     }
                   }
                 }]

  - set: { aggregations.test.after_key: after_key }
  - length: { aggregations.test.buckets: 3 }
  - match: { aggregations.test.buckets.0.key.ul: 0 }
  - match: { aggregations.test.buckets.0.doc_count:  1 }
  - match: { aggregations.test.buckets.1.key.ul: 9223372036854775807 }
  - match: { aggregations.test.buckets.1.doc_count:  1 }
  - match: { aggregations.test.buckets.2.key.ul: 9223372036854775808 }
  - match: { aggregations.test.buckets.2.doc_count:  1 }

  - do:
      search:
        index: test1
        body:
          size: 0
          aggs:
            test:
              composite:
                size: 3
                after: $after_key
                sources: [{
                  "ul": {
                    "terms": {
                      "field": "ul"
                    }
                }
              }]

  - set: { aggregations.test.after_key: after_key }
  - length: { aggregations.test.buckets: 2 }
  - match: { aggregations.test.buckets.0.key.ul: 18446744073709551614 }
  - match: { aggregations.test.buckets.0.doc_count:  1 }
  - match: { aggregations.test.buckets.1.key.ul: 18446744073709551615 }
  - match: { aggregations.test.buckets.1.doc_count:  1 }

  - do:
      search:
        index: test1
        body:
          size: 0
          aggs:
            test:
              composite:
                size: 3
                after: $after_key
                sources: [{
                  "ul": {
                    "terms": {
                      "field": "ul"
                    }
                  }
              }]

  - length: { aggregations.test.buckets: 0 }

---
"Decay":
  - requires:
      test_runner_features: close_to
      cluster_features: ["gte_v8.9.0"]
      reason: "decay functions not supported for unsigned_long"

  - do:
      search:
        index: test1
        body:
          size: 10
          query:
            function_score:
              functions: [{
                "linear": {
                  "ul": {
                    "scale": 18000000000000000000.0,
                    "origin": 12000000000000000000.0
                  }
                }
              }]

  - close_to: { hits.hits.0._score: { value: 0.9228715, error: 0.001 } }
  - close_to: { hits.hits.1._score: { value: 0.9228715, error: 0.001 } }
  - close_to: { hits.hits.2._score: { value: 0.8209238, error: 0.001 } }
  - close_to: { hits.hits.3._score: { value: 0.8209238, error: 0.001 } }
  - close_to: { hits.hits.4._score: { value: 0.6666667, error: 0.001 } }
