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
                null_value: 17446744073709551615
                store: true

  - do:
      bulk:
        index: test1
        refresh: true
        body: |
          { "index": {"_id" : "1"} }
          { "ul": 0 }
          { "index": {"_id" : "2_null"} }
          { "ul": null }
          { "index": {"_id" : "3_null"} }
          { "ul": ""}
          { "index": {"_id" : "4"} }
          { "ul": 18446744073709551614 }
          { "index": {"_id" : "5_missing"} }
          {}

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


---
"Asc sort" :
  - do:
      search:
        index: test1
        body:
          sort: { ul : { order: asc, missing : "_last" } }
  - match: { "hits.total.value": 5 }
  - match: {hits.hits.0._id: "1" }
  - match: {hits.hits.0.sort: [0] }
  - match: {hits.hits.1._id: "2_null" }
  - match: {hits.hits.1.sort: [17446744073709551615] }
  - match: {hits.hits.2._id: "3_null" }
  - match: {hits.hits.2.sort: [17446744073709551615] }
  - match: {hits.hits.3._id: "4" }
  - match: {hits.hits.3.sort: [18446744073709551614] }
  - match: {hits.hits.4._id: "5_missing" }
  - match: {hits.hits.4.sort: [18446744073709551615] }

---
"Desc sort" :
  - do:
      search:
        index: test1
        body:
          sort: { ul: { order: desc, missing: "_first" } }
  - match: { "hits.total.value": 5 }
  - match: {hits.hits.0._id: "5_missing" }
  - match: {hits.hits.0.sort: [18446744073709551615] }
  - match: {hits.hits.1._id: "4" }
  - match: {hits.hits.1.sort: [18446744073709551614] }
  - match: {hits.hits.2._id: "2_null" }
  - match: {hits.hits.2.sort: [17446744073709551615] }
  - match: {hits.hits.3._id: "3_null" }
  - match: {hits.hits.3.sort: [17446744073709551615] }
  - match: {hits.hits.4._id: "1" }
  - match: {hits.hits.4.sort: [0] }

---
"Fields retrieval" :

  # fields API
  - do:
      search:
        index: test1
        body:
          query:
            match_all: {}
          fields: [ "ul" ]
          sort: { ul: { order: desc, missing: "_first" } }
          _source: false

  - match: { hits.hits.0._id: "5_missing" }
  - is_false: hits.hits.0.fields
  - match: { hits.hits.1._id: "4" }
  - match: { hits.hits.1.fields.ul: [18446744073709551614] }
  - match: { hits.hits.2._id: "2_null" }
  - match: { hits.hits.2.fields.ul: [17446744073709551615] }
  - match: { hits.hits.3._id: "3_null" }
  - match: { hits.hits.3.fields.ul: [17446744073709551615] }
  - match: { hits.hits.4._id: "1" }
  - match: { hits.hits.4.fields.ul: [0] }

  # doc values fields
  - do:
      search:
        index: test1
        body:
          query:
            match_all: {}
          docvalue_fields: [ "ul" ]
          sort: { ul: { order: desc, missing: "_first" } }
          _source: false

  - match: { hits.hits.0._id: "5_missing" }
  - is_false: hits.hits.0.fields
  - match: { hits.hits.1._id: "4" }
  - match: { hits.hits.1.fields.ul: [18446744073709551614] }
  - match: { hits.hits.2._id: "2_null" }
  - match: { hits.hits.2.fields.ul: [17446744073709551615] }
  - match: { hits.hits.3._id: "3_null" }
  - match: { hits.hits.3.fields.ul: [17446744073709551615] }
  - match: { hits.hits.4._id: "1" }
  - match: { hits.hits.4.fields.ul: [0] }

  # stored fields
  - do:
      search:
        index: test1
        body:
          query:
            match_all: {}
          stored_fields: [ "ul" ]
          sort: { ul: { order: desc, missing: "_first" } }
          _source: false

  - match: { hits.hits.0._id : "5_missing" }
  - is_false: hits.hits.0.fields
  - match: { hits.hits.1._id : "4" }
  - match: { hits.hits.1.fields.ul : ["18446744073709551614"] }
  - match: { hits.hits.2._id : "2_null" }
  - match: { hits.hits.2.fields.ul : ["17446744073709551615"] }
  - match: { hits.hits.3._id : "3_null" }
  - match: { hits.hits.3.fields.ul  : ["17446744073709551615"] }
  - match: { hits.hits.4._id : "1" }
  - match: { hits.hits.4.fields.ul : ["0"] }
