Elasticsearch 参考指南(Reindex API)

3次阅读

共计 1466 个字符,预计需要花费 4 分钟才能阅读完成。

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”
}
}

正文完
 0