setup:
  - do:
      indices.create:
          index: test_1
          body:
            settings:
              number_of_replicas: 0
              number_of_shards: 5
              number_of_routing_shards: 5
            mappings:
              doc:
                properties:
                  int_field:
                     type : integer
                  double_field:
                     type : double
                  string_field:
                     type: keyword

  - do:
       bulk:
         refresh: true
         body:
           - index:
               _index: test_1
               _type:  doc
               _id:    1
           - int_field: 1
             double_field: 1.0
             string_field: foo
           - index:
               _index: test_1
               _type:  doc
               _id:    2
           - int_field: 51
             double_field: 51.0
             string_field: foo
           - index:
               _index: test_1
               _type:  doc
               _id:    3
           - int_field: 101
             double_field: 101.0
             string_field: foo
           - index:
               _index: test_1
               _type:  doc
               _id:    4
           - int_field: 151
             double_field: 151.0
             string_field: foo

---
"Basic test":

  - do:
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}
            percentiles_double:
              percentiles:
                field: double_field
                hdr: {}


  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }

  - match: { aggregations.percentiles_int.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_int.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_int.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_int.values.99\.0: 151.1240234375 }

  - match: { aggregations.percentiles_double.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_double.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_double.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_double.values.99\.0: 151.1240234375 }

  - do:
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr:
                  number_of_significant_value_digits: 3
            percentiles_double:
              percentiles:
                field: double_field
                hdr:
                  number_of_significant_value_digits: 3


  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }

  - match: { aggregations.percentiles_int.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_int.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_int.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_int.values.99\.0: 151.1240234375 }

  - match: { aggregations.percentiles_double.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_double.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_double.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_double.values.99\.0: 151.1240234375 }


---
"Only aggs test":

  - do:
      search:
        body:
          size: 0
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}
            percentiles_double:
              percentiles:
                field: double_field
                hdr: {}

  - match: { hits.total: 4 }
  - length: { hits.hits: 0 }

  - match: { aggregations.percentiles_int.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_int.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_int.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_int.values.99\.0: 151.1240234375 }

  - match: { aggregations.percentiles_double.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_double.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_double.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_double.values.99\.0: 151.1240234375 }


---
"Filtered test":

  - do:
      search:
        body:
          query:
            constant_score:
              filter:
                range:
                  int_field:
                    gte: 50
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}
            percentiles_double:
              percentiles:
                field: double_field
                hdr: {}

  - match: { hits.total: 3 }
  - length: { hits.hits: 3 }

  - match: { aggregations.percentiles_int.values.1\.0: 51.0 }
  - match: { aggregations.percentiles_int.values.5\.0: 51.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 51.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 101.03125 }
  - match: { aggregations.percentiles_int.values.75\.0: 101.03125 }
  - match: { aggregations.percentiles_int.values.95\.0: 151.09375 }
  - match: { aggregations.percentiles_int.values.99\.0: 151.09375 }

  - match: { aggregations.percentiles_double.values.1\.0: 51.0 }
  - match: { aggregations.percentiles_double.values.5\.0: 51.0 }
  - match: { aggregations.percentiles_double.values.25\.0: 51.0 }
  - match: { aggregations.percentiles_double.values.50\.0: 101.03125 }
  - match: { aggregations.percentiles_double.values.75\.0: 101.03125 }
  - match: { aggregations.percentiles_double.values.95\.0: 151.09375 }
  - match: { aggregations.percentiles_double.values.99\.0: 151.09375 }


---
"Missing field with missing param":

  - do:
      search:
        body:
          aggs:
            percentiles_missing:
              percentiles:
                field: missing_field
                missing: 1.0
                hdr: {}

  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }

  - match: { aggregations.percentiles_missing.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_missing.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_missing.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_missing.values.50\.0: 1.0 }
  - match: { aggregations.percentiles_missing.values.75\.0: 1.0 }
  - match: { aggregations.percentiles_missing.values.95\.0: 1.0 }
  - match: { aggregations.percentiles_missing.values.99\.0: 1.0 }

---
"Missing field without missing param":

  - do:
      search:
        body:
          aggs:
            percentiles_missing:
              percentiles:
                field: missing_field
                hdr: {}

  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }
  - is_false: aggregations.percentiles_missing.value

---
"Metadata test":

  - do:
      search:
        body:
          aggs:
            percentiles_int:
              meta:
                foo: bar
              percentiles:
                field: int_field
                hdr: {}

  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }
  - match: { aggregations.percentiles_int.meta.foo: "bar" }

  - match: { aggregations.percentiles_int.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_int.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_int.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_int.values.99\.0: 151.1240234375 }

---
"Invalid params test":

  - do:
      catch: /\[numberOfSignificantValueDigits\] must be between 0 and 5/
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr:
                  number_of_significant_value_digits: -1

  - do:
      catch: /\[numberOfSignificantValueDigits\] must be between 0 and 5/
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr:
                  number_of_significant_value_digits: 10

  - do:
      catch: bad_request
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr:
                  number_of_significant_value_digits: null

  - do:
      catch: /\[percents\] must not be empty/
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}
                percents: []

  - do:
      catch: bad_request
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}
                percents: null

  - do:
      catch: bad_request
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}
                percents: ["foo"]

  - do:
      catch: bad_request
      search:
        body:
          aggs:
            percentiles_string:
              percentiles:
                field: string_field
                hdr: {}

---
"Explicit Percents test":

  - do:
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                percents: [5.0, 25.0, 50.0]
                hdr: {}
            percentiles_double:
              percentiles:
                field: double_field
                percents: [5.0, 25.0, 50.0]
                hdr: {}


  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }

  - match: { aggregations.percentiles_int.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 51.0302734375 }


  - match: { aggregations.percentiles_double.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_double.values.50\.0: 51.0302734375 }


---
"Non-keyed test":

  - do:
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                percents: [5.0, 25.0, 50.0]
                keyed: false
                hdr: {}


  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }


  - match:  { aggregations.percentiles_int.values.0.key:  5.0 }
  - match:  { aggregations.percentiles_int.values.0.value:  1.0 }
  - match:  { aggregations.percentiles_int.values.1.key:  25.0 }
  - match:  { aggregations.percentiles_int.values.1.value:  1.0 }
  - match:  { aggregations.percentiles_int.values.2.key:  50.0 }
  - match:  { aggregations.percentiles_int.values.2.value:  51.0302734375 }


---
"Negative values test":

  - do:
      index:
          index:   test_1
          type:    doc
          id:      5
          refresh: true
          body:    { int_field: -10 }

  - do:
      search:
        body:
          aggs:
            percentiles_int:
              percentiles:
                field: int_field
                hdr: {}


  - match: { hits.total: 4 }
  - length: { hits.hits: 4 }

  - match: { aggregations.percentiles_int.values.1\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.5\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.25\.0: 1.0 }
  - match: { aggregations.percentiles_int.values.50\.0: 51.0302734375 }
  - match: { aggregations.percentiles_int.values.75\.0: 101.0615234375 }
  - match: { aggregations.percentiles_int.values.95\.0: 151.1240234375 }
  - match: { aggregations.percentiles_int.values.99\.0: 151.1240234375 }
  - match: { _shards.failures.0.reason.type: array_index_out_of_bounds_exception }


