---
setup:
  - skip:
      features: allowed_warnings
  - do:
      allowed_warnings:
        - "index template [logs_template] has index patterns [logs-foobar] matching patterns from existing older templates [global] with patterns (global => [*]); this template [logs_template] will take precedence during new index creation"
      indices.put_index_template:
        name: logs_template
        body:
          index_patterns: logs-foobar
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: logs-foobar

---
teardown:
  - do:
      indices.delete_data_stream:
        name: logs-foobar

---
"Verify get index api":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: logs-foobar
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get:
        index: logs-foobar
  - is_true: .$idx0name
  - is_false: logs-foobar

---
"Verify get mapping api":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: logs-foobar
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_mapping:
        index: logs-foobar
  - is_true: .$idx0name.mappings
  - is_false: logs-foobar.mappings

---
"Verify shard stores api":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams available in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          template:
            settings:
              number_of_shards: "1"
              number_of_replicas: "0"
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  # rollover data stream to create new backing index
  - do:
      indices.rollover:
        alias: "simple-data-stream1"

  - match: { old_index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { new_index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000002/" }
  - match: { rolled_over: true }
  - match: { dry_run: false }

  - do:
      cluster.health:
        index: simple-data-stream1
        wait_for_status: green

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream1
  - set: { data_streams.0.indices.0.index_name: idx0name }
  - set: { data_streams.0.indices.1.index_name: idx1name }

  - do:
      indices.shard_stores:
        index: simple-data-stream1
        status: "green"

  - match: { indices.$idx0name.shards.0.stores.0.allocation: "primary" }
  - match: { indices.$idx1name.shards.0.stores.0.allocation: "primary" }

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged
---
"Verify search shards api":
  - requires:
      test_runner_features: allowed_warnings
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams available in 7.9+"

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  - do:
      search_shards:
        index:  "simple-data-stream1"

  - match: { shards.0.0.index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

---
"Verify get field mappings api":
  - requires:
      test_runner_features: allowed_warnings
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams available in 7.9+"

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream1
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_field_mapping:
        index: simple-data-stream1
        fields: foo

  - is_true: .$idx0name

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

---
"Open write index for data stream opens all backing indices":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams available in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  # rollover data stream twice to create new backing indices
  - do:
      indices.rollover:
        alias: "simple-data-stream1"

  - match: { old_index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { new_index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000002/" }
  - match: { rolled_over: true }
  - match: { dry_run: false }

  - do:
      indices.rollover:
        alias: "simple-data-stream1"

  - match: { old_index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000002/" }
  - match: { new_index: "/\\.ds-simple-data-stream1-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000003/" }
  - match: { rolled_over: true }
  - match: { dry_run: false }

  - do:
      indices.close:
        index: ".ds-simple-data-stream1-*000001,.ds-simple-data-stream1-*000002"
  - is_true: acknowledged

  - do:
      indices.open:
        index: simple-data-stream1
  - is_true: acknowledged

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream1

  - set: { data_streams.0.indices.0.index_name: idx0name }
  - set: { data_streams.0.indices.1.index_name: idx1name }

  # all closed backing indices should be re-opened and returned
  - do:
      indices.get:
        index: ".ds-simple-data-stream1-*"

  - is_true: .$idx0name.settings
  - is_true: .$idx1name.settings

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

---
"Verify rank eval with data streams":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams available in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      index:
        index:   logs-foobar
        id:      doc1
        op_type: create
        body:    { "text": "berlin", "@timestamp": "2020-01-01" }

  - do:
      index:
        index:   logs-foobar
        id:      doc2
        op_type: create
        body:    { "text": "amsterdam", "@timestamp": "2020-01-01" }

  # rollover data stream to split documents across multiple backing indices
  - do:
      indices.rollover:
        alias: "logs-foobar"

  - match: { old_index: "/\\.ds-logs-foobar-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { new_index: "/\\.ds-logs-foobar-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000002/" }
  - match: { rolled_over: true }
  - match: { dry_run: false }

  - do:
      index:
        index:   logs-foobar
        id:      doc3
        op_type: create
        body:    { "text": "amsterdam", "@timestamp": "2020-01-01" }

  - do:
      index:
        index:   logs-foobar
        id:      doc4
        op_type: create
        body:    { "text": "something about amsterdam and berlin", "@timestamp": "2020-01-01" }

  - do:
      indices.refresh:
        index: logs-foobar

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: logs-foobar

  - set: { data_streams.0.indices.0.index_name: idx0name }
  - set: { data_streams.0.indices.1.index_name: idx1name }

  - do:
      rank_eval:
        index: logs-foobar
        search_type: query_then_fetch
        body: {
          "requests" : [
          {
            "id": "amsterdam_query",
            "request": { "query": { "match" : {"text" : "amsterdam" }}},
            "ratings": [
            {"_index": $idx0name, "_id": "doc1", "rating": 0},
            {"_index": $idx0name, "_id": "doc2", "rating": 1},
            {"_index": $idx1name, "_id": "doc3", "rating": 1}]
          },
          {
            "id" : "berlin_query",
            "request": { "query": { "match" : { "text" : "berlin" } }, "size" : 10 },
            "ratings": [{"_index": $idx0name, "_id": "doc1", "rating": 1}]
          }
          ],
          "metric" : { "precision": { "ignore_unlabeled" : true }}
        }

  - match: { metric_score: 1}
  - match: { details.amsterdam_query.metric_score: 1.0}
  - length: { details.amsterdam_query.hits: 3}
  - match: { details.berlin_query.metric_score: 1.0}
