question. 一个原始索引,一个A索引,一个B索引,数据在原始索引,A与B索引采用一个别名,将原始索引数据复制到该别名,A、B索引下是否都有全量数据?
要点1:能否通过别名对应多个索引,将原始数据复制给别名代表的多个索引?
要点2:如何从旧索引复制数据到新索引?
有博客说:不能对有多个索引的别名进行写操作,当有多个索引时alias,不能区分到底操作哪一个。
下面进行验证与尝试
测试环境elastic search2.2
1.创建原始索引
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
2.创建两个新索引
PUT test1
PUT test2
3.创建test1、test2的别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "test1",
"alias": "my_index_alias"
}
},
{
"add": {
"index": "test2",
"alias": "my_index_alias"
}
}
]
}
4. 重索引 _reindex,将原始索引中数据复制给别名,参考官方文档:ES 5.1 ReindexAPI
POST _reindex
{
"source": {
"index": "cars"
},
"dest": {
"index": "my_index_alias"
}
}
结果
{
"error": "NullPointerException[null]",
"status": 500
}
结论
首先,可能2.2不支持该操作,其次,此api处于实验阶段可靠性低,下面将尝试用其他方式进行步骤4的重索引工作。
利用bulk操作将原始索引中的数据PUT到别名代表的两个索引中:
POST /my_index_alias/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
结果:
{
"error": "ElasticsearchIllegalArgumentException[Alias [my_index_alias] has more than one indices associated with it [[test1, test2]], can't execute a single index op]",
"status": 400
}
此外,不采用bulk操作而只单个的进行POST操作,也存在相同的报错。
POST my_index_alias/transactions
{ "index": {}}
{ "price" : 25001, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
将别名对应一个索引进行上述操作,成功将数据复制到了别名对应的索引。通过报错可知该操作只能针对单个索引进行。
在Elasticsearch所有的API中,对应的是一个或者多个索引。Elasticsearch可以对一个或者多个索引指定别名,通过别名可以查询到一个或者多个索引的内容,在内部,Elasticsearch会自动把别名映射到响应的索引上。可以对别名编写过滤器或者路由,在系统中别名不能重复,也不能和索引名重复。其实在Elasticsearch的别名机制有点像数据库中的视图。
结论:查询操作允许针对多个索引,增删改操作不能对应多索引,无法针对别名进行多索引的增删改。