如何重命名集群中的索引?
how to rename an index in a cluster?
我需要重命名集群中的几个索引(它们的名称必须更改,我不能使用aliases)。
我看到没有支持的方法来做到这一点,我发现最接近的是 rename the directory of the index,我在集群中试过了。
集群有 3 台机器 A
、B
和 C
,分片复制到每台机器上。我在 A
上关闭了 elasticsearch,将 /var/lib/elasticsearch/security/nodes/0/indices/oldindexname
重命名为 /var/lib/elasticsearch/security/nodes/0/indices/newindexname
并重新启动了 A
。
集群的状态是黄色的,elasticsearch 正在施展魔法来恢复正确的状态。一段时间后,我得到了
oldindexname
可用并完全复制(我猜是从 B
和 C
中恢复的)
newindexname
可用(我可以搜索它)但 head 插件显示其碎片处于 "Unassigned" 状态并且它们呈灰色(未复制)
在恢复期间 security.log
显示了以下消息:
[2015-02-20 11:02:33,461][INFO ][gateway.local.state.meta ] [A.example.com] dangled index directory name is [newindexname], state name is [oldindexname], renaming to directory name
虽然newindexname
可以搜索到,但肯定不是正常状态
我通过删除 newindexname
回滚到以前的状态。集群恢复为绿色,没有任何 "Unassigned" 个条目。
鉴于此,如何在集群中将 oldindexname
重命名为 newindexname
?
注意: 我想到的最终解决方案是将 oldindex
滚动复制到 newindex
中,然后删除 oldindex
。这需要时间,所以如果有更直接的解决方案那就太好了。
因此在 ES 中没有直接的方法来复制或重命名索引(我确实广泛搜索了我自己的项目)
然而,一个非常简单的选择是使用流行的迁移工具 [Elastic-Exporter]。
http://www.retailmenot.com/corp/eng/posts/2014/12/02/elasticsearch-cluster-migration/
[PS: 这不是我的博客,只是偶然发现,觉得不错]
因此你可以复制index/type然后删除旧的。
实现重命名或更改索引映射的另一种不同方法是使用 logstash 重建索引。
这是 logstash 2.1 配置的示例:
input {
elasticsearch {
hosts => ["es01.example.com", "es02.example.com"]
index => "old-index-name"
size => 500
scroll => "5m"
}
}
filter {
mutate {
remove_field => [ "@version" ]
}
date {
"match" => [ "custom_timestamp", "MM/dd/YYYY HH:mm:ss" ]
target => "@timestamp"
}
}
output {
elasticsearch {
hosts => ["es01.example.com", "es02.example.com" ]
manage_template => false
index => "new-index-name"
}
}
如Elasticsearch reference for snapshot module、
所示
The rename_pattern and rename_replacement options can be also used to rename index on restore using regular expression
要重命名索引,您可以使用 Elasticsearch 快照模块。
首先你必须拍摄你的 index.while 恢复它的快照你可以
重命名您的索引。
POST /_snapshot/my_backup/snapshot_1/_restore
{
"indices": "jal",
"ignore_unavailable": "true",
"include_global_state": false,
"rename_pattern": "jal",
"rename_replacement": "jal1"
}
rename_replacement :-您要在其中备份数据的新索引名称。
您可以使用 REINDEX 来做到这一点。
Reindex does not attempt to set up the destination index. It does not
copy the settings of the source index. You should set up the
destination index prior to running a _reindex action, including
setting up mappings, shard counts, replicas, etc.
- 首先将索引复制到新名称
POST /_reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
- 现在删除索引
DELETE /twitter
以防万一有人还需要它。重命名索引的成功方法(非官方方法)是:
- 关闭需要重命名的索引
- 重命名主节点和数据节点的所有数据目录中的索引文件夹。
- 重新打开旧的关闭索引(我使用 kofp 插件)。旧索引将重新打开但保持未分配状态。新索引将以关闭状态出现
- 重新打开新索引
- 删除旧索引
如果您碰巧遇到此错误"dangled index directory name is",请删除所有主节点(不是数据节点)中的索引文件夹,然后重新启动其中一个数据节点。
如果您无法重新索引,解决方法 是使用别名。来自 official 文档:
elasticsearch 中的 APIs 在处理特定索引时接受索引名称,并在适用时接受多个索引。索引别名 API 允许使用名称作为索引的别名,所有 API 都会自动将别名转换为实际的索引名称。一个别名也可以映射到多个索引,当指定别名时,别名会自动扩展到别名索引。别名还可以与将在搜索和路由值时自动应用的过滤器相关联。别名不能与索引同名。
请注意,如果您使用“更喜欢此功能”,则此解决方案不起作用。 https://github.com/elastic/elasticsearch/issues/16560
从 ElasticSearch 7.4 开始,重命名索引的最佳方法是使用新引入的 Clone Index API, then to delete the original index using the Delete Index API.
复制索引
克隆索引 API 相对于使用快照 API 或 Reindex API 的主要优势在于速度,因为克隆索引 API 将源索引的段硬链接到目标索引,而不重新处理其任何内容(显然,在支持硬链接的文件系统上;否则,文件将在文件系统级别复制,这仍然比替代方案更有效)。 Clone Index 还保证目标索引在每个点都与源索引相同(也就是说,不需要手动复制设置和映射,与 Reindex 方法相反),并且不需要配置本地快照目录.
旁注:尽管此过程比以前的解决方案快得多,但它仍然意味着停机时间。有真实的用例证明重命名索引是合理的(例如,作为拆分、收缩或备份工作流程中的一个步骤),但重命名索引不应成为日常操作的一部分。如果您的工作流程需要频繁重命名索引,那么您应该考虑使用 Indices Aliases。
这是将索引 source_index
重命名为 target_index
的完整操作序列示例。它可以使用一些特定于 ElasticSearch 的控制台来执行,例如集成的 in Kibana. See this gist 作为此示例的替代版本,使用 curl
而不是 Elastic Search 控制台。
# Make sure the source index is actually open
POST /source_index/_open
# Put the source index in read-only mode
PUT /source_index/_settings
{
"settings": {
"index.blocks.write": "true"
}
}
# Clone the source index to the target name, and set the target to read-write mode
POST /source_index/_clone/target_index
{
"settings": {
"index.blocks.write": null
}
}
# Wait until the target index is green;
# it should usually be fast (assuming your filesystem supports hard links).
GET /_cluster/health/target_index?wait_for_status=green&timeout=30s
# If it appears to be taking too much time for the cluster to get back to green,
# the following requests might help you identify eventual outstanding issues (if any)
GET /_cat/indices/target_index
GET /_cat/recovery/target_index
GET /_cluster/allocation/explain
# Delete the source index
DELETE /source_index
我需要重命名集群中的几个索引(它们的名称必须更改,我不能使用aliases)。
我看到没有支持的方法来做到这一点,我发现最接近的是 rename the directory of the index,我在集群中试过了。
集群有 3 台机器 A
、B
和 C
,分片复制到每台机器上。我在 A
上关闭了 elasticsearch,将 /var/lib/elasticsearch/security/nodes/0/indices/oldindexname
重命名为 /var/lib/elasticsearch/security/nodes/0/indices/newindexname
并重新启动了 A
。
集群的状态是黄色的,elasticsearch 正在施展魔法来恢复正确的状态。一段时间后,我得到了
oldindexname
可用并完全复制(我猜是从B
和C
中恢复的)newindexname
可用(我可以搜索它)但 head 插件显示其碎片处于 "Unassigned" 状态并且它们呈灰色(未复制)
在恢复期间 security.log
显示了以下消息:
[2015-02-20 11:02:33,461][INFO ][gateway.local.state.meta ] [A.example.com] dangled index directory name is [newindexname], state name is [oldindexname], renaming to directory name
虽然newindexname
可以搜索到,但肯定不是正常状态
我通过删除 newindexname
回滚到以前的状态。集群恢复为绿色,没有任何 "Unassigned" 个条目。
鉴于此,如何在集群中将 oldindexname
重命名为 newindexname
?
注意: 我想到的最终解决方案是将 oldindex
滚动复制到 newindex
中,然后删除 oldindex
。这需要时间,所以如果有更直接的解决方案那就太好了。
因此在 ES 中没有直接的方法来复制或重命名索引(我确实广泛搜索了我自己的项目)
然而,一个非常简单的选择是使用流行的迁移工具 [Elastic-Exporter]。
http://www.retailmenot.com/corp/eng/posts/2014/12/02/elasticsearch-cluster-migration/
[PS: 这不是我的博客,只是偶然发现,觉得不错]
因此你可以复制index/type然后删除旧的。
实现重命名或更改索引映射的另一种不同方法是使用 logstash 重建索引。 这是 logstash 2.1 配置的示例:
input {
elasticsearch {
hosts => ["es01.example.com", "es02.example.com"]
index => "old-index-name"
size => 500
scroll => "5m"
}
}
filter {
mutate {
remove_field => [ "@version" ]
}
date {
"match" => [ "custom_timestamp", "MM/dd/YYYY HH:mm:ss" ]
target => "@timestamp"
}
}
output {
elasticsearch {
hosts => ["es01.example.com", "es02.example.com" ]
manage_template => false
index => "new-index-name"
}
}
如Elasticsearch reference for snapshot module、
所示The rename_pattern and rename_replacement options can be also used to rename index on restore using regular expression
要重命名索引,您可以使用 Elasticsearch 快照模块。
首先你必须拍摄你的 index.while 恢复它的快照你可以 重命名您的索引。
POST /_snapshot/my_backup/snapshot_1/_restore
{
"indices": "jal",
"ignore_unavailable": "true",
"include_global_state": false,
"rename_pattern": "jal",
"rename_replacement": "jal1"
}
rename_replacement :-您要在其中备份数据的新索引名称。
您可以使用 REINDEX 来做到这一点。
Reindex does not attempt to set up the destination index. It does not copy the settings of the source index. You should set up the destination index prior to running a _reindex action, including setting up mappings, shard counts, replicas, etc.
- 首先将索引复制到新名称
POST /_reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
- 现在删除索引
DELETE /twitter
以防万一有人还需要它。重命名索引的成功方法(非官方方法)是:
- 关闭需要重命名的索引
- 重命名主节点和数据节点的所有数据目录中的索引文件夹。
- 重新打开旧的关闭索引(我使用 kofp 插件)。旧索引将重新打开但保持未分配状态。新索引将以关闭状态出现
- 重新打开新索引
- 删除旧索引
如果您碰巧遇到此错误"dangled index directory name is",请删除所有主节点(不是数据节点)中的索引文件夹,然后重新启动其中一个数据节点。
如果您无法重新索引,解决方法 是使用别名。来自 official 文档:
elasticsearch 中的APIs 在处理特定索引时接受索引名称,并在适用时接受多个索引。索引别名 API 允许使用名称作为索引的别名,所有 API 都会自动将别名转换为实际的索引名称。一个别名也可以映射到多个索引,当指定别名时,别名会自动扩展到别名索引。别名还可以与将在搜索和路由值时自动应用的过滤器相关联。别名不能与索引同名。
请注意,如果您使用“更喜欢此功能”,则此解决方案不起作用。 https://github.com/elastic/elasticsearch/issues/16560
从 ElasticSearch 7.4 开始,重命名索引的最佳方法是使用新引入的 Clone Index API, then to delete the original index using the Delete Index API.
复制索引克隆索引 API 相对于使用快照 API 或 Reindex API 的主要优势在于速度,因为克隆索引 API 将源索引的段硬链接到目标索引,而不重新处理其任何内容(显然,在支持硬链接的文件系统上;否则,文件将在文件系统级别复制,这仍然比替代方案更有效)。 Clone Index 还保证目标索引在每个点都与源索引相同(也就是说,不需要手动复制设置和映射,与 Reindex 方法相反),并且不需要配置本地快照目录.
旁注:尽管此过程比以前的解决方案快得多,但它仍然意味着停机时间。有真实的用例证明重命名索引是合理的(例如,作为拆分、收缩或备份工作流程中的一个步骤),但重命名索引不应成为日常操作的一部分。如果您的工作流程需要频繁重命名索引,那么您应该考虑使用 Indices Aliases。
这是将索引 source_index
重命名为 target_index
的完整操作序列示例。它可以使用一些特定于 ElasticSearch 的控制台来执行,例如集成的 in Kibana. See this gist 作为此示例的替代版本,使用 curl
而不是 Elastic Search 控制台。
# Make sure the source index is actually open
POST /source_index/_open
# Put the source index in read-only mode
PUT /source_index/_settings
{
"settings": {
"index.blocks.write": "true"
}
}
# Clone the source index to the target name, and set the target to read-write mode
POST /source_index/_clone/target_index
{
"settings": {
"index.blocks.write": null
}
}
# Wait until the target index is green;
# it should usually be fast (assuming your filesystem supports hard links).
GET /_cluster/health/target_index?wait_for_status=green&timeout=30s
# If it appears to be taking too much time for the cluster to get back to green,
# the following requests might help you identify eventual outstanding issues (if any)
GET /_cat/indices/target_index
GET /_cat/recovery/target_index
GET /_cluster/allocation/explain
# Delete the source index
DELETE /source_index