setup:
  - skip:
      features: headers
---
"Test new fields are mapped as keyword":

  - do:
      ml.put_job:
        job_id: ml-anomalies-default-mappings-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"count","by_field_name":"foo"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }
  - match: { job_id: "ml-anomalies-default-mappings-job" }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
        Content-Type: application/json
      index:
        index:  .ml-anomalies-shared-000001
        id:     "new_doc"
        body: >
          {
            "new_field": "bar"
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.refresh:
        index: .ml-anomalies-shared-000001

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.get_field_mapping:
        index: .ml-anomalies-shared-000001
        fields: new_field
  - match: {\.ml-anomalies-shared-000001.mappings.new_field.mapping.new_field.type: keyword}

---
"Test _meta exists when two jobs share an index":

  - do:
      ml.put_job:
        job_id: ml-anomalies-shared-mappings-job1
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"count","by_field_name":"foo"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }
  - match: { job_id: "ml-anomalies-shared-mappings-job1" }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.refresh:
        index: .ml-anomalies-shared-000001

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.get_mapping:
        index: .ml-anomalies-shared-000001
  - is_true: \.ml-anomalies-shared-000001.mappings._meta.version

  - do:
      ml.put_job:
        job_id: ml-anomalies-shared-mappings-job2
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"count","by_field_name":"bar"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }
  - match: { job_id: "ml-anomalies-shared-mappings-job2" }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.refresh:
        index: .ml-anomalies-shared-000001

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.get_mapping:
        index: .ml-anomalies-shared-000001
  - is_true: \.ml-anomalies-shared-000001.mappings._meta.version
