# Cannot add runtime field to an existing index

**URL:** <https://discuss.elastic.co/t/cannot-add-runtime-field-to-an-existing-index/375577>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [March 7, 2025, 9:07pm UTC](https://discuss.elastic.co/t/cannot-add-runtime-field-to-an-existing-index/375577 "2025-03-07T21:07:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wpm/32/110146_2.png) [@wpm](https://discuss.elastic.co/u/wpm)\
**Post date:** [March 7, 2025, 9:07pm UTC](https://discuss.elastic.co/t/cannot-add-runtime-field-to-an-existing-index/375577/1 "2025-03-07T21:07:46Z")

</div>

I am trying to add a runtime field following the example from "[Map a runtime field](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html)" in the documentation.

```json
PUT my-index-000001/
{
  "mappings": {
    "runtime": {
      "day_of_week": {
        "type": "keyword",
        "script": {
          "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"
        }
      }
    },
    "properties": {
      "@timestamp": {"type": "date"}
    }
  }
}

```

The index `my-index-000001` already exists. This command fails with a `resource_already_exists_exception` error. I expect it to succeed and add a `day_of_week` field to the index.

Why is this happening? The `dynamic` property is not set anywhere in `my-index-000001`, and I thought that dynamic field mapping was turned on by default.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [March 7, 2025, 11:15pm UTC](https://discuss.elastic.co/t/cannot-add-runtime-field-to-an-existing-index/375577/2 "2025-03-07T23:15:42Z")

</div>

I think you need to use the \_mapping endpoint?

Here's my test, version 8.17.2

```auto
DELETE an-index-000001

PUT an-index-000001

PUT an-index-000001/_doc/0
{
  "field1" : "value0",
  "@timestamp" : "2025-03-06T23:10:20+00:00"
}

PUT an-index-000001/_mapping
{
  "runtime": {
    "day_of_week": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"
      }
    }
  },
  "properties": {
    "@timestamp": {
      "type": "date"
    }
  }
}

PUT an-index-000001/_doc/1
{
  "field1" : "value1",
  "@timestamp" : "2025-03-07T23:10:25+00:00"
}

GET an-index-000001/_search
{
  "fields": ["field1" , "@timestamp" , "day_of_week"]
}

```

The search returns

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "an-index-000001",
        "_id": "0",
        "_score": 1,
        "_source": {
          "field1": "value0",
          "@timestamp": "2025-03-06T23:10:20+00:00"
        },
        "fields": {
          "field1": [
            "value0"
          ],
          "@timestamp": [
            "2025-03-06T23:10:20.000Z"
          ],
          "day_of_week": [
            "Thursday"
          ]
        }
      },
      {
        "_index": "an-index-000001",
        "_id": "1",
        "_score": 1,
        "_source": {
          "field1": "value1",
          "@timestamp": "2025-03-07T23:10:25+00:00"
        },
        "fields": {
          "field1": [
            "value1"
          ],
          "@timestamp": [
            "2025-03-07T23:10:25.000Z"
          ],
          "day_of_week": [
            "Friday"
          ]
        }
      }
    ]
  }
}

```
