elasticsearch 更改已有字段的数据类型
【问题背景】:
在elasticsearch中,如果你没有指定字段映射,那么,elasticsearch将对为指定数据类型的字段做动态映射。
例如,当入库的前期数据字段result为数值型时,elasticsearch将其映射为long类型;当入库的后期数据字段result为字符串时,会报数据类型错误。
更改索引blog中的动态映射字段priority数据类型:由long更改为keyword
一:查看原有字段动态映射类型
查看mapping
命令:GET http://localhost:9002/index[索引名]/_mapping
只能创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping
{
"blog": {
"mappings": {
"article": {
"properties": {
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"counter": {
"type": "long"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"priority": {
"type": "long"
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
二:更改指定字段类型,新建索引blog_new
命令:PUT http://localhost:9002/blog_new
{
"mappings": {
"article": {
"properties": {
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"counter": {
"type": "long"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"priority": {
"type": "keyword"
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
三:将blog数据reindex到blog_new
命令:POST http://localhost:9002/_reindex
{
"source": {
"index": "blog"
},
"dest": {
"index": "blog_new"
}
}
四:删除索引blog
命令:DELETE http://localhost:9002/blog
五:将blog_new数据reindex到blog
命令:POST http://localhost:9002/_reindex
{
"source": {
"index": "blog_new"
},
"dest": {
"index": "blog"
}
}
六:删除索引blog_new
命令:DELETE http://localhost:9002/blog_new
本文介绍了在Elasticsearch中更改已有字段数据类型的方法。因未指定字段映射会导致动态映射出现数据类型错误,以更改索引blog中priority字段类型为例,详细说明了查看原有映射、新建索引、数据迁移、删除旧索引等步骤及对应命令。

2万+

被折叠的 条评论
为什么被折叠?



