ToB企服应用市场:ToB评测及商务社交产业平台
标题:
es索引数据复制并增加条件和修改目标数据值
[打印本页]
作者:
尚未崩坏
时间:
2023-6-9 09:54
标题:
es索引数据复制并增加条件和修改目标数据值
es操作同一个索引里数据的复制语法
复制数据:
POST _reindex
{
"source": {
"index": "source_index"
},
"dest": {
"index": "destination_index"
}
}
复制代码
字段值修改:
POST source_index/_update_by_query
{
"script": {
"source": "ctx._source.field_name = 'new_value'"
},
"query": {
"match": {
"field_name": "old_value"
}
}
}
复制代码
可以通过在 source 中添加 query 来设置条件,只有满足条件的文档才会被复制到目标索引中。例如:
POST _reindex
{
"source": {
"index": "source_index",
"query": {
"match": {
"field_name": "value"
}
}
},
"dest": {
"index": "destination_index"
}
}
复制代码
上述代码将只复制 source_index 中 field_name 字段值为 value 的文档到 destination_index 中。
可以在复制数据时使用脚本来修改字段的值,将修改后的值写入目标索引中。例如:
POST _reindex
{
"source": {
"index": "source_index"
},
"dest": {
"index": "destination_index"
},
"script": {
"source": "ctx._source.field_name = 'new_value'"
}
}
复制代码
上述代码将复制 source_index 中的所有文档到 destination_index 中,并将其中的 field_name 字段值修改为 new_value。如果需要对特定的文档进行修改,可以在 source 中添加 query 条件来指定。
例:在同一个索引下复制并设置字段新值
POST _reindex
{
"source": {
"index": "source_index",
"query": {
"match": {
"field_name": "value"
}
}
},
"dest": {
"index": "source_index"
},
"script": {
"source": "ctx._source.field_name = 'new_value'"
}
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4