title: Copy field value to a new field in existing index
body: <p>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.</p>
  <p>How can I copy the data from the old field to the new field inside the nested field?</p>
  <p>My document:</p>
  <pre><code>curl -XPUT 'localhost:9200/problems?pretty' -H 'Content-Type: application/json' -d '
  {
      &quot;settings&quot;: {
          &quot;number_of_shards&quot;: 1
      },
      &quot;mappings&quot;: {
          &quot;problem&quot;: {
              &quot;properties&quot;: {
                  &quot;problemid&quot;: {
                      &quot;type&quot;: &quot;long&quot;
                  },
                  &quot;subject&quot;: {
                      &quot;type&quot;: &quot;text&quot;,
                      &quot;index&quot;: true
                  },
                  &quot;usermessage&quot;: {
                      &quot;type&quot;: &quot;object&quot;,
                      &quot;properties&quot;: {
                          &quot;content&quot;: {
                              &quot;type&quot;: &quot;nested&quot;,
                              &quot;properties&quot;: {
                                  &quot;messageid&quot;: {
                                      &quot;type&quot;: &quot;long&quot;,
                                      &quot;index&quot;: true
                                  },
                                  &quot;message&quot;: {
                                      &quot;type&quot;: &quot;text&quot;,
                                      &quot;index&quot;: true
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }'
  </code></pre>
  <p>My New Field:</p>
  <pre><code>curl -XPUT 'localhost:9200/problems/_mapping/problem?pretty' -H 'Content-Type: application/json' -d '
  {
      &quot;properties&quot;: {
          &quot;usermessage&quot;: {
              &quot;type&quot;: &quot;object&quot;,
              &quot;properties&quot;: {
                  &quot;content&quot;: {
                      &quot;type&quot;: &quot;nested&quot;,
                      &quot;properties&quot;: {
                          &quot;message_accents&quot; : {
                              &quot;type&quot; : &quot;text&quot;,
                              &quot;analyzer&quot; : &quot;ignoreaccents&quot;
                          }
                      }
                  }
              }
          }
      }
  }
  '
  </code></pre>
  <p>Data Example:</p>
  <pre><code>{
      &quot;problemid&quot;: 1,
      &quot;subject&quot;: &quot;Test&quot;,
      &quot;usermessage&quot;: [
          {
              &quot;messageid&quot;: 1
              &quot;message&quot;: &quot;Hello&quot;
          },
          {
              &quot;messageid&quot;: 2
              &quot;message&quot;: &quot;Its me&quot;
          },
      ]
  }'
  </code></pre>
  <p>My script to copy fields:</p>
  <pre><code>curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
  {
      &quot;query&quot;: {
          &quot;match_all&quot;: {
          }
      },
      &quot;script&quot;: &quot;ctx._source.usermessage.content.message_accents = ctx._source.usermessage.content.message&quot;
  }'
  </code></pre>
  <p>I tried the code below but it didn't work, it returns an error.</p>
  <pre><code>curl -XPOST 'localhost:9200/problems/_update_by_query' -H 'Content-Type: application/json' -d '
  {
      &quot;query&quot;: {
          &quot;match_all&quot;: {
          }
      },
      &quot;script&quot;: &quot;ctx._source.usermessage.content.each { elm -&gt; elm.message_accents = elm.message }&quot;
  }
  '
  </code></pre>
  <p>Error:</p>
  <pre><code>&quot;script&quot;:&quot;ctx._source.usermessage.content.each { elm -&gt; elm.message_accents = elm.message }&quot;,&quot;lang&quot;:&quot;painless&quot;,&quot;caused_by&quot;:{&quot;type&quot;:&quot;illegal_argument_exception&quot;,&quot;reason&quot;:&quot;unexpected token ['{'] was expecting one of [{, ';'}].&quot;}},&quot;status&quot;:500}%
  </code></pre>