Skip to content

WordLin/ElasticSearchData7_8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ElasticSearchTransfer7_8

用于将 Elasticsearch 7.x 的索引与数据迁移到 Elasticsearch 8.x,重点处理向量字段兼容和大批量数据搬迁。

功能说明

  • 连接源集群(ES 7.x)和目标集群(ES 8.x)
  • 按规则过滤待迁移索引(支持白名单、包含/排除模式)
  • 自动迁移索引 mapping / settings
  • 自动适配向量字段:
    • type: vector 升级为 type: dense_vector
    • 清理 8.x 不支持的旧参数
  • 使用 scan + bulk 批量迁移数据
  • 生成迁移报告 migration_report_*.json

目录结构

.
├─ main.py           # 迁移主程序
├─ config.py         # 读取 config.yaml
├─ requirements.txt  # 依赖
└─ config.yaml       # 运行前需要自行创建

环境要求

  • Python 3.9+
  • 可访问的 Elasticsearch 7.x 源集群
  • 可访问的 Elasticsearch 8.x 目标集群

安装依赖

pip install -r requirements.txt

配置说明

程序默认读取项目根目录下的 config.yaml。请先创建该文件。

config.yaml 示例

migration_config:
  source:
    hosts: ["http://127.0.0.1:9200"]
    username: "elastic"
    password: "source_password"
    verify_certs: false

  target:
    hosts: ["https://127.0.0.1:9201"]
    username: "elastic"
    password: "target_password"
    verify_certs: false
    ca_certs: null

  migration:
    batch_size: 500
    scroll_time: "5m"
    concurrent_workers: 1

  indices:
    # 指定具体索引(非空时,仅迁移这里列出的索引)
    specific_indices: []
    # 当 specific_indices 为空时生效:索引名包含任一字符串即匹配
    include_patterns: ["rag", "kb"]
    # 排除规则:索引名包含任一字符串即排除
    exclude_patterns: [".kibana", ".security"]

索引筛选规则

程序逻辑如下:

  1. 自动跳过以 . 开头的系统索引
  2. 如果 specific_indices 非空:只迁移该列表中的索引
  3. 如果 specific_indices 为空:按 include_patternsexclude_patterns 过滤

操作步骤(推荐)

1. 先小范围验证

先在 specific_indices 中只配置 1~2 个测试索引,确认迁移和查询结果正常后再全量执行。

2. 执行迁移

python main.py

执行后流程为:

  1. 连接源/目标集群
  2. 获取并过滤索引
  3. 迁移索引结构(mapping/settings)
  4. 分批迁移文档数据
  5. 输出迁移报告

3. 查看迁移报告

运行结束后,项目目录会生成:

migration_report_YYYYMMDD_HHMMSS.json

报告包含:

  • 总索引数
  • 总文档数 / 成功数 / 失败数
  • 成功率
  • 各索引的迁移状态与耗时

迁移后验证建议

  • 对比源/目标索引文档总量(_count
  • 抽样核对关键字段(含向量字段维度)
  • 使用业务查询验证检索结果是否符合预期

常见问题

1)提示 配置文件不存在

请确认项目根目录存在 config.yaml,且字段结构与示例一致。

2)无法连接 ES 集群

  • 检查 hosts、账号密码是否正确
  • 检查网络连通与防火墙
  • HTTPS 场景确认 verify_certs 与证书配置是否匹配

3)目标索引已存在

程序会跳过已存在索引的创建,但仍会继续数据迁移。
如需重跑全量,请先手动清理目标索引(谨慎操作)。

注意事项

  • 当前脚本按顺序迁移索引(concurrent_workers 当前版本未启用并发)
  • 脚本会尝试创建别名:新别名名为“索引名去掉最后 3 个字符”,请确认你的索引命名符合预期
  • 生产迁移前建议先在测试环境完整演练

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages