es修改資料型別
阿新 • • 發佈:2018-12-25
環境:
es版本:6.5.0
es建立好了mapping後是不允許修改欄位型別的,要是我們想修改欄位型別怎麼辦呢,我們可以採用reindex的方法實現,就是建立一個新的mapping,裡面的欄位型別按照新的型別定義,然後使用reindex的方法把原來的資料拷貝到新的index下面。
1.檢視原來的mapping
[[email protected] ~]$ curl -u elastic:elastic -H "Content-Type: application/json" -XGET "http://192.168.1.85:9200/db_customer/_mappings?pretty=true" {"db_customer" : { "mappings" : { "tb_test" : { "properties" : { "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } } }
可以看到tb_test的欄位name為text型別,我想將其修改成keyword型別
2.建立新的index和mapping
建立一個新的index curl -u elastic:elastic -H 'Content-Type: application/json' -XPUT "http://192.168.1.85:9200/copy01_db_customer" 建立一個mapping curl -u elastic:elastic -H 'Content-Type: application/json' -XPOST "http://192.168.1.85:9200/copy01_db_customer/tb_test/_mapping?pretty" -d ' {"tb_test": { "properties": { "name": { "type": "keyword", "store": "true" } } } } '
這裡建立了一個新的index叫做opy01_db_customer,相應的tb_test mapping 欄位name 為keyword型別
3.資料同步
curl -u elastic:elastic -X POST "192.168.1.85:9200/_reindex" -H 'Content-Type: application/json' -d' { "source": { "index": "db_customer" }, "dest": { "index": "copy01_db_customer" } }'
4.再次檢視新index結構
[[email protected] ~]$ curl -u elastic:elastic -H "Content-Type: application/json" -XGET "http://192.168.1.85:9200/copy01_db_customer/_mappings?pretty=true" { "copy01_db_customer" : { "mappings" : { "tb_test" : { "properties" : { "name" : { "type" : "keyword", "store" : true } } } } } }
-- The End --