How to add new field to existing docs


#1

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' -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."
	}

#2

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?


(Abdon Pijpelink) #3

_source is a HashMap 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'];"


#4

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
}


(Abdon Pijpelink) #5

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

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


#6

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


(system) #7

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