Elasticsearch的API支持给索引起别名,有了别名之后可以像使用索引一样使用它。但不只是这些,一个别名可以映射多个索引,所以在需要经常指定多个索引查询的情况下,大可将所查询的索引起一个别名来查。别名也可以将索引查询的过滤条件包含在内,使用别名查询时可以查询索引的一个子集。
创建一个别名:
curl -XPOST 'http://localhost:9200/_aliases'-d '
{
"actions" : [
{ "add" : { "index" : "test1","alias" : "alias1" } }
]
}'
上述将创建一个指向索引test1的别名alias1。
删除别名:
curl -XPOST 'http://localhost:9200/_aliases'-d '
{
"actions" : [
{ "remove" : { "index" : "test1","alias" : "alias1" } }
]
}'
上述将删除别名alias1.
重命名别名:
curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test1", "alias" : "alias2" } } ] }'
创建映射多个索引的别名:
curl -XPOST 'http://localhost:9200/_aliases'-d '
{
"actions" : [
{ "add" : { "index" : "test1","alias" : "alias1" } },
{ "add" : { "index" : "test2","alias" : "alias1" } }
]
}'
上述创建的别名alias1映射索引test1和索引test2。
创建索引在索引创建期间:
curl -XPUT localhost:9200/logs_20142801 -d '{
"aliases" : {
"current_day" : {},
"2014" : {
"filter" : {
"term" : {"year": 2014 }
}
}
}
}'
查看索引是所有别名:
curl -XGET 'localhost:9200/users/_alias/*<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
查看具体别名:
curl -XGET 'localhost:9200/_alias/2013'
包含过滤条件的别名:
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "test1",
"alias" : "alias2",
"filter" : { "term" : { "user" : "kimchy" } }
}
}
]
}'
上述的别名将映射到索引test1使用过滤条件之后的结果,类似数据库的视图。