Reindex with field value converted to uppercase

I am trying to migrate from ES 1.4 to ES 5.5. In one of the index I need to change the name of field and also convert it's value to uppercase. I am able to reindex with change in name of field and remove the unwanted field but need help in converting the value to uppercase.

This is what I tried

POST _reindex?wait_for_completion=false
  "source": {
	"remote": {
	  "host": "http://source_ip:17002"
	"index": "log_event_2017-09-11",
	"size": 1000,
	"query": {
	  "match_all": {}
  "dest": {
	"index": "logs-ics-2017-09-11"
  "script": {
	"inline": "ctx._source.product = ctx._source.remove(\"product_name\")",
	"lang": "painless"

The above POST request is able to remove "product_name" and create "product" with it's value. So in order to uppercase "product" value I tried below inline script but it gives a null_pointer_exception. Please help

"ctx._source.product = ctx._source.remove(\"product_name\");ctx._source.product = doc[\"product\"].toUpperCase()"

You should not use the doc[\"product\"] syntax when reindexing. That syntax is for accessing fields when using scripts in search queries and aggregations. Instead, use the ctx._source.product syntax:

ctx._source.product = ctx._source.remove(\"product_name\");ctx._source.product = ctx._source.product.toUpperCase()

You may still run into issues, because you may have documents that do not contain the product_name field. Then you will get an error again. A better script would be:

if (ctx._source.product_name != null) { ctx._source.product = ctx._source.remove(\"product_name\");ctx._source.product = ctx._source.product.toUpperCase()}

1 Like

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