setup:
  - skip:
      features: ["allowed_warnings", "headers"]
  - do:
      indices.create:
        index: foo
        body:
          mappings:
            properties:
              timestamp:
                type: date
              partition:
                type: keyword
              price:
                type: integer

  - do:
      indices.create:
        index: dummy-rollup-index
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            _meta:
              _rollup:
                my-id: { }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.put_job:
        id: foo
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "calendar_interval": "1h"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      bulk:
        refresh: true
        body:
          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 1
            partition.terms.value: "a"
            partition.terms._count: 1
            price.max.value: 1
            "_rollup.id": "foo"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 2
            partition.terms.value: "b"
            partition.terms._count: 2
            price.max.value: 2
            "_rollup.id": "foo"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "b"
            partition.terms._count: 10
            price.max.value: 4
            "_rollup.id": "foo"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

---
"Basic Search":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }

---
"Basic Search with rest_total_hits_as_int":
  - requires:
      cluster_features: ["gte_v6.6.0"]
      reason: rest_total_hits_as_int was introduced in 6.6.0
  - do:
      allowed_warnings:
        - The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information.
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }

---
"Formatted Date Histo":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
                format: "yyyy-MM-dd"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }

---
"Empty aggregation":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        rest_total_hits_as_int: true
        index: "foo_rollup"
        body:
          size: 0
          aggs: {}

  - length: { hits.hits: 0 }
  - match: { hits.total: 0 }
  - is_false: aggregations

---
"Empty aggregation with new response format":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          aggs: {}

  - length: { hits.hits: 0 }
  - match: { hits.total.value:    0 }
  - match: { hits.total.relation: eq }
  - is_false: aggregations



---
"Search with Metric":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.1.the_max.value: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }
  - match: { aggregations.histo.buckets.3.the_max.value: 4 }

---
"Search with Query":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          query:
            term:
              partition: a
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 0 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.the_max.value: 3 }

---
"Search with MatchAll and Second Job":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "calendar_interval": "1h"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      bulk:
        refresh: true
        body:
          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 1
            partition.terms.value: "a"
            partition.terms._count: 1
            price.max.value: 1
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 2
            partition.terms.value: "b"
            partition.terms._count: 2
            price.max.value: 2
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "b"
            partition.terms._count: 10
            price.max.value: 4
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1


  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.1.the_max.value: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }
  - match: { aggregations.histo.buckets.3.the_max.value: 4 }

---
"Search with Query and Second Job":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "calendar_interval": "1h"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      bulk:
        refresh: true
        body:
          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 1
            partition.terms.value: "a"
            partition.terms._count: 1
            price.max.value: 1
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 2
            partition.terms.value: "b"
            partition.terms._count: 2
            price.max.value: 2
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "b"
            partition.terms._count: 10
            price.max.value: 4
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1


  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          query:
            term:
              partition: a
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 0 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.the_max.value: 3 }

---
"Search with Query and Second Job different intervals":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "calendar_interval": "1d"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      bulk:
        refresh: true
        body:
          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T05:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 1
            partition.terms.value: "a"
            partition.terms._count: 1
            price.max.value: 1
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T06:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 2
            partition.terms.value: "b"
            partition.terms._count: 2
            price.max.value: 2
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T07:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "b"
            partition.terms._count: 10
            price.max.value: 4
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

          - index:
              _index: "foo_rollup"
          - timestamp.date_histogram.timestamp: "2017-01-01T08:00:00Z"
            timestamp.date_histogram.interval: "1h"
            timestamp.date_histogram.time_zone: "UTC"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "foo2"
            "_rollup.computed":
              - "timestamp.date_histogram"
              - "partition.terms"
            "_rollup.version": 1

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        body:
          size: 0
          query:
            term:
              partition: a
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 0 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.the_max.value: 3 }

---
"Wildcards matching single rollup index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup*"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }

