Reindex API用于将自建Elasticsearch或OpenSearch集群的数据迁移至PolarSearch。该API通过网络从远端源集群拉取索引数据到PolarSearch。
准备工作
开始操作前,请确认以下条件:
环境依赖:源Elasticsearch或OpenSearch集群与PolarSearch节点部署在同一专有网络(VPC)中。如果源集群不在同一VPC中,请在操作前完成两个环境之间的网络连通配置。
版本对应:PolarSearch版本与源集群版本需兼容。详细兼容说明,请参见版本选型指南。
步骤一:创建目标索引
执行Reindex任务前,在PolarSearch节点上创建与源索引相同Settings和Mappings的目标索引。
查询源索引的Settings。
curl -XGET "http://<source_address>/<source_index>/_settings?pretty"查询源索引的Mappings。
curl -XGET "http://<source_address>/<source_index>/_mapping?pretty"在PolarSearch节点上创建目标索引。将
<source_settings>和<source_mappings>替换为前述步骤获取的实际Settings和Mappings。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任务
在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源Elasticsearch或OpenSearch集群的访问地址,格式为
host:port。source_username访问源集群的用户名。如果源集群不需要身份验证,可以省略
username和password字段。source_password访问源集群的密码。
source_index待迁移的源索引名称。
dest_addressPolarSearch的访问地址。
dest_username访问PolarSearch的用户名。
dest_password访问PolarSearch的密码。
dest_indexPolarSearch上的目标索引名称。
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_copy1,test_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的索引名
}
}'参数说明
参数 | 类型 | 默认值 | 含义 |
| Boolean |
| 是否等待任务完成。
|
| Integer |
| 每秒处理的子请求数,用于限流。 |
| Time |
| 请求的超时时间。您可以根据网络情况和任务复杂度适当延长此值(例如 |
| Boolean |
| 写入后是否立即刷新,使数据对搜索可见。 |
| Integer / "auto" |
| 任务并行度,即将任务拆分为多少个子任务并行执行。设置为 说明 跨集群Reindex时, |
| Integer | - | 本次任务最多处理的文档数。默认处理所有匹配的文档。您可以设置一个较小的值(如 |
| String |
| 执行任务前需激活的分片数。在开始处理请求前,必须有指定数量的活跃分片可用。可选值为 |
| Time |
| 滚动查询的上下文保持时间。在Reindex过程中,用于保持源索引的搜索上下文(Scroll Context)有效。 |
| Boolean |
| 是否强制要求目标为别名。如果设置为 |