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

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/sensorsen/p/17468193.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!