elasticsearch 如何重命名集群中索引?

w41d8nur  于 2023-02-07  发布在  ElasticSearch
关注(0)|答案(9)|浏览(692)

我需要重命名集群中的几个索引(它们的名称必须更改,我不能使用aliases)。
我发现没有支持的方法来完成这一任务,我找到的最接近的方法是rename the directory of the index,我在集群中尝试了这一方法。
集群有3台机器ABC,每个机器上都复制了碎片。我关闭了A上的elasticsearch,将/var/lib/elasticsearch/security/nodes/0/indices/oldindexname重命名为/var/lib/elasticsearch/security/nodes/0/indices/newindexname,然后重新启动A
集群的状态是黄色的,而elasticsearch正在做一些神奇的事情来恢复正确的状态。

  • oldindexname可用且完全复制(我猜是从BC恢复的)
  • 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。这需要时间,所以如果有更直接的解决方案,那就太好了。

11dmarpk

11dmarpk1#

您可以使用REINDEX来完成此操作。
Reindex不会尝试设置目标索引。它不会复制源索引的设置。在运行a _reindex操作之前,应执行set up the destination index,包括设置Map、碎片计数、副本等。
1.首先将索引复制为新名称

POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

1.现在删除索引

DELETE /twitter
xeufq47z

xeufq47z2#

从ElasticSearch 7.4开始,重命名索引的最佳方法是使用新引入的Clone Index API复制索引,然后使用Delete Index API删除原始索引。
克隆索引API相对于使用快照API或重新索引API的主要优点是速度,因为克隆索引API将段从源索引硬链接到目标索引,而无需重新处理其任何内容(显然,在支持硬链接的文件系统上;否则,文件将在文件系统级别复制,这仍然比其他方法更有效)。克隆索引还保证目标索引在每个点都与源索引相同(即,不需要手动复制设置和Map,这与重新索引方法相反),并且不需要配置本地快照目录。

***旁注:***尽管此过程比以前的解决方案快得多,但它仍然意味着停机时间。有真实的使用案例证明重命名索引是合理的(例如,作为剥离、收缩或备份工作流中的一个步骤),但重命名索引不应成为日常操作的一部分。如果您的工作流需要频繁重命名索引,则应考虑改用Indices Aliases

以下是将索引source_index重命名为target_index的完整操作序列示例。可以使用某些ElasticSearch特定控制台(如集成的in Kibana)执行此操作。有关此示例的替代版本,请参阅this gist,该版本使用curl而不是ElasticSearch控制台。

# 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
enyaitl3

enyaitl33#

要重命名索引,您可以使用Elasticsearch快照模块。
首先,你必须采取快照您的索引。而恢复它,你可以重命名您的索引。

POST /_snapshot/my_backup/snapshot_1/_restore
    {
     "indices": "jal",
     "ignore_unavailable": "true",
     "include_global_state": false,
     "rename_pattern": "jal",
     "rename_replacement": "jal1"
     }

rename_replacement:-要在其中备份数据新索引名称。

pgx2nnw8

pgx2nnw84#

如果无法REINDEX,解决方法是使用 aliases。从official文档:

  • elasticsearch中的API在处理特定索引时接受一个索引名称,在适用时接受多个索引。索引别名API允许使用名称为索引起别名,所有API都自动将别名转换为实际索引名称。别名也可以Map到多个索引,指定别名时,别名将自动扩展到别名索引。别名还可以与筛选器关联,该筛选器将在搜索和路由值时自动应用。别名不能与索引同名。*

请注意,如果您使用的是“更多类似内容”功能,则此解决方案不起作用。https://github.com/elastic/elasticsearch/issues/16560

vfhzx4xs

vfhzx4xs5#

因此,在ES中没有复制或重命名索引的直接方法(我确实广泛地搜索了我自己的项目)
然而,一个非常简单的选择是使用流行的迁移工具[Elastic-Exporter]。
http://www.retailmenot.com/corp/eng/posts/2014/12/02/elasticsearch-cluster-migration/
[PS:这不是我的博客,只是偶然发现并发现它不错]
因此,您可以复制index/type,然后删除旧的。

vqlkdk9b

vqlkdk9b6#

实现重命名或更改索引Map的另一种不同方法是使用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"
 }
}
relj7zay

relj7zay7#

如快照模块的Elasticsearch参考中所示,
rename_pattern和rename_replacement选项也可用于在使用正则表达式恢复时重命名索引

bvjveswy

bvjveswy8#

对于那些基于OpenSearch登陆这里的用户,以下是您的操作方法...
PUT /<source-index>/_block/write
DELETE <target-index>(如果已存在)
PUT /<source-index>/_clone/<target-index>

dvtswwa3

dvtswwa39#

以防万一有人仍然需要它。成功的(非官方的)重命名索引的方法是:
1.关闭需要重命名的索引
1.重命名主节点和数据节点的所有数据目录中的索引文件夹。
1.重新打开旧的关闭的索引(我使用kofp插件)。旧的索引将被重新打开,但保持未分配。新的索引将出现在关闭状态
1.重新打开新索引
1.删除旧索引
如果您碰巧得到这个错误“悬挂索引目录名称是”,删除所有主节点(不是数据节点)中的索引文件夹,并重新启动其中一个数据节点。

相关问题