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


(Kshitij Yelpale) #1

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.