重建索引要求为源索引中的全部文档启用
_source
。
重建索引不会尝试设置目标索引,它不会复制源索引的设置,你应该在运行
_reindex
操做以前设置目标索引,包括设置映射、碎片数、副本等。
_reindex
的最基本形式只是将文档从一个索引复制到另外一个索引,这会将twitter
索引中的文档复制到new_twitter
索引中:并发
POST _reindex { "source": { "index": "twitter" }, "dest": { "index": "new_twitter" } }
这将返回以下内容:oop
{ "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
的文档:code
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
中:requests
POST _reindex { "source": { "index": "twitter", "type": "_doc", "query": { "term": { "user": "kimchy" } } }, "dest": { "index": "new_twitter" } }