es索引数据复制并增加条件和修改目标数据值
  p8CEK2jy4Fiz 2023年11月01日 109 0

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'"
  }
}

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读