本文介紹Elasticsearch零宕機時間更新索引配置映射內容的方法,包括字段類型、分詞器、分片數等。方法原理就是,利用別名機制,給索引配置別名,全部應用程序都經過別名訪問索引。重建索引,經過索引原名將原索引導入新建索引。再爲新索引配置相同的別名。確認成功導入後,則刪掉老索引。實現配置參數更新。java
注意:app
如下全部操做都是基於一個前提:在建原始索引的時候,給原始索引建立了別名curl
PUT /my_index_v1 //建立索引 my_index_v1
PUT /my_index_v1/_alias/my_index //設置 my_index爲 my_index_v1url
1. 原始的索引bank,類型:account,mapping以下
{
"settings": {
"number_of_shards": 5
},
"mappings": {
"account": {
"properties": {
"balance": {
"type": "long"
},
"account_number": {
"type": "long"
},
"email": {
"type": "string"
},
"address": {
"type": "string"
},
"age": {
"type": "long"
},
"state": {
"type": "string"
},
"employer": {
"type": "string"
},
"lastname": {
"type": "string"
},
"gender": {
"type": "string"
},
"firstname": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
}
}
2.新建一個空的索引bak_bak,類型:account,,分片20,balance,account_number,age字段由long改爲了string類型,具備最新的、正確的配置
{
"settings": {
"number_of_shards": 20
},
"mappings": {
"account": {
"properties": {
"balance": {
"type": "string"
},
"account_number": {
"type": "string"
},
"email": {
"type": "string"
},
"address": {
"type": "string"
},
"age": {
"type": "string"
},
"state": {
"type": "string"
},
"employer": {
"type": "string"
},
"lastname": {
"type": "string"
},
"gender": {
"type": "string"
},
"firstname": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
}
}
3.使用java API遷移索引,主要配置參數以下:
String searchHost = "192.168.11.51";// 舊索引地址
int searchPort = 9300;// 舊索引端口
String searchIndexName = "bank";// 舊索引名字
String searchType = "account";// 舊索引類型
String newIndexName = "bak_bank";// 新索引名字
String newType = "account";// 新索引類型
// 過濾器根據實際狀況來定義,能夠有選擇性的導出數據,不定義filter的話,就是導出所有數據
String filter = "{ 'query' : {'query_string' : { 'query' : 'text:blup*'} } }"
.replaceAll("'", "\"");
String basicAuthCredentials = "base64_ifrequried=";
boolean withVersion = false;
final int hitsPerPage = 2000;//bulk批量操做的內存頁大小,通常1000-2500比較合適
float waitInSeconds = 0.0f;//索引線程休眠時間
// increase if you have lots of things to update
int keepTimeInMinutes = 90;
4.接下來修改alias別名的指向(若是你以前沒有用alias來改mapping,納尼就等着哭吧)
curl -XPOST localhost:8305/_aliases -d '
{
"actions": [
{ "remove": {
"alias": "mybank",
"index": "bank"
}},
{ "add": {
"alias": "mybank",
"index": "bak_bank"
}}
]
}
5.確認導入正常時,將老索引刪掉
curl -XDELETE localhost:8303/store_v1spa