Update by query to rename fields

Hi, I've looked all over for how to rename a field and everyone mentions doing a reindex. But isn't a script just as if not more effective (if you don't want to reindex). I just want to make sure I'm not missing something vital here because this seems like a perfectly good solution!

POST /indexName/_update_by_query
{
  "script": { 
    "source":"ctx._source.newField =ctx._source.oldField"
  },
    "query": { "match_all":{} }
}

POST /indexName/_update_by_query
{
  "script": {
    "inline": "ctx._source.remove(\"oldField\")"
  },
    "query": { "match_all":{} }
}

Or even

POST /indexName/_update_by_query
{
  "script": { 
    "source":"ctx._source.newField =ctx._source.remove(\"oldField\")"
  },
    "query": { "match_all":{} }
}
2 Likes

Yes, that will work, however, the field definition will not be removed from your mapping, i.e. you will continue to see oldField in your mapping.

Whereas with reindexing you can keep your mapping clean, i.e. in the new index, oldField will not be present anymore.

Pragmatism is in order here. If you just need to change one field name and/or your index seems to big to reindex, definitely go with update by query. However, if renaming a field is a frequent operation that you're doing, you might want to reindex in order to keep your mapping clean.

2 Likes

Ok thanks for the heads up!

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