バージョン
Elasticsearch 6.4
手順
再作成したいindex(ここではold)のmappingを取得
pythonを導入している場合は、パイプラインを使って結果を整形
curl -XGET "http://localhost:9200/old/_mapping" | python -m json.tool > old.json
old.jsonファイルを編集し、index名を削除しておく。
そうしないと次のコマンドを実行すると、index名がnew.oldになってしまう。
indexを再作成(ここではnew)、このときfieldの名称などを変更したい場合は、
old.jsonファイルを編集しておく
curl -XPUT "http://localhost:9200/new" --data-binary @old.json
oldからnewにデータをコピー。このときfield名を変更している場合は、
scriptを使ってfield名を置換しながらコピー。
複数のfieldを置換する場合は ; を使って区切る。
curl -XPOST localhost:9200/_reindex?pretty -d'{
"source": {
"index": "old"
},
"dest": {
"index": "new"
},
"script": {
"source": "ctx._source.newのfield名 = ctx._source.remove(\"oldのfield名\"); ctx._source.newのfield名 = ctx._source.remove(\"oldのfield名\")"
}
}'
参考
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html
http://mmzk512.hatenablog.com/entry/2017/09/19/163225