# How to add new field to existing docs

**URL:** <https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135>\
**Category:** Elasticsearch\
**Created:** [January 4, 2018, 5:34pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135 "2018-01-04T17:34:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dh2001](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@dh2001](https://discuss.elastic.co/u/dh2001)\
**Post date:** [January 4, 2018, 5:34pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/1 "2018-01-04T17:34:34Z")

</div>

I tried using \_reindex, but I get an error. I want to add a new field and initialize it to an existing value. I added the new field to the mapping and I created the dest index.

curl -s -XPOST '[http://localhost:9200/\_reindex](http://localhost:9200/_reindex)' -H 'Content-Type: application/json' -d "{  
"source": {  
"index": "oldindex"  
},  
"dest": {  
"index": "newindex"  
},  
"script": {  
"inline": "ctx.\_source.new-name = ctx.\_source.orig-name;"  
}  
}"

```
	"caused_by": {
		"type": "illegal_argument_exception",
		"reason": "Left-hand side cannot be assigned a value."
	}
```

---

<div class="post-metadata">

**Author:** ![dh2001](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@dh2001](https://discuss.elastic.co/u/dh2001)\
**Post date:** [January 4, 2018, 8:59pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/2 "2018-01-04T20:59:31Z")

</div>

Is it because we have dashes in our field names? I played around a bit more and it works if I use field names without dashes. Does painless not support dashes? Is there another way to do this?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [January 5, 2018, 2:04pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/3 "2018-01-05T14:04:16Z")

</div>

`_source` is [a HashMap](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/modules-scripting-painless-syntax.html#painless-maps) in Painless. For keys containing special characters, like your `-`, you can use square bracket notation to access the keys. Try accessing your fields like this:

`"ctx._source['new-name'] = ctx._source['session'];"`

---

<div class="post-metadata">

**Author:** ![dh2001](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@dh2001](https://discuss.elastic.co/u/dh2001)\
**Post date:** [January 5, 2018, 2:11pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/4 "2018-01-05T14:11:37Z")

</div>

Thank you Abdon. I was trying something like that, but I had left the period between source and [. I tried it like your example and got this error. Seems it is still parsing the name and not liking the dash.

"inline": "ctx.\_source['new-name'] = ctx.\_source['orig-name']"

{  
"error": {  
"root\_cause": [{  
"type": "script\_exception",  
"reason": "compile error",  
"script\_stack": ["ctx.\_source[new-name] = ct ...",  
" ^---- HERE"],  
"script": "ctx.\_source[new-name] = ctx.\_source[orig-name]",  
"lang": "painless"  
}],  
"type": "script\_exception",  
"reason": "compile error",  
"script\_stack": ["ctx.\_source[new-name] = ct ...",  
" ^---- HERE"],  
"script": "ctx.\_source[new-name] = ctx.\_source[orig-name]",  
"lang": "painless",  
"caused\_by": {  
"type": "illegal\_argument\_exception",  
"reason": "Variable [new] is not defined."  
}  
},  
"status": 500  
}

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [January 5, 2018, 2:50pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/5 "2018-01-05T14:50:43Z")

</div>

Can you try escaped double quotes instead of single quotes. Like this:

`"ctx._source[\"new-name\"] = ctx._source[\"orig-name\"];"`

---

<div class="post-metadata">

**Author:** ![dh2001](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@dh2001](https://discuss.elastic.co/u/dh2001)\
**Post date:** [January 5, 2018, 3:07pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/6 "2018-01-05T15:07:50Z")

</div>

Success! The one combination that I didn't try. Thank you very much.

---

<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, 2018, 3:08pm UTC](https://discuss.elastic.co/t/how-to-add-new-field-to-existing-docs/114135/7 "2018-02-02T15:08:08Z")

</div>

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