# Script not works correct in reindex operation when want to copy particular data from source

**URL:** <https://discuss.elastic.co/t/script-not-works-correct-in-reindex-operation-when-want-to-copy-particular-data-from-source/168296>\
**Category:** Elasticsearch\
**Created:** [February 13, 2019, 9:27pm UTC](https://discuss.elastic.co/t/script-not-works-correct-in-reindex-operation-when-want-to-copy-particular-data-from-source/168296 "2019-02-13T21:27:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [February 13, 2019, 9:27pm UTC](https://discuss.elastic.co/t/script-not-works-correct-in-reindex-operation-when-want-to-copy-particular-data-from-source/168296/1 "2019-02-13T21:27:36Z")

</div>

Here is the script and try...  
Created one 'demo' index  
PUT demo  
{  
"mappings": {  
"doc":{  
"properties": {  
"field1":{  
"type": "long"  
},  
"field2":{  
"type": "text"  
}  
}  
}  
}  
}  
now inserted two documents  
PUT demo/doc/12\_1  
{  
"field1":12,  
"field2":"text1"  
}  
PUT demo/doc/13\_1  
{  
"field1":13,  
"field2":"text2"  
}

now created one more index demo with same mapping as above  
PUT demo1  
{  
"mappings": {  
"doc":{  
"properties": {  
"field1":{  
"type": "long"  
},  
"field2":{  
"type": "text"  
}  
}  
}  
}  
}

now i want to copy only 'field2' data into the 'demo1' but my '\_id' for new index will be as per 'field1' of the 'demo' and for that i run following script.  
POST \_reindex  
{  
"source": {  
"index": "demo",  
"\_source":"field2"  
},  
"dest": {  
"index": "demo1"  
},  
"script": {  
"source": "ctx.\_id = ctx.\_source['field1']"  
}  
}

but output is  
"hits": {  
"total": 2,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "demo1",  
"\_type": "doc",  
"\_id": "G9G\_6GgB47Hb5C908ES1",  
"\_score": 1,  
"\_source": {  
"field2": "text2"  
}  
},  
{  
"\_index": "demo1",  
"\_type": "doc",  
"\_id": "GtG\_6GgB47Hb5C908ES1",  
"\_score": 1,  
"\_source": {  
"field2": "text1"  
}  
}  
]  
}  
here '\_id' is randomly generated.  
if i remove "\_source":"field2", then '\_id' is properly generated.

Please suggest something to achieve right behaviour.

Bye and thanks.

---

<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 13, 2019, 9:27pm UTC](https://discuss.elastic.co/t/script-not-works-correct-in-reindex-operation-when-want-to-copy-particular-data-from-source/168296/2 "2019-03-13T21:27:38Z")

</div>

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