Elasticsearch索引重建(Rebuild)

Elasticsearch索引重建(Rebuild)

索引重建(Rebuild)

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

1、  创建索引索引index1,使用index1_alias作为别名指定。

 

[html]  view plain copy
 
  1. curl –XPUT localhost:9200/index1 –d‘{  
  2. “aliases”:{  
  3.            “index1_alias”:{}  
  4.   }  
  5. }’  



 

2、  根据新配置创建索引index2

例如:

 

[html]  view plain copy
 
  1. curl–XPUT localhost:9200/index2 –d ‘{  
  2. “settings”:{  
  3.            “index_number_of_shards”:10  
  4. }  
  5. }’  



 

3、  将旧索引数据导入到新索引中

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

部分1:

 

[html]  view plain copy
 
  1.  GET/ old_index / _search ? search_type = scan & scroll = 1m {  
  2.    "query": {  
  3.         "range": {  
  4.             "date": {  
  5.                 "gte":"2014-01-01",  
  6.                 "lt":"2014-02-01"  
  7.             }  
  8.         }  
  9.    },  
  10.    "size": 1000  
  11. }  



 

部分2:

 

[html]  view plain copy
 
  1. GET/ old_index / _search ? search_type = scan & scroll = 1m {  
  2.    "query": {  
  3.         "range": {  
  4.             "date": {  
  5.                 "gte": "2014-02-01",  
  6.                 "lt": "2014-03-01"  
  7.             }  
  8.         }  
  9.    },  
  10.    "size": 1000  
  11. }  



 

使用上述方式查询原始数据,然后调用ES批量bulk接口索引文档。

4、  索引切换

为了保证系统不停机的情况下进行索引切换,通过修改别名的方式进行修改,即删除index1别名index1_alias,给index2增加index1_alias别名,具体如下:

 

[html]  view plain copy
 
  1. curl –XPUT localhost:9200/_alias ‘{  
  2. “action”:[  
  3.            “remove:{  
  4.                     “index”:”index1”,  
  5.                     “alias”:”index1_aias”  
  6. }”,  
  7.            “add:{  
  8.                     “index”:”index2”,  
  9.                     “alias”:”index1_aias”  
  10. }”  
  11.    
  12. ]  
  13. }’  

 

5、  删除旧索引

 

[html]  view plain copy
 
  1. curl–XDELETE localhost:9200/index2  

from http://blog.csdn.net/changong28/article/details/38491185

猜你喜欢

转载自aoyouzi.iteye.com/blog/2144726
今日推荐