How to view the contents of a GZiped file in HDFS?
February 16, 2022How to copy selective documents from one index to a new index in Elasticsearch?
March 2, 2022In this post we will describe how to copy an index and its contents to a new index in Elasticsearch.
We currently have an index named account. We are going to copy the account index and its content to another index named account_v2 using the reindex API.
reindex
We currently have only one index in Elasticsearch.
[osboxes@wk1 ~]$ curl http://localhost:9200/_cat/indices?v health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open account avtO6o3jTmWtgAyQwTTM6Q 1 1 993 7 400.9kb 400.9kb
_reindex takes the source index and copies the contents to the destination index. In our case destination index account_v2 is not present. If the destination index is not present, Elasticsearch will create and copy the documents to the new index.
If you prefer to provide your own definition to the destination index, you can do so by creating a new index yourself before running the _reindex.
curl -X POST localhost:9200/_reindex -H 'Content-Type: application/json' -d' { "source": { "index": "account" }, "dest": { "index": "account_v2" } }' { "took": 1648, "timed_out": false, "total": 993, "updated": 0, "created": 993, "deleted": 0, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "requests_per_second": -1.0, "throttled_until_millis": 0, "failures": [] }
Now that we have reindexed, let’s list the indices in Elasticsearch and we can see the newly created index under account_v2. You can see the number of documents matches between the old and the new index.
[osboxes@wk1 ~]$ curl http://localhost:9200/_cat/indices?v health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open account_v2 Kws5DlAdQuCv8845NZPUhQ 1 1 993 0 393.3kb 393.3kb yellow open account avtO6o3jTmWtgAyQwTTM6Q 1 1 993 7 400.9kb 400.9kb
Fetch a document from the newly created index.
[osboxes@wk1 ~]$ curl -X GET localhost:9200/account_v2/_doc/954?pretty { "_index" : "account_v2", "_type" : "_doc", "_id" : "954", "_version" : 1, "_seq_no" : 785, "_primary_term" : 1, "found" : true, "_source" : { "account_number" : 954, "balance" : 49404, "firstname" : "Jenna", "lastname" : "Martin", "age" : 22, "gender" : "M", "address" : "688 Hart Street", "employer" : "Zinca", "email" : "jennamartin@zinca.com", "city" : "Oasis", "state" : "MD" } }
1 Comment
[…] In this post we have shown how to copy an index and all its contents to a new index in Elasticsearch… […]