---
"Wildcards matching two rollup indices":

  - do:
      indices.create:
        index: bar
        body:
          mappings:
            properties:
              timestamp:
                type: date
              partition:
                type: keyword
              price:
                type: integer

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: bar
        body:  >
          {
            "index_pattern": "bar",
            "rollup_index": "bar_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "calendar_interval": "1h"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      catch: /RollupSearch currently only supports searching one rollup index at a time\./
      rollup.rollup_search:
        index: "*_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"


---
"Rollup search via alias":

  - do:
      indices.put_alias:
        index: foo_rollup
        name: rollup_alias

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "rollup_alias"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"

  - length: { aggregations.histo.buckets: 4 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.histo.buckets.3.doc_count: 20 }

---
"Rollup search via alias, multiple rollup indices match":

  - do:
      indices.create:
        index: bar
        body:
          mappings:
            properties:
              timestamp:
                type: date
              partition:
                type: keyword
              price:
                type: integer

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: bar
        body:  >
          {
            "index_pattern": "bar",
            "rollup_index": "bar_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "calendar_interval": "1h"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      indices.put_alias:
        index: foo_rollup,bar_rollup
        name: rollup_alias

  - do:
       allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
       catch: /RollupSearch currently only supports searching one rollup index at a time\./
       rollup.rollup_search:
         index: "rollup_alias"
         body:
           size: 0
           aggs:
             histo:
               date_histogram:
                 field: "timestamp"
                 calendar_interval: "1h"
                 time_zone: "UTC"

---
"Obsolete Timezone":
  - requires:
      cluster_features: ["gte_v7.2.0"]
      reason: "IANA TZ deprecations in 7.2"
      test_runner_features: "warnings"
  - do:
      indices.create:
        index: tz
        body:
          mappings:
            properties:
              timestamp:
                type: date
              partition:
                type: keyword
              price:
                type: integer
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      warnings:
        - "Creating Rollup job [tz] with timezone [Canada/Mountain], but [Canada/Mountain] has been deprecated by the IANA.  Use [America/Edmonton] instead."
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.put_job:
        id: tz
        body:  >
          {
            "index_pattern": "tz",
            "rollup_index": "tz_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "timestamp",
                "fixed_interval": "5m",
                "time_zone": "Canada/Mountain"
              },
              "terms": {
                "fields": ["partition"]
              }
            },
            "metrics": [
              {
                "field": "price",
                "metrics": ["max"]
              }
            ]
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      bulk:
        refresh: true
        body:
          - index:
              _index: "tz_rollup"
          - timestamp.date_histogram.timestamp: 1531221000000
            timestamp.date_histogram.interval: "5m"
            timestamp.date_histogram.time_zone: "America/Edmonton"
            timestamp.date_histogram._count: 1
            partition.terms.value: "a"
            partition.terms._count: 1
            price.max.value: 1
            "_rollup.id": "tz"
            "_rollup.version": 2

          - index:
              _index: "tz_rollup"
          - timestamp.date_histogram.timestamp: 1531221300000
            timestamp.date_histogram.interval: "5m"
            timestamp.date_histogram.time_zone: "America/Edmonton"
            timestamp.date_histogram._count: 2
            partition.terms.value: "b"
            partition.terms._count: 2
            price.max.value: 2
            "_rollup.id": "tz"
            "_rollup.version": 2

          - index:
              _index: "tz_rollup"
          - timestamp.date_histogram.timestamp: 1531221600000
            timestamp.date_histogram.interval: "5m"
            timestamp.date_histogram.time_zone: "America/Edmonton"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "tz"
            "_rollup.version": 2

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "tz_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "5m"
                time_zone: "America/Edmonton"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 3 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2018-07-10T05:10:00.000-06:00" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2018-07-10T05:15:00.000-06:00" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.1.the_max.value: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2018-07-10T05:20:00.000-06:00" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "tz_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "5m"
                time_zone: "Canada/Mountain"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 3 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2018-07-10T05:10:00.000-06:00" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2018-07-10T05:15:00.000-06:00" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.1.the_max.value: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2018-07-10T05:20:00.000-06:00" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }

---
"Obsolete BWC Timezone":
  - requires:
      cluster_features: ["gte_v7.2.0"]
      reason: "IANA TZ deprecations in 7.2"
  - do:
      indices.create:
        index: tz_rollup
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            properties:
              partition.terms.value:
                type: keyword
              partition.terms._count:
                type: long
              timestamp.date_histogram.time_zone:
                type: keyword
              timestamp.date_histogram.interval:
                type: keyword
              timestamp.date_histogram.timestamp:
                type: date
              timestamp.date_histogram._count:
                type: long
              price.max.value:
                type: double
              _rollup.id:
                type: keyword
              _rollup.version:
                type: long
            _meta:
              _rollup:
                sensor:
                  cron: "* * * * * ?"
                  rollup_index: "tz_rollup"
                  index_pattern: "tz"
                  timeout: "20s"
                  page_size: 1000
                  groups:
                    date_histogram:
                      field: "timestamp"
                      interval: "5m"
                      time_zone: "Canada/Mountain"
                    terms:
                      fields:
                        - "partition"
                  id: tz
                  metrics:
                    - field: "price"
                      metrics:
                        - max

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      bulk:
        refresh: true
        body:
          - index:
              _index: "tz_rollup"
          - timestamp.date_histogram.timestamp: 1531221000000
            timestamp.date_histogram.interval: "5m"
            timestamp.date_histogram.time_zone: "Canada/Mountain"
            timestamp.date_histogram._count: 1
            partition.terms.value: "a"
            partition.terms._count: 1
            price.max.value: 1
            "_rollup.id": "tz"
            "_rollup.version": 2

          - index:
              _index: "tz_rollup"
          - timestamp.date_histogram.timestamp: 1531221300000
            timestamp.date_histogram.interval: "5m"
            timestamp.date_histogram.time_zone: "Canada/Mountain"
            timestamp.date_histogram._count: 2
            partition.terms.value: "b"
            partition.terms._count: 2
            price.max.value: 2
            "_rollup.id": "tz"
            "_rollup.version": 2

          - index:
              _index: "tz_rollup"
          - timestamp.date_histogram.timestamp: 1531221600000
            timestamp.date_histogram.interval: "5m"
            timestamp.date_histogram.time_zone: "Canada/Mountain"
            timestamp.date_histogram._count: 10
            partition.terms.value: "a"
            partition.terms._count: 10
            price.max.value: 3
            "_rollup.id": "tz"
            "_rollup.version": 2

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "tz_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "5m"
                time_zone: "America/Edmonton"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 3 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2018-07-10T05:10:00.000-06:00" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2018-07-10T05:15:00.000-06:00" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.1.the_max.value: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2018-07-10T05:20:00.000-06:00" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }


  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "tz_rollup"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "5m"
                time_zone: "Canada/Mountain"
              aggs:
                the_max:
                  max:
                    field: "price"

  - length: { aggregations.histo.buckets: 3 }
  - match: { aggregations.histo.buckets.0.key_as_string: "2018-07-10T05:10:00.000-06:00" }
  - match: { aggregations.histo.buckets.0.doc_count: 1 }
  - match: { aggregations.histo.buckets.0.the_max.value: 1 }
  - match: { aggregations.histo.buckets.1.key_as_string: "2018-07-10T05:15:00.000-06:00" }
  - match: { aggregations.histo.buckets.1.doc_count: 2 }
  - match: { aggregations.histo.buckets.1.the_max.value: 2 }
  - match: { aggregations.histo.buckets.2.key_as_string: "2018-07-10T05:20:00.000-06:00" }
  - match: { aggregations.histo.buckets.2.doc_count: 10 }
  - match: { aggregations.histo.buckets.2.the_max.value: 3 }


---
"Search with typed_keys":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.rollup_search:
        index: "foo_rollup"
        typed_keys: true
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                calendar_interval: "1h"
                time_zone: "UTC"
              aggs:
                the_max:
                  max:
                    field: "price"

  - match: { aggregations.date_histogram#histo.buckets.0.key_as_string: "2017-01-01T05:00:00.000Z" }
  - match: { aggregations.date_histogram#histo.buckets.0.doc_count: 1 }
  - match: { aggregations.date_histogram#histo.buckets.0.max#the_max.value: 1 }
  - match: { aggregations.date_histogram#histo.buckets.1.key_as_string: "2017-01-01T06:00:00.000Z" }
  - match: { aggregations.date_histogram#histo.buckets.1.doc_count: 2 }
  - match: { aggregations.date_histogram#histo.buckets.1.max#the_max.value: 2 }
  - match: { aggregations.date_histogram#histo.buckets.2.key_as_string: "2017-01-01T07:00:00.000Z" }
  - match: { aggregations.date_histogram#histo.buckets.2.doc_count: 10 }
  - match: { aggregations.date_histogram#histo.buckets.2.max#the_max.value: 3 }
  - match: { aggregations.date_histogram#histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
  - match: { aggregations.date_histogram#histo.buckets.3.doc_count: 20 }
  - match: { aggregations.date_histogram#histo.buckets.3.max#the_max.value: 4 }

---
"Search error against live index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      catch: bad_request
      rollup.rollup_search:
        index: "foo"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "asdfasdf"


---
"Search error against rollup and live index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      catch: bad_request
      rollup.rollup_search:
        index: "foo*"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "asdfasdf"

---
"Search error no matching indices":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      catch: /Must specify at least one concrete index/
      rollup.rollup_search:
        index: "bar*"
        body:
          size: 0
          aggs:
            histo:
              date_histogram:
                field: "timestamp"
                fixed_interval: "1h"

