ElasticSearch - 如何制作现有索引的{1-to-1}副本

时间:2016-09-01 14:24:15

标签: elasticsearch elasticsearch-plugin

我正在使用Elasticsearch 2.3.3并尝试制作现有索引的精确副本。 (使用与Elasticsearch安装捆绑在一起的reindex插件)

问题是数据被复制但是映射和分析器等设置被遗漏了。

制作现有索引的精确副本的最佳方法是什么,包括其所有设置?

我的主要目标是创建一个副本,更改副本,并且只有在一切顺利的情况下才能将副别切换到副本。 (零宕机时间备份和恢复)

3 个答案:

答案 0 :(得分:2)

在我看来,实现这一目标的最佳方法是利用index templates。索引模板允许您存储索引的规范,包括设置(因此分析器)和映射。然后,无论何时创建与模板匹配的新索引,ES都将使用模板中的设置和映射为您创建索引。

因此,首先使用模板模式index_template创建一个名为myindex-*的索引模板:

PUT /_template/index_template
{
  "template": "myindex-*",
  "settings": {
    ... your settings ...
  },
  "mappings": {
    "type1": {
      "properties": {
         ... your mapping ...
      }
    }
  }
}

接下来会发生的是,只要您想在名称与myindex-*匹配的任何索引中索引新文档,ES就会使用此模板(+设置和映射)来创建新索引。

所以说你当前的索引叫做myindex-1,你想把它重新索引到一个名为myindex-2的新索引中。你会发送像这样的重新索引查询

POST /_reindex
{
  "source": {
    "index": "myindex-1"
  },
  "dest": {
    "index": "myindex-2"
  }
}

myindex-2尚不存在,但会在此过程中使用index_template的设置和映射创建,因为名称myindex-2myindex-*模式匹配。

这很简单。

答案 1 :(得分:0)

以下似乎完全符合我的要求:

使用Snapshot And Restore我能够恢复到不同的索引:

POST /_snapshot/index_backup/snapshot_1/_restore { "indices": "original_index", "ignore_unavailable": true, "include_global_state": false, "rename_pattern": "original_index", "rename_replacement": "replica_index" }

据我目前所知,它完全符合我的需要。 我原始索引的一对一副本。

我还怀疑这个操作比我的目的重新索引具有更好的性能。

答案 2 :(得分:0)

使用reindex API时,我也面临着同样的问题。 基本上,我每天,每周,每月都合并索引以减少碎片。

我们有很多具有不同数据输入的索引,并且不能为所有情况维护一个模板。因此,我们使用动态映射。

由于动态映射,如果您的数据很复杂,则重新编制索引过程 可能会产生冲突,例如将json存储在字符串字段中,并且重新编制索引的字段可能会以其他形式结束。

解决方案:

  1. 复制源索引的映射
  2. 创建新索引,应用映射
  3. 开始重新索引过程。

可以创建脚本,并且当然应该进行错误检查。 下面是缩写脚本。

使用原始索引的映射创建一个新的空索引。

#!/bin/bash
SRC=$1
DST=$2

# Create a temporary file for holding the SRC mapping
TMPF=$(mktemp)

# Extract the SRC mapping, use `jq` to get the first record
# write to TMPF
curl -f -s "${URL:?}/${SRC}/_mapping | jq -M -c 'first(.[])' > ${TMPF:?}

# Create the new index
curl -s -H 'Content-Type: application/json' -XPUT ${URL:?}/${DST} -d @${TMPF:?}

开始重新索引

curl -s -XPOST "${URL:?}" -H 'Content-Type: application/json' -d'
{
    "conflicts": "proceed",
    "source": {
        "index": "'${SRC}'"
    },
    "dest": {
        "index": "'${DST}'",
        "op_type": "create"
    }
}'