修改ES索引Mapping
背景:
之前的有一個字段設置是keyword,然而,由於業務場景的調整,現在改成text,模糊檢索,爲了需求變更,需要進行修改ES Mapping
操作步驟
-
創建新的索引
POST new_index/_mapping { "properties": { "id": { "type": "keyword" }, "filed_name": { "type": "text", "analyzer": "standard", "search_analyzer": "standard", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }
-
將新索引的別名設置爲業務需要的別名
POST/PUT都可以,ES新創建的索引默認是不可寫入的,需要加入is_write_index爲true的設定
POST _aliases { "actions": [ { "add": { "index": "new_index", "alias": "business_index" ,"is_write_index": true } } ] }
不進行設置寫入設置,會有異常如下
no write index is defined for alias [o2o-nearby-service_brand_merchant]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index
-
從指定index上面拷貝數據
POST _reindex { "source": { "index": "old_index" }, "dest": { "index": "new_index" } }
-
驗證是否數據是否拷貝成功
# 數據數量驗證 GET old_index/_count GET new_index/_count # 數據詳細比對驗證 GET old_index/_search GET new_index/_search
如果驗證有問題,重複步驟3,如果線上場景複雜,建議不要使用該方式操作,更建議使用API文檔使用查詢後,_bluk批量的方式,用代碼實現數據遷移到新索引
-
移除舊索引的業務映射
POST _aliases { "actions": [ { "remove": { "index": "old_index", "alias": "business_index" } } ] }