---
"Response format for created":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      indices.refresh: {}

  - do:
      reindex:
        body:
          source:
            index: source
          dest:
            index: dest
  - match: {created: 1}
  - match: {updated: 0}
  - match: {version_conflicts: 0}
  - match: {batches: 1}
  - match: {failures: []}
  - match: {throttled_millis: 0}
  - gte: { took: 0 }
  - is_false: task
  - is_false: deleted

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}

---
"Response format for updated":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      index:
        index:   dest
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      indices.refresh: {}

  - do:
      reindex:
        body:
          source:
            index:   source
          dest:
            index:   dest
  - match: {created: 0}
  - match: {updated: 1}
  - match: {version_conflicts: 0}
  - match: {batches: 1}
  - match: {failures: []}
  - match: {throttled_millis: 0}
  - gte: { took: 0 }
  - is_false: task
  - is_false: deleted

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}

---
"wait_for_completion=false":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      indices.refresh: {}

  - do:
      reindex:
        wait_for_completion: false
        body:
          source:
            index: source
          dest:
            index: dest
  - is_false: timed_out
  - match: {task: '/.+:\d+/'}
  - set: {task: task}
  - is_false: updated
  - is_false: version_conflicts
  - is_false: batches
  - is_false: failures
  - is_false: noops
  - is_false: took
  - is_false: throttled_millis
  - is_false: created
  - is_false: deleted

  - do:
      tasks.get:
        wait_for_completion: true
        task_id: $task
  - is_false: node_failures
  # The task will be in the response even if it finished before we got here
  # because of task persistence.
  - is_true: task
  - match: {response.created: 1}
  - match: {response.updated: 0}
  - match: {response.version_conflicts: 0}
  - match: {response.batches: 1}
  - match: {response.failures: []}
  - match: {response.throttled_millis: 0}
  - gte: { response.took: 0 }
  - is_false: response.task
  - is_false: response.deleted

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}

---
"Response format for version conflict":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      index:
        index:   dest
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      indices.refresh: {}

  - do:
      catch: conflict
      reindex:
        body:
          source:
            index:   source
          dest:
            index:   dest
            op_type: create
  - match: {created: 0}
  - match: {updated: 0}
  - match: {version_conflicts: 1}
  - match: {batches: 1}
  - match: {failures.0.index:  dest}
  - match: {failures.0.type:   foo}
  - match: {failures.0.id:     "1"}
  - match: {failures.0.status: 409}
  - match: {failures.0.cause.type:   version_conflict_engine_exception}
  # Use a regex so we don't mind if the version isn't always 1. Sometimes it comes out 2.
  - match: {failures.0.cause.reason: "/\\[foo\\]\\[1\\]:.version.conflict,.document.already.exists.\\(current.version.\\[\\d+\\]\\)/"}
  - match: {failures.0.cause.shard:  /\d+/}
  - match: {failures.0.cause.index:  dest}
  - gte: { took: 0 }

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}

---
"Response format for version conflict with conflicts=proceed":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      index:
        index:   dest
        type:    foo
        id:      1
        body:    { "text": "test" }
  - do:
      indices.refresh: {}

  - do:
      reindex:
        body:
          conflicts: proceed
          source:
            index:   source
          dest:
            index:   dest
            op_type: create
  - match: {created: 0}
  - match: {updated: 0}
  - match: {version_conflicts: 1}
  - match: {batches: 1}
  - match: {failures: []}
  - match: {throttled_millis: 0}
  - gte: { took: 0 }

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}

---
"Source document without any fields works":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    {}
  - do:
      indices.refresh: {}

  - do:
      reindex:
        body:
          source:
            index: source
          dest:
            index: dest
  - match: {created: 1}

  - do:
      get:
        index:   dest
        type:    foo
        id:      1
  - match: { _source: {} }

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}

---
"Reindex with source filtering":
  - do:
      index:
        index:   source
        type:    foo
        id:      1
        body:    { "text": "test", "filtered": "removed" }
        refresh: true

  - do:
      reindex:
        refresh: true
        body:
          source:
            index: source
            _source:
              excludes:
                - filtered
          dest:
            index: dest
  - match: {created: 1}
  - match: {updated: 0}
  - match: {version_conflicts: 0}
  - match: {batches: 1}
  - match: {failures: []}
  - match: {throttled_millis: 0}
  - gte: { took: 0 }
  - is_false: task
  - is_false: deleted

  - do:
      get:
        index: dest
        type: foo
        id: 1
  - match: { _source.text: "test" }
  - is_false: _source.filtered

  # Make sure reindex closed all the scroll contexts
  - do:
      indices.stats:
        index: source
        metric: search
  - match: {indices.source.total.search.open_contexts: 0}
