使用Reindex将数据迁移至PolarSearch

更新时间:
复制为 MD 格式

Reindex API用于将自建ElasticsearchOpenSearch集群的数据迁移至PolarSearch。该API通过网络从远端源集群拉取索引数据到PolarSearch。

准备工作

开始操作前,请确认以下条件:

  • 环境依赖:源ElasticsearchOpenSearch集群与PolarSearch节点部署在同一专有网络(VPC)中。如果源集群不在同一VPC中,请在操作前完成两个环境之间的网络连通配置。

  • 版本对应:PolarSearch版本与源集群版本需兼容。详细兼容说明,请参见版本选型指南

步骤一:创建目标索引

执行Reindex任务前,在PolarSearch节点上创建与源索引相同SettingsMappings的目标索引。

  1. 查询源索引的Settings。

    curl -XGET "http://<source_address>/<source_index>/_settings?pretty"
  2. 查询源索引的Mappings。

    curl -XGET "http://<source_address>/<source_index>/_mapping?pretty"
  3. PolarSearch节点上创建目标索引。将<source_settings><source_mappings>替换为前述步骤获取的实际SettingsMappings。

    curl -X PUT "http://<dest_address>/<dest_index>?pretty" \
      -u <dest_username>:<dest_password> \
      -H 'Content-Type: application/json' \
      -d'{
      "settings": {
        <source_settings>
      },
      "mappings": {
        <source_mappings>
      }
    }'

步骤二:配置Reindex白名单

要允许PolarSearch节点访问远端源集群,需要将源集群地址添加到Reindex白名单中。如果未配置白名单,Reindex任务会返回以下错误之一:

  • [address] not allowlisted in reindex.remote.allowlist

  • [address] not allowlisted in reindex.remote.whitelist

重要
  • 目前暂不支持您手动配置白名单,如有需求,请提交工单联系我们为您处理。

  • 白名单参数为静态配置,修改后需要重启PolarSearch节点才能生效。请在业务低峰期进行重启,以减少对业务的影响。

步骤三:执行Reindex任务

  1. PolarSearch上执行以下命令启动Reindex任务。数据将从远端源集群拉取到PolarSearch的目标索引。

    curl -X POST "http://<dest_address>/_reindex?pretty" \
      -u <dest_username>:<dest_password> \
      -H 'Content-Type: application/json' \
      -d'{
      "source": {
        "remote": {
          "host": "http://<source_address>",
          "username": "<source_username>",
          "password": "<source_password>"
        },
        "index": "<source_index>" // 需要Reindex迁移的源集群索引名
      },
      "dest": {
        "index": "<dest_index>" // 迁移至PolarSearch的目标索引名
      }
    }'

    参数说明

    参数

    说明

    source_address

    ElasticsearchOpenSearch集群的访问地址,格式为host:port

    source_username

    访问源集群的用户名。如果源集群不需要身份验证,可以省略usernamepassword字段。

    source_password

    访问源集群的密码。

    source_index

    待迁移的源索引名称。

    dest_address

    PolarSearch的访问地址。

    dest_username

    访问PolarSearch的用户名。

    dest_password

    访问PolarSearch的密码。

    dest_index

    PolarSearch上的目标索引名称。

  2. Reindex任务完成后,通过比较源索引和目标索引的文档数量来验证迁移结果。

    # 查询源索引的文档数量
    curl -XGET "http://<source_address>/<source_index>/_count?pretty"
    
    # 查询目标索引的文档数量
    curl -XGET "http://<dest_address>/<dest_index>/_count?pretty" \
      -u <dest_username>:<dest_password>

