# Painless: Generating a scripted field thats an exact copy of another field

**URL:** https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135
**Category:** Kibana
**Created:** [January 18, 2018, 9:14pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135 "2018-01-18T21:14:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 18, 2018, 9:14pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135/1 "2018-01-18T21:14:21Z")

</div>

Hi

I'm trying to generate a scripted field that returns only certain tokens of a derived field. For example, if I have a field with possible combination of tokens "Movies, pg13, Drama, comedy, etc" I want to generate a scripted field that only returns the genre tokens (drama, comedy, etc), not the other tokens (pg13, movies, etc). My code to do so is the following:

String r = "";  
if (doc['Industry.keyword'].value =~ /drama/) {  
r = r + "drama ";  
}  
if (doc['Industry.keyword'].value =~ /comedy/) {  
r = r + "comedy ";  
}  
return r

This resulted in a "Courier Fetch: 1 of 5 shards failed."

So, I tried simply just writing a script for a field that would return an exact copy of another field. My script is the following:

return doc['Industry.keyword'].value

But it only returns the first token delimited by a comma. So, if a record has the Industry value "Drama, Thriller, Horror," it only returns "Drama." I want it to return the whole string. Any help?

---

<div class="post-metadata">

### Author: ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)
#### Post date: [January 18, 2018, 10:04pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135/2 "2018-01-18T22:04:32Z")

</div>

Could you share your mapping for that field as well as a couple of examples of the values inside the field?

---

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 18, 2018, 10:28pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135/3 "2018-01-18T22:28:04Z")

</div>

Sure thing!

Mapping for the industry field:

"Industry" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
}

example values inside the field:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/d/ad67a73ec71269b9ce255eb671eac7c81de7e2ac.png)

---

<div class="post-metadata">

### Author: ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)
#### Post date: [January 18, 2018, 10:39pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135/4 "2018-01-18T22:39:44Z")

</div>

Oh, have you tried using doc['industry'].value instead? That should give you what you want.

---

<div class="post-metadata">

### Author: ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)
#### Post date: [January 18, 2018, 10:45pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135/5 "2018-01-18T22:45:36Z")

</div>

Sorry, I meant to put doc['Industry.keyword'].value in my op. Edited op with correction.

Just doc['Industry'].value still gives "Courier Fetch: 1 of 5 shards failed."

---

<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: [February 15, 2018, 10:50pm UTC](https://discuss.elastic.co/t/painless-generating-a-scripted-field-thats-an-exact-copy-of-another-field/116135/6 "2018-02-15T22:50:53Z")

</div>

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