---
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: doc
          - index:
              _index: docs
              _id:    "2"
          - field: doc
          - index:
              _index: docs
              _id:    "3"
          - field: other

  - 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

---
"Clear searchable snapshots cache":
  - requires:
      cluster_features: ["gte_v7.8.0"]
      reason:  searchable snapshots introduced in 7.8.0

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

  - 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.clear_cache:
        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.clear_cache:
        index: "unknown"

  - do:
      indices.create:
        index: non_searchable_snapshot_index

  - do:
      catch: missing
      searchable_snapshots.clear_cache:
        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:
              field: "doc"

  - match: { hits.total: 2 }

  - do:
      searchable_snapshots.clear_cache:
        index: "docs"

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

