ElasticSearch - 如何制作现有索引的一对一副本
ElasticSearch - How to make a 1-to-1 copy of an existing index
我正在使用 Elasticsearch 2.3.3 并尝试制作现有索引的精确副本。 (使用与 Elasticsearch 安装捆绑在一起的 reindex 插件)
问题是数据被复制了,但是映射和分析器等设置被遗漏了。
制作现有索引(包括其所有设置)的精确副本的最佳方法是什么?
我的主要目标是创建一个副本,更改副本,并且只有在一切顺利的情况下才将别名切换到副本。 (零停机时间备份和恢复)
在我看来,实现这一目标的最佳方式是利用 index templates。索引模板允许您存储索引的规范,包括设置(因此分析器)和映射。然后,每当您创建与模板匹配的新索引时,ES 都会使用模板中存在的设置和映射为您创建索引。
因此,首先使用模板模式 myindex-*
:
创建一个名为 index_template
的索引模板
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-2
与 myindex-*
匹配模式。
就这么简单。
下面的似乎正是我想要的:
使用 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"
}
据我目前所知,它完全满足了我的需求。
我的原始索引的一对一副本。
我还怀疑此操作的性能比我的重新索引更好。
我在使用 reindex API 时遇到了同样的问题。
基本上我正在合并每日、每周、每月的索引以减少碎片。
我们有很多具有不同数据输入的索引,为所有情况维护一个模板不是一种选择。因此我们使用动态映射。
由于动态映射,如果您的数据很复杂,重新索引过程 可能 会产生冲突,比如 json 存储在字符串字段中,重新索引的字段可能会结束作为其他东西。
解决方案:
- 复制源索引的映射
- 创建新索引,应用映射
- 禁用动态映射
- 开始重建索引过程。
可以创建脚本,当然应该进行错误检查。
下面是缩写脚本。
使用原始索引的映射创建一个新的空索引。:
#!/bin/bash
SRC=
DST=
# 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:?}
# Disable dynamic mapping
curl -s -H 'Content-Type: application/json' -XPUT \
${URL:?}/${DST}/_mapping -d '{ "dynamic": false }'
开始重建索引
curl -s -XPOST "${URL:?}" -H 'Content-Type: application/json' -d'
{
"conflicts": "proceed",
"source": {
"index": "'${SRC}'"
},
"dest": {
"index": "'${DST}'",
"op_type": "create"
}
}'
我正在使用 Elasticsearch 2.3.3 并尝试制作现有索引的精确副本。 (使用与 Elasticsearch 安装捆绑在一起的 reindex 插件)
问题是数据被复制了,但是映射和分析器等设置被遗漏了。
制作现有索引(包括其所有设置)的精确副本的最佳方法是什么?
我的主要目标是创建一个副本,更改副本,并且只有在一切顺利的情况下才将别名切换到副本。 (零停机时间备份和恢复)
在我看来,实现这一目标的最佳方式是利用 index templates。索引模板允许您存储索引的规范,包括设置(因此分析器)和映射。然后,每当您创建与模板匹配的新索引时,ES 都会使用模板中存在的设置和映射为您创建索引。
因此,首先使用模板模式 myindex-*
:
index_template
的索引模板
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-2
与 myindex-*
匹配模式。
就这么简单。
下面的似乎正是我想要的:
使用 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"
}
据我目前所知,它完全满足了我的需求。 我的原始索引的一对一副本。
我还怀疑此操作的性能比我的重新索引更好。
我在使用 reindex API 时遇到了同样的问题。 基本上我正在合并每日、每周、每月的索引以减少碎片。
我们有很多具有不同数据输入的索引,为所有情况维护一个模板不是一种选择。因此我们使用动态映射。
由于动态映射,如果您的数据很复杂,重新索引过程 可能 会产生冲突,比如 json 存储在字符串字段中,重新索引的字段可能会结束作为其他东西。
解决方案:
- 复制源索引的映射
- 创建新索引,应用映射
- 禁用动态映射
- 开始重建索引过程。
可以创建脚本,当然应该进行错误检查。 下面是缩写脚本。
使用原始索引的映射创建一个新的空索引。:
#!/bin/bash
SRC=
DST=
# 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:?}
# Disable dynamic mapping
curl -s -H 'Content-Type: application/json' -XPUT \
${URL:?}/${DST}/_mapping -d '{ "dynamic": false }'
开始重建索引
curl -s -XPOST "${URL:?}" -H 'Content-Type: application/json' -d'
{
"conflicts": "proceed",
"source": {
"index": "'${SRC}'"
},
"dest": {
"index": "'${DST}'",
"op_type": "create"
}
}'