# Update by query to rename fields

**URL:** https://discuss.elastic.co/t/update-by-query-to-rename-fields/120552
**Category:** Elasticsearch
**Created:** [February 20, 2018, 3:43am UTC](https://discuss.elastic.co/t/update-by-query-to-rename-fields/120552 "2018-02-20T03:43:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![seanziee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seanziee/32/45151_2.png) [@seanziee](https://discuss.elastic.co/u/seanziee)
#### Post date: [February 20, 2018, 3:43am UTC](https://discuss.elastic.co/t/update-by-query-to-rename-fields/120552/1 "2018-02-20T03:43:25Z")

</div>

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!

```auto
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

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

```

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [February 20, 2018, 4:14am UTC](https://discuss.elastic.co/t/update-by-query-to-rename-fields/120552/2 "2018-02-20T04:14:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![seanziee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seanziee/32/45151_2.png) [@seanziee](https://discuss.elastic.co/u/seanziee)
#### Post date: [February 20, 2018, 4:38am UTC](https://discuss.elastic.co/t/update-by-query-to-rename-fields/120552/3 "2018-02-20T04:38:09Z")

</div>

Ok thanks for the heads up!

---

<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: [March 20, 2018, 4:38am UTC](https://discuss.elastic.co/t/update-by-query-to-rename-fields/120552/4 "2018-03-20T04:38:11Z")

</div>

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