常见问题

  • Q1:单次Reindex任务是否支持迁移多个索引?

    不支持。Reindex API仅支持一对一的索引迁移。如需迁移多个索引,请为每个索引分别执行Reindex任务。同时,在Reindex迁移过程中不建议使用单次Reindex命令中使用多对一的方式进行数据迁移,容易引起数据或者字段混淆。若需要迁移源端多个索引,可以使用多次Reindex命令一对一的方式进行迁移。例如:将test_index1迁移至test_index_copy1test_index2迁移至test_index_copy2

    curl -X POST "http://<dest_address>/_reindex?pretty" \
      -u <dest_username>:<dest_password> \
      -H 'Content-Type: application/json' \
      -d'{
      "source": {
        "remote": {
          "host": "http://<source_address>",
          "username": "<source_username>",
          "password": "<source_password>"
        },
        "index": "test_index1" // 需要Reindex迁移的源集群索引名
      },
      "dest": {
        "index": "test_index_copy1" // 迁移至PolarSearch的目标索引名
      }
    }'
    
    curl -X POST "http://<dest_address>/_reindex?pretty" \
      -u <dest_username>:<dest_password> \
      -H 'Content-Type: application/json' \
      -d'{
      "source": {
        "remote": {
          "host": "http://<source_address>",
          "username": "<source_username>",
          "password": "<source_password>"
        },
        "index": "test_index2" // 需要Reindex迁移的源集群索引名
      },
      "dest": {
        "index": "test_index_copy2" // 迁移至PolarSearch的目标索引名
      }
    }'
  • Q2:Reindex任务失败并报illegal_argument_exception错误,如何处理?

    该错误表示源索引中的字段类型与目标索引的Mappings不兼容。执行Reindex任务前,请确保目标索引的Mappings与源索引一致。

    错误示例:

    "cause" : {
      "type" : "illegal_argument_exception",
      "reason" : "mapper [...] cannot be changed from type [float] to [long]"
    }
  • Q3:如何为多个源集群配置白名单?

    如需从多个源集群迁移数据,可以在提交工单联系我们的时候,将白名单参数中添加多个地址,用逗号分隔。例如:reindex.remote.allowlist: "host1:9200, host2:9200"

附录:Reindex可用参数

请求示例

curl -X POST "http://<dest_address>/_reindex?pretty&refresh=false&timeout=30s&wait_for_active_shards=1&wait_for_completion=false&requests_per_second=-1&require_alias=false&scroll=5m&slices=1&max_docs=100000"
-u <dest_username>:<dest_password>
-H 'Content-Type: application/json'
-d'
{
  "source": {
    "remote": {
      "host": "http://<source_address>",
      "username": "<source_username>",
      "password": "<source_password>"
    },
    "index": "test_index" // 需要reindex迁移的索引名
  },
  "dest": {
    "index": "test_index_copy"  // 迁移至PolarSearch的索引名
  }
}'

参数说明

参数

类型

默认值

含义

wait_for_completion

Boolean

false

是否等待任务完成。

  • false:立即返回一个task_id,任务在后台异步执行。

  • true:客户端将阻塞等待,直到任务完成才返回结果,适用于数据量小的场景,数据量大时易超时。

requests_per_second

Integer

-1

每秒处理的子请求数,用于限流。-1表示不限流,可能会对集群造成较大负载。建议从一个较小值开始测试,并根据集群负载情况进行调整。

timeout

Time

30s

请求的超时时间。您可以根据网络情况和任务复杂度适当延长此值(例如5m),以避免因网络波动导致任务中断。

refresh

Boolean

false

写入后是否立即刷新,使数据对搜索可见。true可实现近实时搜索,但会显著增加系统开销并降低写入性能。

slices

Integer / "auto"

1

任务并行度,即将任务拆分为多少个子任务并行执行。设置为"auto"将由系统自动决定并行数。

说明

跨集群Reindex时,slices只支持设置为1

max_docs

Integer

-

本次任务最多处理的文档数。默认处理所有匹配的文档。您可以设置一个较小的值(如1000)用于测试和验证。

wait_for_active_shards

String

1

执行任务前需激活的分片数。在开始处理请求前,必须有指定数量的活跃分片可用。可选值为"all"或具体整数。

scroll

Time

5m

滚动查询的上下文保持时间。在Reindex过程中,用于保持源索引的搜索上下文(Scroll Context)有效。

require_alias

Boolean

false

是否强制要求目标为别名。如果设置为true,则目标索引(dest.index)必须是一个别名(Alias),不能是具体的索引名。