I need to clone the content of a document in my elasticsearch index (in the same index) by using the kibana console. I need exactly the same fields in the _source of the document (of course, the copy will have another id). I tryed to:
- GET the document
- Create a new empty instance of document
- Update the new document by manually copying the properties of the result on (1):
POST /blog/post/VAv2FWoBKgnBpki61WiD/_update { "doc" : { "content" : "..." ...
But the problem is the field contain veeeery long properties. And sometimes I got an error since the strings seem not to be scaped when I manually copy them from the Kibana interface.
I searched in the documentation but I can not find a query to duplicate a document, and it is a quite common think to do I think...
Any clue?
1 Answer
Make use of Reindex API. Here is what you can do.
Summary of steps:
- Create a
destination_index(dummy). Make sure the mapping is exact to that ofsource_index - Using Reindex API, reindex that particular document from
source_indextodesitnation_index. During this operation, update the _id (I've mentioned the script) - Reindex this document back from
desitnation_indextosource_index
Reindex Query
Step 1: Copy document from source_index to destination_index. (With the script)
POST _reindex { "source": { "index": "source_index", "query": { "match": { "_id": "1" } } }, "dest": { "index": "destination_index" }, "script": { "inline": "ctx._id=2", "lang": "painless" } } Note how I've added a script in the above query that would change the _id (_id is set as 2) of the document. Your destination_index would have all the fields with exact same values as that of source except for the _id field.
Step 2: Copy that document from destination_index to source_index
POST _reindex { "source": { "index": "destination_index", "query": { "match": { "_id": "2" } } }, "dest": { "index": "source_index" } } Now search the source_index, it would have two documents with different _ids (_id=1 and _id=2) having exact same content.
Hope this helps!
1