LoginSignup
1
1

More than 5 years have passed since last update.

Elasticsearchでindexを再作成しながらfieldを変更する手順

Posted at

バージョン

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1