Elasticsearch 数据迁移与任务状态相关 API

本文详细介绍了 Elasticsearch 的 Reindex API 和 Task API 的使用。内容涵盖基本及进阶的 Reindex 操作,如多索引多类型迁移、添加迁移条件、修改字段名等。同时讲解了 Task API 的 GET Task、Cancel Task 和等待任务完成等功能,帮助用户有效地管理和监控数据迁移任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Reindex API

说明

本文所述命令,皆基于 ES_V5.4.2 版本。

本文根据 ES 官方文档进行翻译、总结而出:

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/docs-reindex.html

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/tasks.html

基本形式

POST _reindex
{
  "source": {
    "index": "source_index"
  },
  "dest": {
    "index": "target_index"
  }
}

将文档从 source_index 索引复制到 target_index 索引中。

注意:

​ Reindex 不会复制源索引的设置,应在执行 reindex 操作之前,提前设置好目标索引的映射、分片数等。

类似 _update_by_query ,执行 reindex 时,会在操作时间点生成一个快照,复制文档时均是快照中文档状态,因此在快照时间点之后,对源索引进行的数据写入、更新、删除等操作,均不会同步变更到目标索引中。

如果是复制文档到新索引,那么不会出现文档版本冲突的问题,否则需考虑版本冲突问题,可设置 version_type 或 op_type 参数:

POST _reindex
{
  "source": {
    "index": "source_index"
  },
  "dest": {
    "index": "target_index",
    "version_type": "external"
  }
}

参数说明:

  • version_type

    • internal:忽略文档版本,如果目标索引中恰巧有相同主键的文档,则直接覆盖;(默认)
    • external:保留源索引中文档版本,创建目标索引中没有的文档,并更新目标索引中比源索引中版本旧的文档
  • op_type

    • create:仅在目标索引中创建丢失的文档,所有现有文档将导致版本冲突

    默认情况下,版本冲突会导致 reindex 终止,可通过 “conflicts”: “proceed” 参数忽略冲突:

    POST _reindex
    {
      "conflicts": "proceed",
      "source": {
        "index": "source_index"
      },
      "dest": {
        "index": "target_index",
        "op_type": "create"
      }
    }
    

进阶形式

多索引多类型

POST _reindex
{
  "conflicts": "proceed",
  "source": {
    "index": ["source_index_1","source_index_2"],
    "type": ["source_index_1_type","source_index_2_type"]
  },
  "dest": {
    "index": "target_index_together"
  }
}

将 source_index_1/source_index_1_type 和 source_index_2/source_index_2_type 的文档复制到 target_index_together 索引中。

如果两个索引中存在主键相同的文档,因迭代顺序无法确定,亦无法预测目标索引中哪个文档将继续存在。

此种场景,一般不存在。

增加迁移条件

POST _reindex
{
  "conflicts": "proceed",
  "source": {
    "index": "source_index",
    "query": {
      "match_all": {}
    }
  },
  "dest": {
    "index": "target_index"
  }
}

只将源索引中符合 query 条件的文档复制到目标索引中。

迁移固定文档数

POST _reindex
{
  "conflicts": "proceed",
  "size": 10000,
  "source": {
    "index": "source_index",
    "sort"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值