---
setup:
  - do:
      indices.create:
        index: test-1
        body:
          settings:
            number_of_shards: "2"

  - do:
      indices.create:
        index: test-2
        body:
          settings:
            number_of_shards: "1"

  - do:
      indices.create:
        index: test-3
        body:
          settings:
            number_of_shards: "3"

  - do:
      index:
        index:  test-2
        body:   { max: 2, date: "2022-03-28" }

  - do:
      index:
        index:  test-1
        body:   { max: 1, date: "2022-03-29" }

  - do:
      index:
        index:  test-3
        body:   { max: 3, date: "2022-03-30" }

  - do:
      indices.refresh: {}

---
"Async search":

  - do:
      async_search.submit:
        index: test-*
        batched_reduce_size: 2
        wait_for_completion_timeout: 10s
        body:
          query:
            match_all: {}
          aggs:
            max:
              max:
                field: max
          sort: max

  - is_false: id
  - match:  { is_partial:                   false }
  - length: { response.hits.hits:               3 }
  - match:  { response.hits.hits.0._source.max: 1 }
  - match:  { response.aggregations.max.value:  3.0 }

  - do:
      async_search.submit:
        index: test-*
        batched_reduce_size: 2
        wait_for_completion_timeout: 10s
        keep_on_completion: true
        body:
          aggs:
            max:
              max:
                field: max
          sort: max

  - set:    { id:                                  id }
  - match:  { is_partial:                   false }
  - length: { response.hits.hits:               3 }
  - match:  { response.hits.hits.0._source.max: 1 }
  - match:  { response.aggregations.max.value:  3.0 }

  - do:
      async_search.get:
        id: "$id"

  - match:  { is_partial:                     false }
  - is_false: response._clusters
  - length: { response.hits.hits:                 3 }
  - match:  { response.hits.hits.0._source.max:   1 }
  - match:  { response.aggregations.max.value:    3.0 }

  - do:
      async_search.status:
        id: "$id"
  - match:  { id:                             $id   }
  - match:  { is_running:                     false }
  - match:  { is_partial:                     false }
  - match:  { completion_status:                200 }

  - do:
      async_search.delete:
        id: "$id"

  - match: { acknowledged:   true }

  - do:
      catch: missing
      async_search.get:
        id: "$id"

  - do:
      catch: missing
      async_search.status:
        id: "$id"

  - do:
      catch: missing
      async_search.delete:
        id: "$id"

---
"With typed keys":
  - do:
      async_search.submit:
        index: test-*
        batched_reduce_size: 2
        wait_for_completion_timeout: 10s
        keep_on_completion: true
        typed_keys: true
        body:
          aggs:
            max:
              max:
                field: max
          sort: max

  - set:    { id:                                  id }
  - match:  { is_partial:                       false }
  - is_false: response._clusters
  - length: { response.hits.hits:                   3 }
  - match:  { response.hits.hits.0._source.max:     1 }
  - match:  { response.aggregations.max#max.value:  3.0 }

  - do:
      async_search.get:
        id: "$id"

  - match:  { is_partial:                     false }
  - is_false: response._clusters
  - length: { response.hits.hits:                 3 }
  - match:  { response.hits.hits.0._source.max:   1 }
  - match:  { response.aggregations.max.value:    3.0 }

  - do:
      async_search.get:
        id: "$id"
        typed_keys: true

  - match:  { is_partial:                       false }
  - is_false: response._clusters
  - length: { response.hits.hits:                   3 }
  - match:  { response.hits.hits.0._source.max:     1 }
  - match:  { response.aggregations.max#max.value:  3.0 }

  - do:
      async_search.delete:
        id: "$id"

  - match: { acknowledged:   true }

---
"Auto date histogram interval is saved":
  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason: "interval is only serialized from 8.3 on"
  - do:
      async_search.submit:
        index: test-*
        batched_reduce_size: 2
        wait_for_completion_timeout: 10s
        keep_on_completion: true
        body:
          size: 0
          aggs:
            histo:
              auto_date_histogram:
                field: date

  - set:    { id:                                  id }
  - match:  { is_partial:                       false }
  - is_false: response._clusters
  - length: { response.aggregations.histo.buckets:   5 }
  - match:  { response.aggregations.histo.interval:  12h }

  - do:
      async_search.get:
        id: "$id"

  - match:  { is_partial:                     false }
  - is_false: response._clusters
  - length: { response.aggregations.histo.buckets:   5 }
  - match:  { response.aggregations.histo.interval:  12h }
