lang110 发表于 2017-5-20 14:12:58

Elasticsearch索引重建(Rebuild)

  Elasticsearch索引重建(Rebuild)
索引重建(Rebuild)
         索引创建后,你可以在索引当中添加新的类型,在类型中添加新的字段。但是如果想修改已存在字段的属性(修改分词器、类型等),目前ES是做不到的。如果确实存在类似这样的需求,只能通过重建索引的方式来实现。但想要重建索引,请保证索引_source属性值为true,即存储原始数据。索引重建的过程就是将原来索引数据查询回来入到新建的索引当中去,为了重建过程不影响客户端查询,创建索引时请使用索引别名,例如现在需要将index1进行重建生成index2,index1给客户端提供的别名为index1_alias,完整步骤如下:
1、  创建索引索引index1,使用index1_alias作为别名指定。
 
 view plaincopy 



[*]curl –XPUT localhost:9200/index1 –d‘{  
[*]“aliases”:{  
[*]           “index1_alias”:{}  
[*]  }  
[*]}’  

  


 
2、  根据新配置创建索引index2
例如:
 
 view plaincopy 



[*]curl–XPUT localhost:9200/index2 –d ‘{  
[*]“settings”:{  
[*]           “index_number_of_shards”:10  
[*]}  
[*]}’  

  


 
3、  将旧索引数据导入到新索引中
将索引index1中的数据导入到index2当中,可以将index1原始数据划范围导入到index2中。为了提高查询性能,查询类型选择scan方式。例如:
部分1:
 
 view plaincopy 



[*] GET/ old_index / _search ? search_type = scan & scroll = 1m {  
[*]   "query": {  
[*]        "range": {  
[*]            "date": {  
[*]                "gte":"2014-01-01",  
[*]                "lt":"2014-02-01"  
[*]            }  
[*]        }  
[*]   },  
[*]   "size": 1000  
[*]}  

  


 
部分2:
 
 view plaincopy 



[*]GET/ old_index / _search ? search_type = scan & scroll = 1m {  
[*]   "query": {  
[*]        "range": {  
[*]            "date": {  
[*]                "gte": "2014-02-01",  
[*]                "lt": "2014-03-01"  
[*]            }  
[*]        }  
[*]   },  
[*]   "size": 1000  
[*]}  

  


 
使用上述方式查询原始数据,然后调用ES批量bulk接口索引文档。
4、  索引切换
为了保证系统不停机的情况下进行索引切换,通过修改别名的方式进行修改,即删除index1别名index1_alias,给index2增加index1_alias别名,具体如下:
 
 view plaincopy 



[*]curl –XPUT localhost:9200/_alias ‘{  
[*]“action”:[  
[*]           “remove:{  
[*]                    “index”:”index1”,  
[*]                    “alias”:”index1_aias”  
[*]}”,  
[*]           “add:{  
[*]                    “index”:”index2”,  
[*]                    “alias”:”index1_aias”  
[*]}”  
[*]   
[*]]  
[*]}’  

 
5、  删除旧索引
 
 view plaincopy 



[*]curl–XDELETE localhost:9200/index2  
  from http://blog.csdn.net/changong28/article/details/38491185
页: [1]
查看完整版本: Elasticsearch索引重建(Rebuild)