Elasticsearch不停机升级mapping(亲测)
索引重建(Rebuild)
索引创建后,你可以在索引当中添加新的类型,在类型中添加新的字段。但是如果想修改已存在字段的属性(修改分词器、类型等),目前ES是做不到的。如果确实存在类似这样的需求,只能通过重建索引的方式来实现。但想要重建索引,请保证索引_source属性值为true,即存储原始数据。索引重建的过程就是将原来索引数据查询回来入到新建的索引当中去,为了重建过程不影响客户端查询,创建索引时请使用索引别名,例如现在需要将index1进行重建生成index2,index1给客户端提供的别名为index1_alias,完整步骤如下:
1.为需要升级的索引指定别名
可以使用可视化head界面工具指定--很简单
这里别名起的作用其实就是升级后的别名保持和应用配置的type名是一致的,
如果是第一次升级,升级后的别名就是原来升级前的type名,如果不是第一次升级,最后的别名名称和之前别名名称是一样的
2.新建升级索引
2.1创建主索引
http://localhost:19201/es_test5_1/ PUT { "settings": { "index": { "creation_date": "1533786340419", "analysis": { "analyzer": { "ngram_analyzer": { "tokenizer": "ngram_tokenizer" } }, "tokenizer": { "ngram_tokenizer": { "token_chars": [ "digit", "letter", "symbol", "punctuation" ], "min_gram": "1", "type": "ngram", "max_gram": "1000" } } }, "number_of_shards": "5", "number_of_replicas": "1" } } }

2.2在该索引下创建type已及自定义mapping
http://localhost:19201/es_test5_1/_mapping/book PUT { "properties": { "id": { "index": "not_analyzed", "type": "string" }, "title": { "analyzer": "ngram_analyzer", "type": "string" }, "name": { "analyzer": "ngram_analyzer", "type": "string" }, "publish_data": { "type": "date" }, "price": { "type": "double" }, "number": { "type": "integer" } } }

3.将旧索引数据导入到新索引中
http://localhost:19201/_reindex POST { "source":{"index":"es_test5"}, "dest":{"index":"es_test5_1"} }

4.索引切换
http://localhost:19201/_aliases POST { "actions": [ { "remove": { "alias": "es_test5", "index": "es_test5_1" } }, { "add": { "alias": "es_test5", "index": "es_test5_2" } } ] }

5、 删除旧索引
可视化head页面操作即可
划船不用桨、杨帆不等风、一生全靠浪

浙公网安备 33010602011771号