---
setup:

  - do:
      indices.create:
        index: docs
        body:
          settings:
            number_of_shards:   1
            number_of_replicas: 0

  - do:
      bulk:
        body:
          - index:
              _index: docs
              _id:    "1"
          - field: foo
          - index:
              _index: docs
              _id:    "2"
          - field: bar
          - index:
              _index: docs
              _id:    "3"
          - field: baz

  - do:
      snapshot.create_repository:
        repository: repository-fs
        body:
          type: fs
          settings:
            location: "repository-fs"

  # Remove the snapshot if a previous test failed to delete it.
  # Useful for third party tests that runs the test against a real external service.
  - do:
      snapshot.delete:
        repository: repository-fs
        snapshot: snapshot
        ignore: 404

  - do:
      snapshot.create:
        repository: repository-fs
        snapshot: snapshot
        wait_for_completion: true

  - do:
      indices.delete:
        index: docs

---
"Tests searchable snapshots stats":
  - requires:
      cluster_features: ["gte_v7.13.0"]
      reason:  searchable snapshots response format changed in 7.13.0

  - do:
      catch: missing
      searchable_snapshots.stats: {}

  - match: { error.root_cause.0.type: "resource_not_found_exception" }
  - match: { error.root_cause.0.reason: "No searchable snapshots indices found" }

  - do:
      catch: missing
      searchable_snapshots.stats:
        index: _all

  - match: { error.root_cause.0.type: "resource_not_found_exception" }
  - match: { error.root_cause.0.reason: "No searchable snapshots indices found" }

  - do:
      catch:   missing
      searchable_snapshots.stats:
        index: "unknown"

  - do:
      indices.create:
        index: non_searchable_snapshot_index

  - do:
      catch: missing
      searchable_snapshots.stats:
        index: non_*

  - match: { error.root_cause.0.type: "resource_not_found_exception" }
  - match: { error.root_cause.0.reason: "No searchable snapshots indices found" }

  - do:
      searchable_snapshots.mount:
        repository: repository-fs
        snapshot: snapshot
        wait_for_completion: true
        body:
          index: docs

  - match: { snapshot.snapshot: snapshot }
  - match: { snapshot.shards.failed: 0 }
  - match: { snapshot.shards.successful: 1 }

  - do:
      search:
        rest_total_hits_as_int: true
        index: docs
        body:
          query:
            match_all: {}

  - match: { hits.total: 3 }

  - do:
      nodes.info: {}
  - set:
      nodes._arbitrary_key_: node_id

  - do:
      searchable_snapshots.stats:
        index: "d*"
        level: "shards"

  - match:  { _shards.total: 1 }
  - match:  { _shards.failed: 0 }

  - is_true: total
  - is_true: total.0.file_ext
  - gt:      { total.0.num_files: 0 }
  - gt:      { total.0.open_count: 0 }
  - gt:      { total.0.close_count: 0 }

  - gt:      { total.0.size.total_in_bytes: 0 }
  - gt:      { total.0.size.min_in_bytes: 0 }
  - gt:      { total.0.size.max_in_bytes: 0 }
  - gt:      { total.0.size.average_in_bytes: 0 }

  - gte:     { total.0.contiguous_bytes_read.count: 0 }
  - gte:     { total.0.contiguous_bytes_read.sum: 0 }
  - gte:     { total.0.contiguous_bytes_read.min: 0 }
  - gte:     { total.0.contiguous_bytes_read.max: 0 }

  - gte:     { total.0.non_contiguous_bytes_read.count: 0 }
  - gte:     { total.0.non_contiguous_bytes_read.sum: 0 }
  - gte:     { total.0.non_contiguous_bytes_read.min: 0 }
  - gte:     { total.0.non_contiguous_bytes_read.max: 0 }

  - gte:     { total.0.cached_bytes_read.count: 0 }
  - gte:     { total.0.cached_bytes_read.sum: 0 }
  - gte:     { total.0.cached_bytes_read.min: 0 }
  - gte:     { total.0.cached_bytes_read.max: 0 }

  - gte:     { total.0.index_cache_bytes_read.count: 0 }
  - gte:     { total.0.index_cache_bytes_read.sum: 0 }
  - gte:     { total.0.index_cache_bytes_read.min: 0 }
  - gte:     { total.0.index_cache_bytes_read.max: 0 }

  - gte:     { total.0.cached_bytes_written.count: 0 }
  - gte:     { total.0.cached_bytes_written.sum: 0 }
  - gte:     { total.0.cached_bytes_written.min: 0 }
  - gte:     { total.0.cached_bytes_written.max: 0 }
  - gte:     { total.0.cached_bytes_written.time_in_nanos: 0 }
  - is_false:  total.0.cached_bytes_written.time

  - gte:     { total.0.direct_bytes_read.count: 0 }
  - gte:     { total.0.direct_bytes_read.sum: 0 }
  - gte:     { total.0.direct_bytes_read.min: 0 }
  - gte:     { total.0.direct_bytes_read.max: 0 }
  - gte:     { total.0.direct_bytes_read.time_in_nanos: 0 }
  - is_false:  total.0.direct_bytes_read.time

  - gte:     { total.0.optimized_bytes_read.count: 0 }
  - gte:     { total.0.optimized_bytes_read.sum: 0 }
  - gte:     { total.0.optimized_bytes_read.min: 0 }
  - gte:     { total.0.optimized_bytes_read.max: 0 }
  - gte:     { total.0.optimized_bytes_read.time_in_nanos: 0 }
  - is_false:  total.0.optimized_bytes_read.time

  - gte:     { total.0.forward_seeks.small.count: 0 }
  - gte:     { total.0.forward_seeks.small.sum: 0 }
  - gte:     { total.0.forward_seeks.small.min: 0 }
  - gte:     { total.0.forward_seeks.small.max: 0 }
  - gte:     { total.0.forward_seeks.large.count: 0 }
  - gte:     { total.0.forward_seeks.large.sum: 0 }
  - gte:     { total.0.forward_seeks.large.min: 0 }
  - gte:     { total.0.forward_seeks.large.max: 0 }

  - gte:     { total.0.backward_seeks.small.count: 0 }
  - gte:     { total.0.backward_seeks.small.sum: 0 }
  - gte:     { total.0.backward_seeks.small.min: 0 }
  - gte:     { total.0.backward_seeks.small.max: 0 }
  - gte:     { total.0.backward_seeks.large.count: 0 }
  - gte:     { total.0.backward_seeks.large.sum: 0 }
  - gte:     { total.0.backward_seeks.large.min: 0 }
  - gte:     { total.0.backward_seeks.large.max: 0 }

  - gte:     { total.0.blob_store_bytes_requested.count: 0 }
  - gte:     { total.0.blob_store_bytes_requested.sum: 0 }
  - gte:     { total.0.blob_store_bytes_requested.min: 0 }
  - gte:     { total.0.blob_store_bytes_requested.max: 0 }

  - gte:     { total.0.lucene_bytes_read.count: 0 }
  - gte:     { total.0.lucene_bytes_read.sum: 0 }
  - gte:     { total.0.lucene_bytes_read.min: 0 }
  - gte:     { total.0.lucene_bytes_read.max: 0 }

  - gte:     { total.0.current_index_cache_fills: 0 }

  - length:  { indices: 1 }
  - is_true: indices.docs
  - is_true: indices.docs.total

  - is_true: indices.docs.total.0.file_ext
  - gt:      { indices.docs.total.0.num_files: 0 }
  - gt:      { indices.docs.total.0.open_count: 0 }
  - gt:      { indices.docs.total.0.close_count: 0 }

  - gt:      { indices.docs.total.0.size.total_in_bytes: 0 }
  - gt:      { indices.docs.total.0.size.min_in_bytes: 0 }
  - gt:      { indices.docs.total.0.size.max_in_bytes: 0 }
  - gt:      { indices.docs.total.0.size.average_in_bytes: 0 }

  - gte:     { indices.docs.total.0.contiguous_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.contiguous_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.contiguous_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.contiguous_bytes_read.max: 0 }

  - gte:     { indices.docs.total.0.non_contiguous_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.non_contiguous_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.non_contiguous_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.non_contiguous_bytes_read.max: 0 }

  - gte:     { indices.docs.total.0.cached_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_read.max: 0 }

  - gte:     { indices.docs.total.0.index_cache_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.index_cache_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.index_cache_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.index_cache_bytes_read.max: 0 }

  - gte:     { indices.docs.total.0.cached_bytes_written.count: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_written.sum: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_written.min: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_written.max: 0 }
  - gte:     { indices.docs.total.0.cached_bytes_written.time_in_nanos: 0 }
  - is_false:  indices.docs.total.0.cached_bytes_written.time

  - gte:     { indices.docs.total.0.direct_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.direct_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.direct_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.direct_bytes_read.max: 0 }
  - gte:     { indices.docs.total.0.direct_bytes_read.time_in_nanos: 0 }
  - is_false:  indices.docs.total.0.direct_bytes_read.time

  - gte:     { indices.docs.total.0.optimized_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.optimized_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.optimized_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.optimized_bytes_read.max: 0 }
  - gte:     { indices.docs.total.0.optimized_bytes_read.time_in_nanos: 0 }
  - is_false:  indices.docs.total.0.optimized_bytes_read.time

  - gte:     { indices.docs.total.0.forward_seeks.small.count: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.small.sum: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.small.min: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.small.max: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.large.count: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.large.sum: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.large.min: 0 }
  - gte:     { indices.docs.total.0.forward_seeks.large.max: 0 }

  - gte:     { indices.docs.total.0.backward_seeks.small.count: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.small.sum: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.small.min: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.small.max: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.large.count: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.large.sum: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.large.min: 0 }
  - gte:     { indices.docs.total.0.backward_seeks.large.max: 0 }

  - gte:     { indices.docs.total.0.blob_store_bytes_requested.count: 0 }
  - gte:     { indices.docs.total.0.blob_store_bytes_requested.sum: 0 }
  - gte:     { indices.docs.total.0.blob_store_bytes_requested.min: 0 }
  - gte:     { indices.docs.total.0.blob_store_bytes_requested.max: 0 }

  - gte:     { indices.docs.total.0.lucene_bytes_read.count: 0 }
  - gte:     { indices.docs.total.0.lucene_bytes_read.sum: 0 }
  - gte:     { indices.docs.total.0.lucene_bytes_read.min: 0 }
  - gte:     { indices.docs.total.0.lucene_bytes_read.max: 0 }

  - gte:     { indices.docs.total.0.current_index_cache_fills: 0 }

  - length:  { indices.docs.shards: 1 }
  - length:  { indices.docs.shards.0: 1 }
  - is_true: indices.docs.shards.0.0.snapshot_uuid
  - is_true: indices.docs.shards.0.0.index_uuid
  - match:   { indices.docs.shards.0.0.shard.state: STARTED }
  - match:   { indices.docs.shards.0.0.shard.primary: true }
  - match:   { indices.docs.shards.0.0.shard.node: $node_id }

  - is_true: indices.docs.shards.0.0.files.0.file_ext
  - gt:      { indices.docs.shards.0.0.files.0.num_files: 0 }
  - gt:      { indices.docs.shards.0.0.files.0.open_count: 0 }
  - gt:      { indices.docs.shards.0.0.files.0.close_count: 0 }

  - gt:      { indices.docs.shards.0.0.files.0.size.total_in_bytes: 0 }
  - gt:      { indices.docs.shards.0.0.files.0.size.min_in_bytes: 0 }
  - gt:      { indices.docs.shards.0.0.files.0.size.max_in_bytes: 0 }
  - gt:      { indices.docs.shards.0.0.files.0.size.average_in_bytes: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.contiguous_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.contiguous_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.contiguous_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.contiguous_bytes_read.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.non_contiguous_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.non_contiguous_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.non_contiguous_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.non_contiguous_bytes_read.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_read.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.index_cache_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.index_cache_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.index_cache_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.index_cache_bytes_read.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_written.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_written.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_written.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_written.max: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.cached_bytes_written.time_in_nanos: 0 }
  - is_false:  indices.docs.shards.0.0.files.0.cached_bytes_written.time

  - gte:     { indices.docs.shards.0.0.files.0.direct_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.direct_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.direct_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.direct_bytes_read.max: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.direct_bytes_read.time_in_nanos: 0 }
  - is_false:  indices.docs.shards.0.0.files.0.direct_bytes_read.time

  - gte:     { indices.docs.shards.0.0.files.0.optimized_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.optimized_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.optimized_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.optimized_bytes_read.max: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.optimized_bytes_read.time_in_nanos: 0 }
  - is_false:  indices.docs.shards.0.0.files.0.optimized_bytes_read.time

  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.small.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.small.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.small.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.small.max: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.large.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.large.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.large.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.forward_seeks.large.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.small.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.small.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.small.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.small.max: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.large.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.large.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.large.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.backward_seeks.large.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.blob_store_bytes_requested.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.blob_store_bytes_requested.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.blob_store_bytes_requested.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.blob_store_bytes_requested.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.lucene_bytes_read.count: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.lucene_bytes_read.sum: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.lucene_bytes_read.min: 0 }
  - gte:     { indices.docs.shards.0.0.files.0.lucene_bytes_read.max: 0 }

  - gte:     { indices.docs.shards.0.0.files.0.current_index_cache_fills: 0 }

  - do:
      searchable_snapshots.stats:
        index: "d*"
        level: "shards"
        human: true

  - is_true:   indices.docs.shards.0.0.files.0.cached_bytes_written.time
  - is_true:   indices.docs.shards.0.0.files.0.direct_bytes_read.time
  - is_true:   indices.docs.shards.0.0.files.0.size.total
  - is_true:   indices.docs.shards.0.0.files.0.size.min
  - is_true:   indices.docs.shards.0.0.files.0.size.max
  - is_true:   indices.docs.shards.0.0.files.0.size.average

  - do:
      searchable_snapshots.stats:
        index: "d*"
        level: "cluster"

  - is_true: total.0.file_ext
  - is_false: indices.docs.total.0.file_ext
  - is_false: indices.docs.shards.0.0.files.0.file_ext

  - do:
      searchable_snapshots.stats:
        index: "d*"

  - is_true: total.0.file_ext
  - is_true: indices.docs.total.0.file_ext
  - is_false: indices.docs.shards.0.0.files.0.file_ext
