1.爲何要重建索引?java
總結,一個type下的mapping中的filed不能被修改,因此若是須要修改,則須要重建索引api
2.怎麼zero time重建索引?多線程
一個field的設置是不能被修改的,若是要修改一個Field,那麼應該從新按照新的mapping,創建一個index,而後將數據批量查詢出來,從新用bulk api寫入index中併發
批量查詢的時候,建議採用scroll api,而且採用多線程併發的方式來reindex數據,每次scoll就查詢指定日期的一段數據,交給一個線程便可。app
過程:spa
(1)一開始,依靠dynamic mapping,插入數據,可是不當心有些數據是2017-01-01這種日期格式的,因此title這種field被自動映射爲了date類型,實際上它應該是string類型的線程
PUT /my_index/my_type/3 { "title": "2017-01-03" }
查看mapping:code
{ "my_index": { "mappings": { "my_type": { "properties": { "title": { "type": "date" } } } } } }
(2)當後期向索引中加入string類型的title值的時候,就會報錯orm
PUT /my_index/my_type/4 { "title": "my first article" }
{ "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "failed to parse [title]" } ], "type": "mapper_parsing_exception", "reason": "failed to parse [title]", "caused_by": { "type": "illegal_argument_exception", "reason": "Invalid format: \"my first article\"" } }, "status": 400 }
(3)若是此時想修改title的類型,是不可能的blog
PUT /my_index/_mapping/my_type { "properties": { "title": { "type": "text" } } } { "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "mapper [title] of different type, current_type [date], merged_type [text]" } ], "type": "illegal_argument_exception", "reason": "mapper [title] of different type, current_type [date], merged_type [text]" }, "status": 400 }
(4)此時,惟一的辦法,就是進行reindex,也就是說,從新創建一個索引,將舊索引的數據查詢出來,再導入新索引
(5)若是說舊索引的名字,是old_index,新索引的名字是new_index,終端java應用,已經在使用old_index在操做了,難道還要去中止java應用,修改使用的index爲new_index,才從新啓動java應用嗎?這個過程當中,就會致使java應用停機,可用性下降。
(6)因此說,給java應用一個別名,這個別名是指向舊索引的,java應用先用着,java應用先用goods_index alias來操做,此時實際指向的是舊的my_index
PUT /my_index/_alias/goods_index
(7)新建一個index,調整其title的類型爲string
PUT /my_index_new { "mappings": { "my_type": { "properties": { "title": { "type": "text" } } } } }
(8)使用scroll api將數據批量查詢出來
GET /my_index/_search?scroll=1m { "query": { "match_all": {} }, "sort": ["_doc"], "size": 1 }
{ "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAADpAFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAA6QRY0b25zVFlWWlRqR3ZJajlfc3BXejJ3AAAAAAAAOkIWNG9uc1RZVlpUakd2SWo5X3NwV3oydwAAAAAAADpDFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAA6RBY0b25zVFlWWlRqR3ZJajlfc3BXejJ3", "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": null, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "2", "_score": null, "_source": { "title": "2017-01-02" }, "sort": [ 0 ] } ] } }
(9)採用bulk api將scoll查出來的一批數據,批量寫入新索引
POST /_bulk { "index": { "_index": "my_index_new", "_type": "my_type", "_id": "2" }} { "title": "2017-01-02" }
(10)反覆循環8~9,查詢一批又一批的數據出來,採起bulk api將每一批數據批量寫入新索引
(11)將goods_index alias切換到my_index_new上去,java應用會直接經過index別名使用新的索引中的數據,java應用程序不須要停機,零停機,高可用
POST /_aliases { "actions": [ { "remove": { "index": "my_index", "alias": "goods_index" }}, { "add": { "index": "my_index_new", "alias": "goods_index" }} ] }
(12)直接經過goods_index別名來查詢,是否ok
GET /goods_index/my_type/_search
三、基於alias對client透明切換index
PUT /my_index_v1/_alias/my_index
client對my_index進行操做
reindex操做,完成以後,切換v1到v2
POST /_aliases { "actions": [ { "remove": { "index": "my_index_v1", "alias": "my_index" }}, { "add": { "index": "my_index_v2", "alias": "my_index" }} ] }