Reindex API
重新索引要求爲源索引中的所有文檔啓用_source
。
重新索引不會嘗試設置目標索引,它不會複製源索引的設置,你應該在運行_reindex
操作之前設置目標索引,包括設置映射、碎片數、副本等。
_reindex
的最基本形式只是將文檔從一個索引複製到另一個索引,這會將twitter
索引中的文檔複製到new_twitter
索引中:
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
這將返回如下內容:
{
"took" : 147,
"timed_out": false,
"created": 120,
"updated": 0,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"total": 120,
"failures" : [ ]
}
就像_update_by_query
一樣,_reindex
獲取源索引的快照,但其目標必須是不同的索引,因此版本衝突不太可能,dest
元素可以像索引API一樣配置,以控制樂觀併發控制。只是省略version_type
(如上所述)或將其設置爲internal
將導致Elasticsearch盲目地將文檔轉儲到目標中,覆蓋任何碰巧具有相同類型和id的文檔:
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "internal"
}
}
將version_type
設置爲external
將導致Elasticsearch保留源中的version
,創建缺少的任何文檔,並更新目標索引中具有舊版本的文檔而不是源索引中的任何文檔:
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "external"
}
}
設置op_type
爲create
將導致_reindex
僅在目標索引中創建缺少的文檔,所有現有文檔都會導致版本衝突:
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"op_type": "create"
}
}
默認情況下,版本衝突會中止_reindex
進程,但你可以在請求體中設置"conflicts": "proceed"
即可計算:
POST _reindex
{
"conflicts": "proceed",
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"op_type": "create"
}
}
你可以通過向source
添加類型或添加查詢來限制文檔,這個只會將kimchy
寫的推文複製到new_twitter
中:
POST _reindex
{
"source": {
"index": "twitter",
"type": "_doc",
"query": {
"term": {
"user": "kimchy"
}
}
},
"dest": {
"index": "new_twitter"
}
}