# es_reindex_python **Repository Path**: baOZe/es_reindex_python ## Basic Information - **Project Name**: es_reindex_python - **Description**: 用来重建索引的工具,当你的某个索引创建时没有应用索引模板,可用此工具批量进行重索引 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-19 - **Last Updated**: 2025-08-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Elasticsearch 重索引工具 这是一个用于 Elasticsearch 索引重索引的 Python 工具,主要解决因缺少索引模板导致的字段类型不符合要求的问题。 ## 功能特点 - 支持批量重索引多个索引 - 安全的重索引流程:源索引 → 临时索引 → 删除源索引 → 重建源索引 - 支持多种 ES 连接方式(HTTP/HTTPS、认证等) - 详细的日志输出和错误处理 - 支持从文件读取索引列表 - 提供干运行模式用于预览操作 ## 安装步骤 ### 1. 克隆或下载项目文件 确保您有以下文件: - `es_reindex.py` - 主程序文件 - `requirements.txt` - 依赖包列表 - `README.md` - 说明文档 ### 2. 安装 Python 依赖 ```bash pip install -r requirements.txt ``` 或者直接安装: ```bash pip install elasticsearch ``` ## 使用方法 ### 基本用法 ```bash # 重索引单个索引 python es_reindex.py --host localhost --indices my-index-1 # 重索引多个索引 python es_reindex.py --host localhost --indices my-index-1 my-index-2 my-index-3 # 使用认证 python es_reindex.py --host localhost --username elastic --password yourpassword --indices my-index-1 # 使用 HTTPS 连接 python es_reindex.py --host localhost --port 9200 --ssl --indices my-index-1 ``` ### 从文件读取索引列表 创建一个文本文件(如 `indices.txt`),每行一个索引名: ``` my-index-1 my-index-2 my-index-3 # 这是注释,会被忽略 my-index-4 ``` 然后运行: ```bash python es_reindex.py --host localhost --indices-file indices.txt ``` ### 干运行模式 在实际执行前预览将要处理的索引: ```bash python es_reindex.py --host localhost --indices my-index-1 my-index-2 --dry-run ``` ### 详细日志输出 ```bash python es_reindex.py --host localhost --indices my-index-1 --verbose ``` ### 静默模式(无确认提示) ```bash # 使用 --silent 参数跳过所有确认提示 python es_reindex.py --host localhost --indices my-index-1 --silent # 简写形式 python es_reindex.py --host localhost --indices my-index-1 -y ``` ## 命令行参数 ### 必需参数 - `--host` - Elasticsearch 主机地址 ### 索引参数(二选一) - `--indices` - 要重索引的索引名称列表(空格分隔) - `--indices-file` - 包含索引名称列表的文件路径 ### 连接参数 - `--port` - Elasticsearch 端口(默认:9200) - `--username` - 用户名(可选) - `--password` - 密码(可选) - `--ssl` - 使用 SSL 连接 - `--no-verify-certs` - 不验证 SSL 证书 ### 其他参数 - `--dry-run` - 仅显示将要处理的索引,不执行实际操作 - `--verbose`, `-v` - 显示详细日志 - `--silent`, `--yes`, `-y` - 静默模式,不进行确认提示,自动同意所有删除操作 ## 重索引流程 工具按以下步骤执行重索引: 1. **创建临时索引** - 复制源索引的映射和设置创建临时索引 2. **数据迁移到临时索引** - 将源索引的所有数据重索引到临时索引 3. **删除源索引** - 删除原始索引 4. **重新创建源索引** - 创建空的源索引,让 ES 自动应用最新的索引模板 5. **数据回迁** - 将临时索引的数据重索引回新创建的源索引 6. **清理临时索引** - 删除临时索引 这个流程确保了: - 数据不会丢失 - 索引名称保持不变 - **关键**:重新创建的源索引会自动应用最新的索引模板,解决字段类型不符合要求的问题 - 完整保留了原始数据 ## 示例场景 ### 场景1:单个索引重索引 ```bash python es_reindex.py --host 192.168.1.100 --port 9200 --indices user-logs-2024 ``` ### 场景2:带认证的 HTTPS 连接 ```bash python es_reindex.py --host es.example.com --port 9200 --ssl --username elastic --password mypassword --indices app-logs-* ``` ### 场景3:批量处理多个索引 创建 `reindex_list.txt`: ``` user-logs-2024-01 user-logs-2024-02 app-logs-2024-01 app-logs-2024-02 ``` 执行: ```bash python es_reindex.py --host localhost --indices-file reindex_list.txt --verbose ``` ## 注意事项 1. **数据备份**:虽然工具设计得很安全,但建议在重索引前备份重要数据 2. **索引模板**:确保在运行工具前已经创建了正确的索引模板 3. **集群负载**:重索引会对集群造成负载,建议在业务低峰期运行 4. **磁盘空间**:重索引过程中需要额外的磁盘空间存储临时索引 5. **时间估算**:大索引的重索引可能需要较长时间,请耐心等待 6. **静默模式风险**:使用 `--silent` 模式时要特别小心,工具将不会要求确认就直接删除索引 ## 错误处理 - 工具会自动清理失败操作产生的临时索引 - 详细的错误日志帮助诊断问题 - 支持重试机制处理临时网络问题 ## 版本要求 - Python 3.6+ - Elasticsearch 7.0+ (支持到 8.x) ## 许可证 本工具仅供内部使用,请根据您的组织政策使用。