Elasticsearch模块功能之-索引别名(Index aliases )

Elasticsearch模块功能之-索引别名(Index aliases )

Elasticsearch的API支持给索引起别名,有了别名之后可以像使用索引一样使用它。但不只是这些,一个别名可以映射多个索引,所以在需要经常指定多个索引查询的情况下,大可将所查询的索引起一个别名来查。别名也可以将索引查询的过滤条件包含在内,使用别名查询时可以查询索引的一个子集。

         创建一个别名

 

[html]  view plain copy
 
  1. curl -XPOST 'http://localhost:9200/_aliases'-d '  
  2. {  
  3.     "actions" : [  
  4.         { "add" : { "index" : "test1","alias" : "alias1" } }  
  5.     ]  
  6. }'  

 

上述将创建一个指向索引test1的别名alias1。

         删除别名:

 

[html]  view plain copy
 
  1. curl -XPOST 'http://localhost:9200/_aliases'-d '  
  2. {  
  3.     "actions" : [  
  4.         { "remove" : { "index" : "test1","alias" : "alias1" } }  
  5.     ]  
  6. }'  

上述将删除别名alias1.

 

         重命名别名:

[html] view plain copy
 
  1. curl -XPOST 'http://localhost:9200/_aliases' -d '  
  2. {  
  3.     "actions" : [  
  4.         { "remove" : { "index" : "test1", "alias" : "alias1" } },  
  5.         { "add" : { "index" : "test1", "alias" : "alias2" } }  
  6.     ]  
  7. }'  

 

创建映射多个索引的别名:

[html]  view plain copy
 
  1. curl -XPOST 'http://localhost:9200/_aliases'-d '  
  2. {  
  3.     "actions" : [  
  4.         { "add" : { "index" : "test1","alias" : "alias1" } },  
  5.         { "add" : { "index" : "test2","alias" : "alias1" } }  
  6.     ]  
  7. }'  

上述创建的别名alias1映射索引test1和索引test2。

 

创建索引在索引创建期间:

 

[html]  view plain copy
 
  1. curl -XPUT localhost:9200/logs_20142801 -d '{  
  2.     "aliases" : {  
  3.         "current_day" : {},  
  4.         "2014" : {  
  5.             "filter" : {  
  6.                 "term" : {"year": 2014 }  
  7.             }  
  8.         }  
  9.     }  
  10. }'  

 

查看索引是所有别名:

 

[html]  view plain copy
 
  1. curl -XGET 'localhost:9200/users/_alias/*<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>  

 

查看具体别名:

 

[html]  view plain copy
 
  1. curl -XGET 'localhost:9200/_alias/2013'  

 

包含过滤条件的别名:

 

[html]  view plain copy
 
  1. curl -XPOST 'http://localhost:9200/_aliases' -d '  
  2. {  
  3.     "actions" : [  
  4.         {  
  5.             "add" : {  
  6.                  "index" : "test1",  
  7.                  "alias" : "alias2",  
  8.                  "filter" : { "term" : { "user" : "kimchy" } }  
  9.             }  
  10.         }  
  11.     ]  
  12. }'  

 

上述的别名将映射到索引test1使用过滤条件之后的结果,类似数据库的视图。

 

【参考】:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html#indices-aliases

 

 

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

猜你喜欢

转载自aoyouzi.iteye.com/blog/2144724