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

WoJ*_*WoJ 103 elasticsearch

我需要重命名集群中的几个索引(必须更改其名称,我不能使用别名).

我看到没有支持的方法可以做到这一点,我发现最接近的是重命名索引的目录,我在集群中尝试了这个.

群集有3台计算机A,B并且C每个计算机都会复制分片.我关闭了上elasticsearch A,改名/var/lib/elasticsearch/security/nodes/0/indices/oldindexname/var/lib/elasticsearch/security/nodes/0/indices/newindexname并重新启动A.

集群的状态是黄色的,而elasticsearch正在为恢复正确的状态做一些魔术.过了一段时间,我结束了

  • oldindexname可用并完全复制(从中恢复B并且C我猜)
  • newindexname 可用(我可以搜索它)但是头插件显示其分片处于"未分配"状态并且它们显示为灰色(未复制)

恢复期间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
Run Code Online (Sandbox Code Playgroud)

虽然newindexname可以搜索,但肯定不是正常状态.

我通过删除回滚到以前的状态newindexname.群集返回绿色,没有任何"未分配"条目.

鉴于这种情况,我怎么可以重命名oldindexnamenewindexname集群中的?

注意:我想到的最终解决方案是滚动复制oldindex到之后newindex删除oldindex.这需要时间,所以如果有更直接的解决方案,那就太棒了.

ret*_*eto 153

您可以使用REINDEX来做到这一点.

Reindex不会尝试设置目标索引.它不会复制源索引的设置.您应该在运行_reindex操作之前设置目标索引,包括设置映射,分片计数,副本等.

  1. 首先将索引复制到新名称
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 现在删除索引
DELETE /twitter
Run Code Online (Sandbox Code Playgroud)

  • 据我所知,这不会将`twitter`的映射复制到`new_twitter`. (6认同)
  • 我同意_reindex的解决方案,但问题应该改变.重新索引不仅仅是重命名.它甚至可以改变数据索引的方式. (3认同)
  • 如果映射具有`_source: {enabled: false}`,这是否有效? (2认同)
  • @Harald 不,`_reindex` 使用 `_source` 作为原始文档数据。 (2认同)

小智 57

要重命名索引,可以使用Elasticsearch Snapshot模块.

首先,您必须拍摄索引的快照.在恢复它时,您可以重命名索引.

    POST /_snapshot/my_backup/snapshot_1/_restore
    {
     "indices": "jal",
     "ignore_unavailable": "true",
     "include_global_state": false,
     "rename_pattern": "jal",
     "rename_replacement": "jal1"
     }
Run Code Online (Sandbox Code Playgroud)

rename_replacement: - 要备份数据的新索引名称.

  • Holycrap,这真是有用。谢谢! (3认同)

jwa*_*ins 55

从 ElasticSearch 7.4 开始,重命名索引的最佳方法是使用新引入的Clone Index API 复制索引,然后使用Delete Index API删除原始索引。

与出于相同目的使用 Snapshot API 或 Reindex API 相比,Clone Index API 的主要优势在于速度,因为 Clone Index API 将片段从源索引硬链接到目标索引,而无需重新处理其任何内容(在支持硬链接的文件系统,显然;否则,文件在文件系统级别复制,这仍然比替代方案更有效)。Clone Index 还保证目标索引在每个点都与源索引相同(也就是说,不需要手动复制设置和映射,与 Reindex 方法相反),并且不需要配置本地快照目录.

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

下面是完整的操作序列索引重命名的例子source_indextarget_index。它可以使用某些特定于 ElasticSearch 的控制台执行,例如集成在 Kibana 中的控制台。有关示例的替代版本,请参阅此要点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
Run Code Online (Sandbox Code Playgroud)


Leo*_*Leo 6

如果你不能REINDEX一个解决方法是使用别名.从官方文档:

elasticsearch中的API在针对特定索引时接受索引名称,并在适用时接受多个索引.索引别名API允许使用名称对索引进行别名,所有API都自动将别名转换为实际索引名称.别名也可以映射到多个索引,并且在指定别名时,别名将自动扩展为别名索引.别名还可以与在搜索和路由值时自动应用的过滤器相关联.别名不能与索引同名.

请注意,如果您使用"更多此功能",此解决方案将无效.https://github.com/elastic/elasticsearch/issues/16560

  • “我需要重命名集群中的多个索引(必须更改它们的名称,我不能使用别名)。” 作者:@WoJ (2认同)

laz*_*wiz 5

因此,没有直接的方法来复制或重命名ES中的索引(我确实广泛搜索了我自己的项目)

但是,一个非常简单的选择是使用流行的迁移工具[Elastic-Exporter].

http://www.retailmenot.com/corp/eng/posts/2014/12/02/elasticsearch-cluster-migration/

[PS:这不是我的博客,只是偶然发现并发现它很好]

从而您可以复制索引/类型,然后删除旧的索引/类型.


小智 5

另一种实现重命名或更改索引映射的方法是使用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"
 }
}
Run Code Online (Sandbox Code Playgroud)

  • 所以你说重新索引Elasticsearch索引的最好方法是安装Logstash,然后用它来重新索引?看起来有点矫枉过正,特别是如果你真的不想/使用Logstash ...... (4认同)