ElasticSearch索引重建
ElasticSearch索引一旦建立,便不可修改索引字段類型(允許增加或者删除該字段)
例如從Integer類型修改為long類型,這是不被允許的,錯誤資訊如下:
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "mapper [age] cannot be changed from type [integer] to [long]"
}
],
"type" : "illegal_argument_exception",
"reason" : "mapper [age] cannot be changed from type [integer] to [long]"
},
"status" : 400
}
是以,如果項目中有需求需要修改ElasticSearch的索引中字段的類型,則需要重建索引
以下介紹通過别名_aliase的方式實作不停機的重建ElasticSearch索引
前提:項目中使用的索引的别名,例如索引是index_user ,使用的是别名index_user_latest
索引重建的步驟:
step1: 建立新索引index_user_20210123并初始化映射關系,映射也就是包含了修改後的字段類型;
step2: 使用 _reindex 将原索引index_user中的資料,導入到新索引index_user_20210123;
step3: aliases actions修改别名: 将index_user的别名index_user_latest移除,index_user_20210123新增别名index_user_latest