# Copy nested fields to new nested fields via Reindex or Update API

**URL:** <https://discuss.elastic.co/t/copy-nested-fields-to-new-nested-fields-via-reindex-or-update-api/244897>\
**Category:** Elasticsearch\
**Created:** [August 13, 2020, 2:16pm UTC](https://discuss.elastic.co/t/copy-nested-fields-to-new-nested-fields-via-reindex-or-update-api/244897 "2020-08-13T14:16:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![g30](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@g30](https://discuss.elastic.co/u/g30)\
**Post date:** [August 13, 2020, 2:16pm UTC](https://discuss.elastic.co/t/copy-nested-fields-to-new-nested-fields-via-reindex-or-update-api/244897/1 "2020-08-13T14:16:31Z")

</div>

Hi,

We have the following data stored in our existing index.

```
{
	"id": "83c55df4-dd6d-11ea-87d0-0242ac130003",
	"name": "test",
	"customData": {
		"id_int": 16934,
		"year": "2020",
		"number": "0001",
		"anotherField": "anotherValue",
		"anotherField2": "anotherValue2",
	},
	"testFeild": false,
	"regNumbers": [
		"cccccc"
	],
	"types": [
		"test"
	]
}

```

We now want to change this data, update the mappings and copying some of the fields into new fields and removing them once done, could something like this be achieved with the Reindex or Update API or would it need some sort of custom written approach? So moving some fields from customData to new fields in customDetails

```
   {
    	"id": "83c55df4-dd6d-11ea-87d0-0242ac130003",
    	"name": "test",
    	"customData": {
    		"anotherField": "anotherValue",
    		"anotherField2": "anotherValue2",
    	},
    	"customDetails": {
    		"id_int": 16934,
    		"year": "2020",
    		"number": "0001",
    	},
    	"testFeild": false,
    	"regNumbers": [
    		"cccccc"
    	],
    	"types": [
    		"test"
    	]
    }

```

I had attempted something with the Reindex API but kept getting a null pointer.

```
{
  "source": {
    "index": "dev204113",
    "query": {
      "match_all" : {}
    }
  },
  "dest": {
    "index": "dev204113_test"
  },
  "script": {
    "source": "ctx._source.customData.id_int=ctx._source.customDetails.id_int",
    "lang": "painless"
  }
}
```

---

<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:** [September 10, 2020, 2:16pm UTC](https://discuss.elastic.co/t/copy-nested-fields-to-new-nested-fields-via-reindex-or-update-api/244897/2 "2020-09-10T14:16:54Z")

</div>

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