---
setup:
  - do:
      indices.create:
        index: source
        body:
          mappings:
            properties:
              baz:
                type: keyword
              a:
                type: keyword
              b:
                type: keyword
          aliases:
            myalias: {}

  - do:
      bulk:
        refresh: true
        index: source
        body:
          - '{"index": {}}'
          - '{"baz": "quick", "a": "brown", "b": "fox"}'
          - '{"index": {}}'
          - '{"baz": "lazy", "a": "dog"}'

  - do:
      enrich.put_policy:
        name: test_policy
        body:
          match:
            indices: ["source*"]
            match_field: baz
            enrich_fields: ["a", "b"]

  - do:
      enrich.execute_policy:
        name: test_policy

  - do:
      ingest.put_pipeline:
        id: test_pipeline
        body:
          processors:
            - enrich:
                policy_name: test_policy
                field: baz
                target_field: target

---
teardown:
  - do:
      ingest.delete_pipeline:
        id: test_pipeline

  - do:
      enrich.delete_policy:
        name: test_policy

---
enrich documents over _bulk:
  - do:
      bulk:
        refresh: true
        index: target
        pipeline: test_pipeline
        body:
          - '{"index": {"_id": "1"}}'
          - '{"baz": "quick", "c": 1}'
          - '{"index": {"_id": "2"}}'
          - '{"baz": "lazy", "c": 2}'
          - '{"index": {"_id": "3"}}'
          - '{"baz": "slow", "c": 3}'

  - do:
      get:
        index: target
        id: "1"
  - match:
      _source:
        baz: quick
        target:
          baz: quick
          a: brown
          b: fox
        c: 1

  - do:
      get:
        index: target
        id: "2"
  - match:
      _source:
        baz: lazy
        target:
          baz: lazy
          a: dog
        c: 2

  - do:
      get:
        index: target
        id: "3"
  - match:
      _source:
        baz: slow
        c: 3

---
enrich documents over _bulk via an alias:
  - do:
      enrich.put_policy:
        name: test_alias_policy
        body:
          match:
            indices: ["myalias"]
            match_field: baz
            enrich_fields: ["a", "b"]

  - do:
      enrich.execute_policy:
        name: test_alias_policy

  - do:
      ingest.put_pipeline:
        id: test_alias_pipeline
        body:
          processors:
            - enrich:
                policy_name: test_alias_policy
                field: baz
                target_field: target


  - do:
      bulk:
        refresh: true
        index: target
        pipeline: test_alias_pipeline
        body:
          - '{"index": {"_id": "1"}}'
          - '{"baz": "quick", "c": 1}'
          - '{"index": {"_id": "2"}}'
          - '{"baz": "lazy", "c": 2}'
          - '{"index": {"_id": "3"}}'
          - '{"baz": "slow", "c": 3}'
  - match: { errors: false }

  - do:
      get:
        index: target
        id: "1"
  - match:
      _source:
        baz: quick
        target:
          baz: quick
          a: brown
          b: fox
        c: 1

  - do:
      get:
        index: target
        id: "2"
  - match:
      _source:
        baz: lazy
        target:
          baz: lazy
          a: dog
        c: 2

  - do:
      get:
        index: target
        id: "3"
  - match:
      _source:
        baz: slow
        c: 3

  - do:
      ingest.delete_pipeline:
        id: test_alias_pipeline

  - do:
      enrich.delete_policy:
        name: test_alias_policy

---
enrich stats REST response structure:
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: GET
          path: /_enrich/stats
          capabilities:
            - size-in-bytes
      reason: "Capability required to run test"

  - do:
      ingest.simulate:
        id: test_pipeline
        body: >
          {
            "docs": [
              {
                "_index": "enrich-cache-stats-index",
                "_id": "1",
                "_source": {"baz": "quick", "c": 1}
              },
              {
                "_index": "enrich-cache-stats-index",
                "_id": "2",
                "_source": {"baz": "lazy", "c": 2}
              },
              {
                "_index": "enrich-cache-stats-index",
                "_id": "3",
                "_source": {"baz": "slow", "c": 3}
              }
            ]
          }
  - length: { docs: 3 }

  # This test's main purpose is to verify the REST response structure.
  # So, rather than assessing specific values, we only assess the existence of fields.
  - do:
      enrich.stats: {}
  - exists: executing_policies
  - is_true: coordinator_stats
  # We know there will be at least one node, but we don't want to be dependent on the exact number of nodes.
  - is_true: coordinator_stats.0.node_id
  - exists: coordinator_stats.0.queue_size
  - exists: coordinator_stats.0.remote_requests_current
  - exists: coordinator_stats.0.remote_requests_total
  - exists: coordinator_stats.0.executed_searches_total
  - is_true: cache_stats
  - is_true: cache_stats.0.node_id
  - exists: cache_stats.0.count
  - exists: cache_stats.0.hits
  - exists: cache_stats.0.misses
  - exists: cache_stats.0.evictions
  - exists: cache_stats.0.hits_time_in_millis
  - exists: cache_stats.0.misses_time_in_millis
  - exists: cache_stats.0.size_in_bytes
