如何在Elasticsearch里面使用索引别名
- - ITeye博客在elasticsearch里面给index起一个aliases(别名)能非常优雅的解决两个索引无缝切换的问题,这个功能在某些场景下非常使用. 比如电商的核心商品索引库,除了实时增量数据外,每天都要重建一遍索引,避免index里面的数据和db里面的数据不一致,因为index分shard了,所以要一个一个的shard做全量替换,直到所有的shard替换完毕,才能宣布重建成功.
```` _alias 执行单个别名操作 _aliases 原子的执行多个别名操作 ````
```` PUT /my_index_v1 //构建索引 PUT /my_index_v1/_alias/my_index //给索引添加别名 ````
```` GET /*/_alias/my_index //查某个别名映射的所有index GET /my_index_v1/_alias/* //查询某个index拥有的别名 ````
```` { "my_index_v1" : { "aliases" : { "my_index" : { } } } } ````
```` PUT /my_index_v2 //构建索引 ````
```` POST /_aliases { "actions": [ { "remove": { "index": "my_index_v1", "alias": "my_index" }}, { "add": { "index": "my_index_v2", "alias": "my_index" }} ] } ````
```` client.admin().indices().prepareAliases().addAlias("my_index_v1","my_index"); ````
client.admin().indices().prepareAliases().removeAlias("my_index_v1","my_index"); ````
```` client.admin().indices().prepareAliases().removeAlias("my_index_v1","my_index") .addAlias("my_index_v2","my_index").execute().actionGet(); ````
```` SearchRequestBuilder search=client.prepareSearch("my_index"); ````