阳小猿的博客通过本文主要向大家介绍了elasticsearch等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
    Elasticsearch 的 mapping 在创建 indices 时即已确定,无法更改。那么,当我们需要更新 mapping 时,该如何是好呢?
基本思路
当我们在创建一条索引时,添加好 mapping 后,可设置一个
alias指向该索引,然后生产环境采用该alias来索引数据。当然,如果没有这样做的话,建议趁早备份,修改 API 。
既然已创建的 indices 无法修改,我们可以重新创建一个新的 indices, 然后将原 indices 上的数据复制到新的 indices 上,再将 alias 指向新 indices。最后,删除原索引。
参数说明
- 当前索引名称: 
test_v1 - 生产索引名称:
test - 目标索引名称: 
test_v2 
操作步骤
- 
	
将生产索引指向当前索引:
test->test_v1- Method: POST
 - Url: http://localhost:9200/_aliases
 - 
		
Body:
{ "actions": [ { "add" : { "index" : "test_v1", "alias" : "test" } } ] } 
 - 
	
创建新索引
test_v2- Method: PUT
 - Url: http://localhost:9200/test_v2
 - 
		
Body:
{ "mappings": { "content": { "properties": { "title": { "type": "text", "fields": { "accurate": { "type": "keyword" } }, "analyzer": "ik_smart", "search_analyzer": "ik_smart", "include_in_all": "true" }, "content": { "type": "text", "analyzer": "ik_smart", "search_analyzer": "ik_smart", "include_in_all": "true" }, "author": { "type": "keyword" }, "category": { "type": "keyword" } } } } } 
 - 
	
复制数据:
test_v1->test_v2- Method: POST
 - Url: http://localhost:9200/_reindex
 - 
		
Body:
{ "source": { "index": "test_v1" }, "dest": { "index": "test_v2" } } 
 - 
	
修改别名:
test->test_v2- Method: POST
 - Url: http://localhost:9200/_aliases
 - 
		
Body:
{ "actions": [ { "remove" : { "index" : "test_v1", "alias" : "test" } }, { "add" : { "index" : "test_v2", "alias" : "test" } } ] } 
 - 删除旧索引: 
test_v1- Method: DELETE
 - Url: http://localhost:9200/test_v1
 
 
小结
至此,我们达到了伪更新的效果。不过这里存在一个问题,如果数据量超大的话,复制数据所消费的时间比较多,所以请在构建索引前尽量考虑周全。

