避坑 + 實(shí)操!Elasticdump 遷移 Elasticsearch 數(shù)據(jù)到 Easysearch,看這篇就夠了
一、背景介紹
在實(shí)際項目中,我們常常需要將數(shù)據(jù)從 Elasticsearch 遷移到 Easysearch(或其他兼容的搜索引擎)。
圖片
Elasticdump 是一個強(qiáng)大的開源工具,可以幫助我們輕松完成索引的映射、設(shè)置和數(shù)據(jù)的遷移工作。
圖片
https://github.com/elasticsearch-dump/elasticsearch-dump
本文將詳細(xì)介紹如何使用 Elasticdump 將 Elasticsearch 的索引完整遷移到 Easysearch,包括環(huán)境準(zhǔn)備、遷移步驟和常見問題處理。
二、工具介紹
2.1 什么是 Elasticdump?
Elasticdump 是一個用于導(dǎo)入和導(dǎo)出 Elasticsearch 索引數(shù)據(jù)的命令行工具,支持:
- 索引映射(mapping)遷移
- 索引設(shè)置(settings)遷移
- 索引數(shù)據(jù)(data)遷移
- 支持 HTTPS 和身份認(rèn)證
- 支持批量數(shù)據(jù)傳輸
2.2 遷移內(nèi)容說明
完整的索引遷移包含三個核心部分:
- Mapping(映射):定義索引的字段類型和結(jié)構(gòu)
- Settings(設(shè)置):定義索引的分片、副本等配置
- Data(數(shù)據(jù)):實(shí)際的文檔數(shù)據(jù)
三、環(huán)境準(zhǔn)備
3.1 安裝 Node.js
Elasticdump 基于 Node.js,首先需要安裝 Node.js 環(huán)境:
# 訪問 Node.js 官網(wǎng)下載安裝
# https://nodejs.org/3.2 安裝 Elasticdump
npm install -g elasticdump
圖片
3.3 環(huán)境信息確認(rèn)
在本案例中:
- 源端(Elasticsearch):https://外網(wǎng)IP地址:9200
用戶名:elastic
密碼:changeme
使用 HTTPS 協(xié)議
- 目標(biāo)端(Easysearch):http://127.0.0.1:9200
本地部署
使用 HTTP 協(xié)議
- 遷移索引:product-index
四、遷移步驟詳解
4.1 處理 SSL 證書問題
由于源端 Elasticsearch 使用自簽名證書,我們需要先設(shè)置環(huán)境變量以忽略 SSL 驗(yàn)證:
Windows 系統(tǒng):
set NODE_TLS_REJECT_UNAUTHORIZED=0Linux/Mac 系統(tǒng):
export NODE_TLS_REJECT_UNAUTHORIZED=0?? 注意:此設(shè)置僅適用于開發(fā)/測試環(huán)境。生產(chǎn)環(huán)境建議使用有效的 SSL 證書。
4.2 步驟一:遷移索引映射(Mapping)
映射定義了索引的數(shù)據(jù)結(jié)構(gòu),必須首先遷移:
elasticdump \
--input=https://elastic:changeme@外網(wǎng)ip:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=mapping參數(shù)說明:
- --input:源 Elasticsearch 地址(包含認(rèn)證信息)
- --output:目標(biāo) Easysearch 地址
- --type=mapping:指定遷移類型為映射
預(yù)期結(jié)果:
Fri, 04 Oct 2025 12:00:00 GMT | got 1 objects from source elasticsearch (offset: 0)
Fri, 04 Oct 2025 12:00:00 GMT | sent 1 objects to destination elasticsearch, wrote 1
Fri, 04 Oct 2025 12:00:00 GMT | got 0 objects from source elasticsearch (offset: 1)
Fri, 04 Oct 2025 12:00:00 GMT | Total Writes: 1
Fri, 04 Oct 2025 12:00:00 GMT | dump complete4.3 步驟二:遷移索引設(shè)置(Settings)
設(shè)置包含分片數(shù)、副本數(shù)等配置信息:
elasticdump \
--input=https://elastic:changeme@外網(wǎng)IP:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=settings參數(shù)說明:
- --type=settings:指定遷移類型為設(shè)置
?? 提示:某些設(shè)置(如 number_of_shards)在索引創(chuàng)建后無法修改,可能會收到警告,這是正常現(xiàn)象。
4.4 步驟三:遷移索引數(shù)據(jù)(Data)
最后遷移實(shí)際的文檔數(shù)據(jù):
elasticdump \
--input=https://elastic:changeme@外網(wǎng)ip:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=data \
--limit=5000參數(shù)說明:
- --type=data:指定遷移類型為數(shù)據(jù)
- --limit=5000:每批次傳輸 5000 條文檔(可根據(jù)網(wǎng)絡(luò)和性能調(diào)整)
性能優(yōu)化建議:
- 對于小數(shù)據(jù)集:--limit=1000 ~ 5000
- 對于大數(shù)據(jù)集:--limit=5000 ~ 10000
- 可以添加 --cnotallow=3 參數(shù)啟用并發(fā)傳輸
五、使用 CA 證書的方式(可選)
如果不想禁用 SSL 驗(yàn)證,可以使用 CA 證書:
# 遷移 Mapping
elasticdump \
--input=https://elastic:changeme@外網(wǎng)ip:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=mapping \
--input-ca="D:\software\easysearch-1.15.3-2373-windows-amd64\http_ca.crt"
# 遷移 Settings
elasticdump \
--input=https://elastic:changeme@外網(wǎng)ip:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=settings \
--input-ca="D:\software\easysearch-1.15.3-2373-windows-amd64\http_ca.crt"
# 遷移 Data
elasticdump \
--input=https://elastic:changeme@外網(wǎng)ip:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=data \
--limit=5000 \
--input-ca="D:\software\easysearch-1.15.3-2373-windows-amd64\http_ca.crt"六、驗(yàn)證遷移結(jié)果
6.1 檢查索引是否創(chuàng)建
curl http://127.0.0.1:9200/_cat/indices?v
圖片
6.2 對比文檔數(shù)量
源端:
curl -u elastic:changeme -k https://外網(wǎng)ip:9200/product-index/_count
圖片
目標(biāo)端:
curl http://127.0.0.1:9200/product-index/_count
圖片
6.3 抽樣驗(yàn)證數(shù)據(jù)
# 查詢目標(biāo)索引的前 10 條數(shù)據(jù)
curl http://127.0.0.1:9200/product-index/_search?size=10
圖片
七、常見問題與解決方案
7.1 SSL 證書錯誤
錯誤信息:
Error: unable to verify the first certificate解決方案:
- 設(shè)置 NODE_TLS_REJECT_UNAUTHORIZED=0
- 或使用 --input-ca 指定證書路徑
7.2 認(rèn)證失敗
錯誤信息:
Error: 401 Unauthorized解決方案:
- 檢查用戶名密碼是否正確
- 確保 URL 格式:https://username:password@host:port/index
7.3 目標(biāo)索引已存在
錯誤信息:
Error: resource_already_exists_exception解決方案:
# 刪除已存在的索引
curl -X DELETE http://127.0.0.1:9200/product-index7.4 大數(shù)據(jù)集遷移超時
解決方案:
- 減小 --limit 值
- 增加超時時間:--timeout=120000(毫秒)
- 使用 --concurrency 參數(shù):--cnotallow=3
八、完整遷移腳本
為了方便使用,可以創(chuàng)建一個批處理腳本:
Windows (migrate.bat):
@echo off
echo ========================================
echo Elasticsearch to Easysearch Migration
echo ========================================
:: 設(shè)置環(huán)境變量
set NODE_TLS_REJECT_UNAUTHORIZED=0
:: 源和目標(biāo)配置
set SOURCE=https://elastic:changeme@外網(wǎng)ip:9200
set TARGET=http://127.0.0.1:9200
set INDEX=product-index
echo.
echo Step 1: Migrating Mapping...
elasticdump --input=%SOURCE%/%INDEX% --output=%TARGET%/%INDEX% --type=mapping
if %errorlevel% neq 0 goto error
echo.
echo Step 2: Migrating Settings...
elasticdump --input=%SOURCE%/%INDEX% --output=%TARGET%/%INDEX% --type=settings
if %errorlevel% neq 0 goto error
echo.
echo Step 3: Migrating Data...
elasticdump --input=%SOURCE%/%INDEX% --output=%TARGET%/%INDEX% --type=data --limit=5000
if %errorlevel% neq 0 goto error
echo.
echo ========================================
echo Migration completed successfully!
echo ========================================
goto end
:error
echo.
echo ========================================
echo Migration failed! Please check errors above.
echo ========================================
:end
pause九、性能優(yōu)化建議
- 批量大小調(diào)整:根據(jù)文檔大小調(diào)整 --limit 參數(shù)
- 并發(fā)傳輸:使用 --concurrency 提升速度
- 網(wǎng)絡(luò)優(yōu)化:確保源端和目標(biāo)端網(wǎng)絡(luò)通暢
- 分時遷移:大數(shù)據(jù)量可以在業(yè)務(wù)低峰期進(jìn)行
- 增量遷移:使用查詢條件遷移部分?jǐn)?shù)據(jù)
增量遷移示例:
elasticdump \
--input=https://elastic:changeme@外網(wǎng)ip:9200/product-index \
--output=http://127.0.0.1:9200/product-index \
--type=data \
--searchBody='{"query":{"range":{"created_at":{"gte":"2025-01-01"}}}}'十、總結(jié)
通過 Elasticdump 工具,我們可以快速、安全地將 Elasticsearch 索引遷移到 Easysearch。關(guān)鍵步驟包括:
- ? 安裝并配置 Elasticdump
- ? 處理 SSL 證書問題
- ? 按順序遷移 Mapping → Settings → Data
- ? 驗(yàn)證遷移結(jié)果
- ? 根據(jù)實(shí)際情況優(yōu)化性能參數(shù)
遷移完成后,建議進(jìn)行充分的功能測試和性能測試,確保業(yè)務(wù)正常運(yùn)行。
參考資源:
- Elasticdump 官方文檔:https://github.com/elasticsearch-dump/elasticsearch-dump
- Elasticsearch 官方文檔:https://www.elastic.co/guide/
- Easysearch 官方文檔:https://docs.infinilabs.com/console/main/zh/docs/



























