乐闻世界logo
搜索文章和话题

How to rename an index in a cluster in elasticsearch

7 个月前提问
3 个月前修改
浏览次数149

6个答案

1
2
3
4
5
6

在Elasticsearch中,索引的名称一旦创建之后是不能直接修改的,但是您可以通过创建索引的别名(alias)或重新索引(reindexing)的方法来间接"重命名"索引。

方法一:使用别名(Alias)

虽然不能直接重命名索引,但是您可以给索引创建一个或多个别名,这样可以通过新的别名来访问原有的索引。

创建别名的步骤如下:

  1. 使用POST或者PUT请求为现有索引创建别名:
json
POST /_aliases { "actions": [ { "add": { "index": "原始索引名", "alias": "新的别名" } } ] }
  1. 确认别名已被创建,并可以通过它访问数据。

  2. 可选的,您可以删除旧的索引名,但这样做前要确保所有写入和读取操作都已经切换到了新的别名。

方法二:重新索引(Reindexing)

如果您需要更彻底地改名,可以使用重新索引的方法。这涉及到将旧索引中的数据复制到一个新的索引中,然后您可以根据需要删除旧的索引。

重新索引的步骤如下:

  1. 创建新的索引,并指定所需的设置和映射。

  2. 使用 _reindex API 将旧索引的数据复制到新索引:

json
POST /_reindex { "source": { "index": "旧索引名" }, "dest": { "index": "新索引名" } }
  1. 在重新索引完成后,确保新索引已正确地包含了所有的数据。

  2. 更新所有应用程序和服务,以使用新的索引名称。

  3. 删除旧的索引(如果确定不再需要):

json
DELETE /旧索引名

注意: 重命名索引(特别是重新索引)是一个可能会消耗时间和资源的过程,对于大型索引或生产环境,需要谨慎进行,并考虑到可能的停机时间、数据一致性问题以及对正在进行的查询和索引操作的影响。在生产环境中,可能需要在低流量时段进行此操作,并确保有完整的备份以防万一出错。

2024年6月29日 12:07 回复

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

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

  1. 首先将索引复制到一个新名称

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

  2. 现在删除索引

    DELETE /twitter

2024年6月29日 12:07 回复

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

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

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

source_index以下是将索引重命名为 的完整操作序列的示例target_index。它可以使用一些 ElasticSearch 特定的控制台来执行,例如集成在 Kibana 中的控制台。请参阅此要点以获取此示例的替代版本,使用curl而不是 Elastic Search 控制台。

shell
# 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
2024年6月29日 12:07 回复

要重命名索引,您可以使用 Elasticsearch 快照模块。

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

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

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

2024年6月29日 12:07 回复

如果无法 REINDEX,解决方法是使用_aliases_。来自官方文档:

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

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

2024年6月29日 12:07 回复

实现重命名或更改索引映射的另一种不同方法是使用 Logstash 重新索引。以下是 Logstash 2.1 配置示例:

shell
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" } }
2024年6月29日 12:07 回复

你的答案