# Copy values from one field to another within a nested object

**URL:** <https://discuss.elastic.co/t/copy-values-from-one-field-to-another-within-a-nested-object/293478>\
**Category:** Elasticsearch\
**Created:** [January 4, 2022, 7:51pm UTC](https://discuss.elastic.co/t/copy-values-from-one-field-to-another-within-a-nested-object/293478 "2022-01-04T19:51:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisdeveloper](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@louisdeveloper](https://discuss.elastic.co/u/louisdeveloper)\
**Post date:** [January 4, 2022, 7:51pm UTC](https://discuss.elastic.co/t/copy-values-from-one-field-to-another-within-a-nested-object/293478/1 "2022-01-04T19:51:02Z")

</div>

I have a document that has the structure with an field object with a nested field internally. The nested field is responsible for storing all interactions that occurred in an internal communication.  
It happens that I need to create a new field inside the nested field, with a new type that will now be used to store the old field with a new parser.

How can I copy the data from the old field to the new field inside the nested field?

My document:

```auto

curl -XPUT 'localhost:9200/problems?pretty' -H 'Content-Type: application/json' -d '
{
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "problem": {
            "properties": {
                "problemid": {
                    "type": "long"
                },
                "subject": {
                    "type": "text",
                    "index": true
                },
                "usermessage": {
                    "type": "object",
                    "properties": {
                        "content": {
                            "type": "nested",
                            "properties": {
                                "messageid": {
                                    "type": "long",
                                    "index": true
                                },
                                "message": {
                                    "type": "text",
                                    "index": true
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}'

```

My New Field:

```auto
curl -XPUT 'localhost:9200/problems/_mapping/problem?pretty' -H 'Content-Type: application/json' -d '
{
    "properties": {
        "usermessage": {
            "type": "object",
            "properties": {
                "content": {
                    "type": "nested",
                    "properties": {
                        "message_accents" : {
                            "type" : "text",
                            "analyzer" : "ignoreaccents"
                        }
                    }
                }
            }
        }
    }
}
'

Data Example:

{
    "problemid": 1,
    "subject": "Test",
    "usermessage": [
        {
            "messageid": 1
            "message": "Hello"
        },
        {
            "messageid": 2
            "message": "Its me"
        },
    ]
}'

```

My script to copy fields:

```auto
curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_all": {
        }
    },
    "script": "ctx._source.usermessage.content.message_accents = ctx._source.usermessage.content.message"
}'

```

My script should probably go through the entire array, but I don't know how to do this.

**Edit:**

I tried the code below but it didn't work, it returns an error.

```auto
curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_all": {
        }
    },
    "script": "ctx._source.usermessage.content.each { elm -> elm.message_accents = elm.message }"
}
'

```

> "script":"ctx.\_source.usermessage.content.each { elm -\> elm.message\_accents = elm.message }","lang":"painless","caused\_by":{"type":"illegal\_argument\_exception","reason":"unexpected token ['{'] was expecting one of [{, ';'}]."}},"status":500}%

---

<div class="post-metadata">

**Author:** ![louisdeveloper](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@louisdeveloper](https://discuss.elastic.co/u/louisdeveloper)\
**Post date:** [January 5, 2022, 2:18pm UTC](https://discuss.elastic.co/t/copy-values-from-one-field-to-another-within-a-nested-object/293478/2 "2022-01-05T14:18:45Z")

</div>

Solved:

```auto
curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_all": {
        }
    },
    "script": "for(int i = 0; i < ctx._source.usermessage[0].content.length; i++) {ctx._source.usermessage[0].content[i].message_accents = ctx._source.usermessage[0].content[i].message}"
}
'

```

Due to my lack of knowledge, the `usermessage` field is an array, and within that array there is another array (`content`) which is where the values ​​are, so I must set index 0 by default.

---

<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:** [February 2, 2022, 2:19pm UTC](https://discuss.elastic.co/t/copy-values-from-one-field-to-another-within-a-nested-object/293478/3 "2022-02-02T14:19:18Z")

</div>

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