---
teardown:
  - do:
      indices.delete:
        index: "test"
        ignore_unavailable: true
  - do:
      ingest.delete_pipeline:
        id: "my_pipeline"
        ignore: 404

---
"Test Drop Processor":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body: >
          {
            "description" : "pipeline with drop",
            "processors" : [
              {
                "drop" : {
                  "if": "ctx.foo == 'bar'"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {
          foo: "bar"
        }

  - do:
      index:
        index: test
        id: "2"
        pipeline: "my_pipeline"
        body: {
          foo: "blub"
        }

  - do:
      catch: missing
      get:
        index: test
        id: "1"
  - match: { found: false }

  - do:
      get:
        index: test
        id: "2"
  - match: { _source.foo: "blub" }

---
"Test Drop Processor On Failure":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline_with_failure"
        body: >
          {
            "description" : "pipeline with on failure drop",
            "processors": [
                {
                  "fail": {
                    "message": "failed",
                    "on_failure": [
                      {
                        "drop": {}
                      }
                    ]
                  }
                }
              ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "3"
        pipeline: "my_pipeline_with_failure"
        body: {
          foo: "bar"
        }

  - do:
      catch: missing
      get:
        index: test
        id: "3"

---
"Test Drop Processor with Upsert (_bulk)":
  - requires:
      cluster_features: "gte_v8.12.1"
      reason: 'https://github.com/elastic/elasticsearch/issues/36746 fixed in 8.12.1'
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body: >
          {
            "processors": [
              {
                "drop": {
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      bulk:
        refresh: true
        pipeline: "my_pipeline"
        body:
          - update:
              _index: test
              _id: 4
          - '{"upsert":{"some":"fields"},"script":"ctx"}'
  - match: { errors: false }
  - match: { items.0.update._index: test }
  - match: { items.0.update._id: "4" }
  - match: { items.0.update._version: -3 }
  - match: { items.0.update.result: noop }
  - match: { items.0.update.status: 200 }

  - do:
      catch: missing
      get:
        index: test
        id: "4"

---
"Test Drop Processor with Upsert (_update)":
  - requires:
      cluster_features: "gte_v8.12.1"
      reason: 'https://github.com/elastic/elasticsearch/issues/36746 fixed in 8.12.1'
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body: >
          {
            "processors": [
              {
                "drop": {
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      indices.create:
        index: test
        body:
          settings:
            index:
              default_pipeline: "my_pipeline"

  - do:
      update:
        index: test
        id: "5"
        body:
          script:
            source: "ctx._source.foo = 'bar'"
          upsert:
            foo: "bar"

  - match: { _index: test }
  - match: { _id: "5" }
  - match: { result: noop }

  - do:
      catch: missing
      get:
        index: test
        id: "5"
