使用Reindex將資料移轉至PolarSearch

更新時間:
Copy as MD

Reindex API用於將自建ElasticsearchOpenSearch叢集的資料移轉至PolarSearch。該API通過網路從遠端源叢集拉取索引資料到PolarSearch。

準備工作

開始操作前,請確認以下條件:

  • 環境依賴:源ElasticsearchOpenSearch叢集與PolarSearch節點部署在同一Virtual Private Cloud中。如果源叢集不在同一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),不能是具體的索引名。