Search
Close this search box.

Make a copy of Elasticsearch Index

Table of Contents

1. Introduction

Sometimes you wonder how quickly copy your index. It should be like Ctrl+C & Ctrl+V, right? It is almost like that. Let’s see.

2. Start Elasticsearch and Load data

Using docker start Elasticsearch node, setup password for elastic user and load sample data.

				
					docker run --rm -d \
--name elk \
--net kibana \
-p 9300:9300 \
-p 9200:9200 \
docker.elastic.co/elasticsearch/elasticsearch:8.8.1

dockerContainerID=`docker ps --filter name=elk -q`;
docker exec -it $dockerContainerID sh -c '/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i --batch --url https://localhost:9200';

curl -k -XPOST -u elastic "https://localhost:9200/vehicle-truck/_doc" -H 'Content-Type: application/json' -d'
{
    "brand": "KrAZ"
}'

curl -k -XPOST -u elastic "https://localhost:9200/vehicle-car/_doc" -H 'Content-Type: application/json' -d'
{
    "brand": "toyota"
}'

curl -k -XPOST -u elastic "https://localhost:9200/vehicle-motorcycle/_doc" -H 'Content-Type: application/json' -d'
{
    "brand": "ducati"
}'
				
			

After Loading data display indexes and decide which one you would like to copy. Let’s copy vehicle-motorcycle

				
					$ curl -k -XGET -u elastic "https://localhost:9200/_cat/indices/vehicle-*"
Enter host password for user 'elastic':
yellow open vehicle-car        c924aovJQ965_iTIgpOuoA 1 1 1 0 4.6kb 4.6kb
yellow open vehicle-motorcycle 5J2nTOQpQFSID7WJ4UOW-g 1 1 1 0 4.6kb 4.6kb
yellow open vehicle-truck      1s2eTi5GQv-2CXrE2QSi1Q 1 1 1 0 4.6kb 4.6kb

				
			

3. Copy index

To copy index ‘vehicle-motorcycle’ into new called ‘vehicle-motorcycle-copy’ (name can be different if you want) run as below

				
					curl -k -XPOST -u elastic "https://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
    "source":{
    "index":"vehicle-motorcycle"
    },
    "dest":{
    "index":"vehicle-motorcycle-copy"
    }
}'
				
			

Then check if copy is created and now you can search both indexes

				
					curl -k -XGET -u elastic "https://localhost:9200/_cat/indices/vehicle-*"                           
Enter host password for user 'elastic':
yellow open vehicle-car             c924aovJQ965_iTIgpOuoA 1 1 1 0 4.7kb 4.7kb
yellow open vehicle-motorcycle-copy mEY-blJbQE6OSA2oW_8_ug 1 1 1 0 4.6kb 4.6kb
yellow open vehicle-motorcycle      5J2nTOQpQFSID7WJ4UOW-g 1 1 1 0 4.7kb 4.7kb
yellow open vehicle-truck           1s2eTi5GQv-2CXrE2QSi1Q 1 1 1 0 4.7kb 4.7kb
				
			
				
					
curl -k -XGET -u elastic "https://localhost:9200/vehicle-motorcycle-*/_search"

# response:
{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "vehicle-motorcycle",
        "_id": "Hhvmk4kBoGFbsTC-nn3k",
        "_score": 1,
        "_source": {
          "brand": "ducati"
        }
      },
      {
        "_index": "vehicle-motorcycle-copy",
        "_id": "Hhvmk4kBoGFbsTC-nn3k",
        "_score": 1,
        "_source": {
          "brand": "ducati"
        }
      }
    ]
  }
}
				
			
Follow me on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Share the Post:

Enjoy Free Useful Amazing Content

Related Posts