如何从现有索引在 ElasticSearch 中创建重复索引?

时间:2020-12-28 04:05:01

标签: elasticsearch clone

我在 ElasticSearch 中有一个包含映射和数据的现有索引,我需要复制该索引以测试新开发。无论如何要从已经存在的索引创建临时/重复索引?

来自 SQL 背景,我正在寻找与

相当的东西
SELECT * 
INTO TestIndex
FROM OriginalIndex
WHERE 1 = 0

我已经尝试过 Clone API,但无法让它工作。

我正在尝试使用以下方法进行克隆:

POST /originalindex/_clone/testindex
{
}

但这会导致以下异常:

{
  "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
}

我知道有人会快速指导我。在此先感谢各位了不起的人。

2 个答案:

答案 0 :(得分:1)

首先你必须将源索引设置为只读

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

然后你可以clone

POST /originalindex/_clone/testindex

如果需要将文档复制到新索引,可以使用 reindex api

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

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

答案 1 :(得分:1)

在发布问题后不久,我想出了一个方法。

首先获取原始索引的属性:

GET originalindex

复制属性并放入新索引:

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

现在我有了一个新的测试索引。