Elasticsearch教程(18) reindex 重建索引,非常实用的功能

reindex功能是es的一个非常方便实用的功能,在工作中备份数据比较方便。比如:

注意:

reindex不会复制index的设置,所以得先设置好目标索引的setting和mapping,然后再index。

源index的文档id和目标index的文档id一样时,默认会覆盖,当然可手动配置冲突时行为

source里是源index,dest里是目标索引

POST _reindex { "source": { "index": "pigg_test" }, "dest": { "index": "pigg_test2" } } 
POST _reindex { "source": { "index": "pigg_test" }, "dest": { "index": "pigg_test2", "op_type": "create" } } 

reindex底层是scroll,默认批次是1000条,可以设置多点

POST _reindex { "source": { "index": "pigg_test", "size": 5000 }, "dest": { "index": "pigg_test2" } } 
POST _reindex { "conflicts": "proceed", "source": { "index": "pigg_test" }, "dest": "index": "pigg_test2", "op_type": "create" } } 
POST _reindex { "source": { "index": "pigg_test", "query": { "term": { "name.keyword": { "value": "冬哥" } } } }, "dest": { "index": "pigg_test2" } } 
POST _reindex { "source": { "index": "pigg_test", "_source": ["name", "age"] }, "dest": { "index": "pigg_test2" } } 
POST _reindex { "source": { "index": "pigg_test", "_source": { "excludes": ["name"] } }, "dest": { "index": "pigg_test2" } } 

es支持的script非常强大,这个不详细讲,仅仅举个简单的例子

POST _reindex { "source": { "index": "pigg_test" }, "dest": { "index": "pigg_test2" }, "script": { "source": "ctx._source.age += 2", "lang": "painless" } } 

同样是用script,讲name属性重命名为newName

POST _reindex { "source": { "index": "pigg_test" }, "dest": { "index": "pigg_test2" }, "script": { "source": "ctx._source.newName = ctx._source.remove(\"name\")", "lang": "painless" } } 
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
文明发言,共建和谐米科社区
提交
头像

昵称

取消
昵称表情图片