# Can I add a field to a mapping via groovy script?

**URL:** <https://discuss.elastic.co/t/can-i-add-a-field-to-a-mapping-via-groovy-script/55082>\
**Category:** Elasticsearch\
**Created:** [July 8, 2016, 6:29pm UTC](https://discuss.elastic.co/t/can-i-add-a-field-to-a-mapping-via-groovy-script/55082 "2016-07-08T18:29:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jai.srivastav](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jai.srivastav](https://discuss.elastic.co/u/jai.srivastav)\
**Post date:** [July 8, 2016, 6:29pm UTC](https://discuss.elastic.co/t/can-i-add-a-field-to-a-mapping-via-groovy-script/55082/1 "2016-07-08T18:29:06Z")

</div>

I've had a bit of trouble with scripting as I couldn't find much documentation. My problem is that I need to programmatically add a field that is a list of objects. I found out [here](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/nested.html) that the field will not behave as expected unless it has a nested type. With my groovy script I can set the field to its new value `ctx._source.field = [object]` but this does not give the field a nested type.

Long question short: Is there a way to add a field with a specific type to a mapping in a groovy script?

---

<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:** [July 5, 2017, 10:36pm UTC](https://discuss.elastic.co/t/can-i-add-a-field-to-a-mapping-via-groovy-script/55082/2 "2017-07-05T22:36:56Z")

</div>

Closed on July 5, 2017, 10:36pm UTC


