es索引数据复制并增加条件和修改目标数据值

打印 上一主题 下一主题

主题 919|帖子 919|积分 2757

es操作同一个索引里数据的复制语法
复制数据:
 
  1. POST _reindex
  2. {
  3.   "source": {
  4.     "index": "source_index"
  5.   },
  6.   "dest": {
  7.     "index": "destination_index"
  8.   }
  9. }
复制代码
 
字段值修改:
  1. POST source_index/_update_by_query
  2. {
  3.   "script": {
  4.     "source": "ctx._source.field_name = 'new_value'"
  5.   },
  6.   "query": {
  7.     "match": {
  8.       "field_name": "old_value"
  9.     }
  10.   }
  11. }
复制代码
可以通过在 source 中添加 query 来设置条件,只有满足条件的文档才会被复制到目标索引中。例如:
  1. POST _reindex
  2. {
  3.   "source": {
  4.     "index": "source_index",
  5.     "query": {
  6.       "match": {
  7.         "field_name": "value"
  8.       }
  9.     }
  10.   },
  11.   "dest": {
  12.     "index": "destination_index"
  13.   }
  14. }
复制代码
上述代码将只复制 source_index 中 field_name 字段值为 value 的文档到 destination_index 中。
可以在复制数据时使用脚本来修改字段的值,将修改后的值写入目标索引中。例如:
  1. POST _reindex
  2. {
  3.   "source": {
  4.     "index": "source_index"
  5.   },
  6.   "dest": {
  7.     "index": "destination_index"
  8.   },
  9.   "script": {
  10.     "source": "ctx._source.field_name = 'new_value'"
  11.   }
  12. }
复制代码
上述代码将复制 source_index 中的所有文档到 destination_index 中,并将其中的 field_name 字段值修改为 new_value。如果需要对特定的文档进行修改,可以在 source 中添加 query 条件来指定。
例:在同一个索引下复制并设置字段新值
  1. POST _reindex
  2. {
  3.   "source": {
  4.     "index": "source_index",
  5.     "query": {
  6.       "match": {
  7.         "field_name": "value"
  8.       }
  9.     }
  10.   },
  11.   "dest": {
  12.     "index": "source_index"
  13.   },
  14.   "script": {
  15.     "source": "ctx._source.field_name = 'new_value'"
  16.   }
  17. }
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

尚未崩坏

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表