setup:
  - skip:
      version: " - 5.3.99"
      reason: typed_keys parameter was added in 5.4.0

  - do:
      indices.create:
          index: test
          body:
            settings:
              number_of_replicas: 0
            mappings:
              test:
                properties:
                  name:
                     type: keyword
                  num:
                     type: integer
                  created:
                    type: date

  - do:
     bulk:
        refresh: true
        index: test
        type: test
        body:
          - '{"index": {}}'
          - '{"name": "one", "num": 1, "created": "2010-03-12T01:07:45"}'
          - '{"index": {}}'
          - '{"name": "two", "num": 2, "created": "2010-03-12T04:11:00"}'
          - '{"index": {}}'
          - '{"name": "three", "num": 3, "created": "2010-04-27T03:43:34"}'

---
"Test typed keys parameter for avg aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_avg:
              avg:
                field: num
  - is_true: aggregations.avg#test_avg

---
"Test typed keys parameter for cardinality aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_cardinality:
              cardinality:
                field: name
  - is_true: aggregations.cardinality#test_cardinality

---
"Test typed keys parameter for extended_stats aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_extended_stats:
              extended_stats:
                field: num
  - is_true: aggregations.extended_stats#test_extended_stats

---
"Test typed keys parameter for max aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_max:
              max:
                field: num
  - is_true: aggregations.max#test_max

---
"Test typed keys parameter for min aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_min:
              min:
                field: num
  - is_true: aggregations.min#test_min

---
"Test typed keys parameter for percentiles aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_percentiles:
              percentiles:
                field: num
  - is_true: aggregations.tdigest_percentiles#test_percentiles

---
"Test typed keys parameter for percentile_ranks aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_percentile_ranks:
              percentile_ranks:
                field: num
                values: [0,10]
  - is_true: aggregations.tdigest_percentile_ranks#test_percentile_ranks

---
"Test typed keys parameter for stats aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_stats:
              stats:
                field: num
  - is_true: aggregations.stats#test_stats

---
"Test typed keys parameter for sum aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_sum:
              sum:
                field: num
  - is_true: aggregations.sum#test_sum

---
"Test typed keys parameter for terms and top_hits aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_terms:
              terms:
                field: name
              aggs:
                test_top_hits:
                  top_hits:
                    sort: num
  - is_true: aggregations.sterms#test_terms
  - is_true: aggregations.sterms#test_terms.buckets.0.top_hits#test_top_hits
  - is_true: aggregations.sterms#test_terms.buckets.1.top_hits#test_top_hits
  - is_true: aggregations.sterms#test_terms.buckets.2.top_hits#test_top_hits

---
"Test typed keys parameter for terms aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_terms:
              terms:
                field: num
  - is_true: aggregations.lterms#test_terms

---
"Test typed keys parameter for value_count aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_value_count:
              value_count:
                field: num
  - is_true: aggregations.value_count#test_value_count

---
"Test typed keys parameter for date_histogram aggregation and max_bucket pipeline aggregation":
  - do:
      search:
        typed_keys: true
        body:
          size: 0
          aggregations:
            test_created_histogram:
              date_histogram:
                field: created
                interval: month
              aggregations:
                test_sum:
                  sum:
                    field: num
                test_moving_avg:
                  moving_avg:
                    buckets_path: "test_sum"
            test_max_bucket:
              max_bucket:
                buckets_path: "test_created_histogram>test_sum"

  - is_true: aggregations.date_histogram#test_created_histogram
  - is_true: aggregations.date_histogram#test_created_histogram.buckets.0.sum#test_sum
  - is_true: aggregations.date_histogram#test_created_histogram.buckets.1.sum#test_sum
  - is_true: aggregations.date_histogram#test_created_histogram.buckets.1.simple_value#test_moving_avg
  - is_true: aggregations.bucket_metric_value#test_max_bucket
