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
}
es5.1reindex API提示
結論
首先,可能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會自動把別名映射到響應的索引上??梢詫e名編寫過濾器或者路由,在系統中別名不能重復,也不能和索引名重復。其實在Elasticsearch的別名機制有點像數據庫中的視圖。
結論:查詢操作允許針對多個索引,增刪改操作不能對應多索引,無法針對別名進行多索引的增刪改。