How to reindex , with multiple _id in index


(Abhishekh) #1

Hi,
I have Index with more than one document and wanted to reindex but while reindexing wanted to change the _id field of each document in destination index. Followed this but it is for one index. How to do that?


(David Pilato) #2

I'm unsure but may be you can modify ctx._source._index?


(Abhishekh) #3

Thanks for your suggestion but wanted to modify the _id not _index. For one document we can change the _id while reindexing and my case is for multiple docs


(David Pilato) #4

So I don't understand. Why ctx._source._id does not work in your case?


(Abhishekh) #5

I am using command to reindex:

curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
	"source": {
    "index": "megacorp"
  },
  "dest": {
    "index": "tempindex"
  },

    "script" : {
    "inline": "ctx._id= 12345"
  }
}
'

After successful execution it copies the megacorp/type/1( where 1 is _id ) document to tempindex/type/1234. As I could not found to give the source _id field in command so by default it is taking first document in this case _id =1 and copies the document in the destination _id =1234. As source index megacorp contains many documents but it copies only one to tempindex. Is there any way to give the source _id while reindexing?


(David Pilato) #6

Is there any way to give the source _id while reindexing?

Which value you want to set the id to? Is that from a field which is available within your document?


(Abhishekh) #7

"_index" : "megacorp1",
"_type" : "employee",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0

I want to give all _id value from existing document as a source and a destination _id's for new index. As given above _id is 2 so in new index i want to copy this document to other index suppose 12 like this while reindexing wanted to copy document from source index to destination index at different _id


(David Pilato) #8

So if I understand correctly you have a document which coordinate is:

source/doc/1

And you want to reindex it as

destination/doc/12

Do I understand correctly the requirement?
If so, tell me how you compute 12 from the data you have in document 1. Where this 12 number is coming from?


(Abhishekh) #9

Yes U have understood the requirement correctly David. As per my project requirement I need to copy the document to different id in destination index


(David Pilato) #10

Could you answer that ^^^ then?


(Abhishekh) #11
curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
	"source": {
    "index": "megacorp"
  },
  "dest": {
    "index": "tempindex"
  },

    "script" : {
    "inline": "ctx._id= 12345"
  }
}
'

As in this it is copying _id 1 data to 12345 at destination similarly as per requirement I have to insert at any _id in destination.


(David Pilato) #12

What is "any _id"? Again, how do you compute this 12345? Is it the current _id of the document multiplied by 12345? What is the rule to get that number?


(Abhishekh) #13

Yes it is multiplied by 12345.


(David Pilato) #14

Then can you write: ctx._id = ctx._id * 12345?


(Abhishekh) #16

Thanks David for your suggestion, My problem got resolved.


(system) #17

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