How to create a duplicate index in ElasticSearch from existing index?

Viewed 2466

I have an existing index with mappings and data in ElasticSearch which I need to duplicate for testing new development. Is there anyway to create a temporary/duplicate index from the already existing one?

Coming from an SQL background, I am looking at something equivalent to

SELECT * 
INTO TestIndex
FROM OriginalIndex
WHERE 1 = 0

I have tried the Clone API but can't get it to work.

I'm trying to clone using:

POST /originalindex/_clone/testindex
{
}

But this results in the following exception:

{
  "error": {
    "root_cause": [
      {
        "type": "invalid_type_name_exception",
        "reason": "Document mapping type name can't start with '_', found: [_clone]"
      }
    ],
    "type": "invalid_type_name_exception",
    "reason": "Document mapping type name can't start with '_', found: [_clone]"
  },
  "status": 400
}

I know someone would guide me quickly. Thanks in advance all you wonderful folks.

2 Answers

First you have to set the source index to be read-only

PUT /originalindex/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

Then you can clone

POST /originalindex/_clone/testindex

If you need to copy documents to a new index, you can use the reindex api

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: 
application/json' -d'
{
  "source": {
    "index": "someindex"
  },
  "dest": {
    "index": "someindex_copy"
  }
}
'

(See: https://wrossmann.medium.com/clone-an-elasticsearch-index-b3e9b295d3e9)

Shortly after posting the question, I figured out a way.

First, get the properties of original index:

GET originalindex

Copy the properties and put to a new index:

PUT /testindex
{
    "aliases": {...from the above GET request},
    "mappings": {...from the above GET request},
    "settings": {...from the above GET request}
}

Now I have a new index for testing.

Related