# Add Runtime field to change Mapping of a Field

**URL:** <https://discuss.elastic.co/t/add-runtime-field-to-change-mapping-of-a-field/345373>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [October 19, 2023, 9:36am UTC](https://discuss.elastic.co/t/add-runtime-field-to-change-mapping-of-a-field/345373 "2023-10-19T09:36:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuele\_Lolli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samuele_lolli/32/107848_2.png) [@Samuele\_Lolli](https://discuss.elastic.co/u/Samuele_Lolli)\
**Post date:** [October 19, 2023, 9:36am UTC](https://discuss.elastic.co/t/add-runtime-field-to-change-mapping-of-a-field/345373/1 "2023-10-19T09:36:22Z")

</div>

Hi everyone!  
I need to change the mapping of a current field because is currentry a text field and i need to be a keyword.  
The field path is event.userDefined.headerId  
The currentry mapping has some dynamic templates:

```auto
          "userdefined-string": {
            "path_match": "event.userDefined.*",
            "match_mapping_type": "string",
            "mapping": {
              "type": "text"
            }
          }
        },
        {
          "userdefined-long": {
            "path_match": "event.userDefined.*",
            "match_mapping_type": "long",
            "mapping": {
              "type": "integer"
            }
          }
        }

```

and i need to change the field header id inside event.userDefined to keyword

```auto
"userDefined": {
  "parameters": {
    "properties": {
      "error_code": {
      "type": "text"
      },
      "error_description": {
      "type": "text"
      },
      "headerId": {
      "type": "text"
      }
    }
    },
    "query": {
    "type": "text"
    }
  }
}

```

I created the following runtime field to do that but is not currently working

```auto
"runtime": {
      "event.userDefined.headerId": {
        "type": "keyword",
        "script": {
          "source": "emit(doc['event.userDefined.headerId'].value)",
          "lang": "painless"
        }
      }
    },e

```

Please help

Thanks in advance

---

<div class="post-metadata">

**Author:** ![azizim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azizim/32/127005_2.png) [@azizim](https://discuss.elastic.co/u/azizim)\
**Post date:** [October 27, 2023, 7:01pm UTC](https://discuss.elastic.co/t/add-runtime-field-to-change-mapping-of-a-field/345373/2 "2023-10-27T19:01:25Z")

</div>

Hello  
You can not change the mapping of already indexed documents you will have to reindex them

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 27, 2023, 8:17pm UTC](https://discuss.elastic.co/t/add-runtime-field-to-change-mapping-of-a-field/345373/3 "2023-10-27T20:17:04Z")

</div>

I think you can do something like:

```auto
"runtime": {
      "event.userDefined.headerId.keyword": {
        "type": "keyword",
        "script": {
          "source": "emit(doc['event.userDefined.headerId'].value)",
          "lang": "painless"
        }
      }
    },

```

And then run your queries/aggs against `event.userDefined.headerId.keyword`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 24, 2023, 8:17pm UTC](https://discuss.elastic.co/t/add-runtime-field-to-change-mapping-of-a-field/345373/4 "2023-11-24T20:17:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
