我需要重命名集群中的几个索引(它们的名称必须更改,我不能使用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插件显示它的分片处于“未分配”状态,并且它们是灰色的(未复制)在恢复过程中,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
回滚到以前的状态。集群恢复为绿色,没有任何“未分配”的条目。
鉴于此,如何在集群中将oldindexname
重命名为newindexname
?
注意:我考虑的最终解决方案是滚动复制oldindex
到newindex
中,然后删除oldindex
。这将需要时间,所以如果有更直接的解决方案,那就太好了。
发布于 2019-12-19 02:00:25
从ElasticSearch 7.4开始,重命名索引的最佳方法是使用新引入的Clone Index API复制索引,然后使用Delete Index API删除原始索引。
与出于相同目的使用Snapshot API或Reindex API相比,Clone index API的主要优势是速度,因为Clone Index API将段从源索引硬链接到目标索引,而无需重新处理其任何内容(显然,在支持硬链接的文件系统上;否则,在文件系统级别复制文件,这仍然比其他方法更有效)。克隆索引还保证目标索引在每个点都与源索引相同(也就是说,不需要手动复制设置和映射,这与重新编制索引的方法相反),并且不需要配置本地快照目录。
旁注:尽管此过程比以前的解决方案快得多,但它仍然意味着停机时间。有一些实际的用例可以证明重命名索引是合理的(例如,作为拆分、收缩或备份工作流中的一个步骤),但是重命名索引不应该是日常操作的一部分。如果您的工作流需要频繁地重命名索引,那么您应该考虑改用。
以下是将索引source_index
重命名为target_index
的完整操作序列的示例。它可以使用一些特定于ElasticSearch的控制台来执行,比如集成的in Kibana。有关此示例的替代版本,请参阅this gist,使用curl
而不是弹性搜索控制台。
# 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
发布于 2016-06-28 21:49:06
您可以使用REINDEX来做到这一点。
重新索引不会尝试设置目标索引。它不复制源索引的设置。您应该在运行_reindex操作之前执行set up the destination index,包括设置映射、分片计数、副本等。
POST /_reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
DELETE /twitter
发布于 2016-02-24 13:00:13
要重命名索引,您可以使用Elasticsearch Snapshot模块。
首先,您必须为您的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:-要在其中备份数据的新索引名称。
https://stackoverflow.com/questions/28626803
复制相似问题