# Copy field value to a new field in existing index

**URL:** <https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595>\
**Category:** Elasticsearch\
**Created:** [March 23, 2020, 5:58am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595 "2020-03-23T05:58:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 23, 2020, 5:58am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/1 "2020-03-23T05:58:21Z")

</div>

Hey all,  
I had a field name with a typo. Let's say it was called Helo.keyword.  
I have created a new field with the correct name Hello.keyword.  
New data is populating the new field.  
How can I copy the old values to the new field created?  
This is a one-time operation. After the values are copied I will delete the old field from the index.  
I tried the following script, but it does not update any document

```auto
POST /test/text/_update_by_query
{
"query": {
  "match_all": {}
},
"script": "ctx._source.Hello.keyword = ctx._source.Helo.keyword"
}

```

Result

```auto
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 4,
  "timed_out" : false,
  "total" : 0,
  "updated" : 0,
  "deleted" : 0,
  "batches" : 0,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : []
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 23, 2020, 8:16am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/2 "2020-03-23T08:16:45Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Without that it's hard to know but let's make some guesses. May be the type is actually `_doc` and not `test`.  
You can look also at the rename processor and the reindex API. So you reindex in a new clean index instead of keeping the old mapping around.

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 24, 2020, 12:43am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/3 "2020-03-24T00:43:26Z")

</div>

> [@dadoonet](#):
>
> Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.
> 
> A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.
> 
> Without that it's hard to know but let's make some guesses. May be the type is actually `_doc` and not `test` .  
> You can look also at the rename processor and the reindex API. So you reindex in a new clean index instead of keeping the old mapping around.

Hi @dadoonet  
Thank you for the reply.  
I do not have a full recreation scripts as I was not involved in the entire process.  
Let me know what info I should provide, and I'll do it with pleasure.

The script, as I understand it:  
POST / **test** /text/\_update\_by\_query - test is the index name  
POST /test/ **text** /\_update\_by\_query - text is the field type  
POST /test/text/ **\_update\_by\_query** - update by query is the method

If my understanding is wrong, please let me know.  
I took the script from [here](https://stackoverflow.com/a/38006012) - [elasticsearch - Add a field copying data from another fields - Stack Overflow](https://stackoverflow.com/a/38006012)

Reading the [documentation here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html#picking-up-a-new-property), makes me think I have a syntax problem?!

```auto
POST twitter/_update_by_query
{
  "script": {
    "source": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

```

I do not want to use the re-index API, as there are multiple indices with multiple fields I need to update and I am afraid it will cause issues/a mess with the data. I prefer the approach of updating and then deleting 1 field at a time.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 24, 2020, 2:34am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/4 "2020-03-24T02:34:37Z")

</div>

Could you create a sample script with some fake data so we can start from there to help you fixing the problem?

If you look at the link I provided, a sample script is something we can paste into Kibana dev console and play.  
look at the sample script provided in the link.

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 24, 2020, 3:23am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/5 "2020-03-24T03:23:30Z")

</div>

Hi @dadoonet  
This are the steps, more or less  
Create an index

```auto
PUT test-24-03-2020

```

Add mappings. Need also to add the keyword type, not sure how to do that.

```auto
PUT /test-24-03-2020/_mapping
{
        "properties" : {
            "helo" : { "type" : "text" }
        }
}

```

Populate values

```auto
PUT test-24-03-2020/_doc/1
{
  "helo": "test123"
}

```

Add mappings. Need also to add the keyword type, not sure how to do that.

```auto
PUT /test-24-03-2020/_mapping
{
        "properties" : {
            "hello" : { "type" : "text" }
        }
}

```

Now I want to copy all the values from helo to hello

Hope this explains a bit more what is required

Thanks

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 24, 2020, 11:23pm UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/6 "2020-03-24T23:23:35Z")

</div>

OK.  
I managed to copy the values from one field to another.  
I had a typo(?) error or logic(?) issue.

The solution is

```auto
POST index-patter-name-*/_update_by_query
{
  "script": {
    "source": "ctx._source.destination_field= ctx._source.origin_field",
    "lang": "painless"
  },
  "query": {
          "term": {
                  "somefield.keyword": "somevalue"
          }
  }
}

```

- Construct the query according to your specific logic.

Originally I tried to use the keyword and the process failed.

```auto
    "source": "ctx._source.destination_field.keyword= ctx._source.origin_field.keyword",

```

Cheers!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 25, 2020, 5:50am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/7 "2020-03-25T05:50:43Z")

</div>

Great. It's because the `keyword` field does not exist in the `_source` field but is generated at index time.

---

<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:** [April 22, 2020, 5:50am UTC](https://discuss.elastic.co/t/copy-field-value-to-a-new-field-in-existing-index/224595/8 "2020-04-22T05:50:50Z")

</div>

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