Elasticsearch怎么修改索引字段类型?
由于ElasticSearch没有像mysql一样可以直接字段数据类型的方法,因此需要通过创建中间索引:data_index_1,备份数据到中间索引:data_index_1,然后删除原索引: data_index,重新创建正确数据类型索引:data_index,再把中间索引:data_index_1的数据备份到新创建索引:data_index。语句通过kibana的 dev_tools/console 执行。
操作步骤如下:
复制代码
- 创建一个中间索引
- 向中间索引备份源索引的数据(mapping)
- 查询确认数据是否copy过去
- 删除有问题的索引
- 重新创建同名的索引(★字段类型修改正确★)
- 从中间索引还原到源索引的数据
- 删除中间索引
在kibana执行:
- 创建中间索引并指定mapping(字段类型)
PUT tmp_gd_whole_real_start_stop
{
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
},
"applied_time" : {
"type" : "date"
},
"capacity" : {
"type" : "long"
},
"crew" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"data_source" : {
"type" : "long"
},
"plant_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"total_elec" : {
"type" : "float"
},
"total_hours" : {
"type" : "float"
},
"total_load_ratio" : {
"type" : "float"
},
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
执行结果如下:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "tmp_gd_whole_real_start_stop"
}
- 向中间索引备份源索引的数据
POST _reindex
{
"source": {
"index": "gd_whole_real_start_stop"
},
"dest": {
"index": "tmp_gd_whole_real_start_stop"
}
}
执行结果如下:
{
"took" : 64,
"timed_out" : false,
"total" : 5,
"updated" : 0,
"created" : 5,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
- 查询确认数据是否copy过去
GET tmp_gd_whole_real_start_stop/_search
发现已经有了数据。
- 删除有问题的原索引
DELETE gd_whole_real_start_stop
执行结果如下:
{
"acknowledged" : true
}
- 重新创建同名的索引(★字段类型修改正确★)
PUT gd_whole_real_start_stop
{
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
},
"applied_time" : {
"type" : "date"
},
"capacity" : {
"type" : "long"
},
"crew" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"data_source" : {
"type" : "long"
},
"plant_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"total_elec" : {
"type" : "float"
},
"total_hours" : {
"type" : "float"
},
"total_load_ratio" : {
"type" : "float"
},
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
- 从中间索引还原到源索引的数据
POST _reindex
{
"source": {
"index": "tmp_gd_whole_real_start_stop"
},
"dest": {
"index": "gd_whole_real_start_stop"
}
}
- 删除中间索引
DELETE tmp_gd_whole_real_start_stop