es不支持直接修改字段类型,解决思路:
- 新建临时索引,执行字段类型,复制数据
- 删除旧索引,重建同名索引,从临时索引复制数据
举例:
我这有个学生的索引:
{
"properties": {
"name": {
"type": "text",
"index": true
},
"nickname": {
"type": "text",
"index": true
},
"sex": {
"type": "text",
"index": false
},
"age": {
"type": "long",
"index": false
}
}
}
在 Postman 中,向 ES 服务器发 GET 请求 :http://127.0.0.1:9200/student/_mapping
查看索引
服务器响应结果:
我想要把sex
、age
的index设置为true
1、获取旧索引的字段映射
GET /users/_mapping
2、创建临时索引带映射
PUT: http://127.0.0.1:9200/student_temp
服务器响应结果:
然后正确的创建映射
PUT: http://127.0.0.1:9200/student_temp/_mapping
{
"properties": {
"name": {
"type": "text",
"index": true
},
"nickname": {
"type": "text",
"index": true
},
"sex": {
"type": "text",
"index": true
},
"age": {
"type": "long",
"index": true
}
}
}
响应结果:
3、复制数据
POST: http://127.0.0.1:9200/_reindex
{
"source": {
"index": "student"
},
"dest": {
"index": "student_temp"
}
}
4、删除旧索引
DELETE: http://127.0.0.1:9200/student
5、创建新的正确的索引带映射
- PUT:
http://127.0.0.1:9200/student
- PUT:
http://127.0.0.1:9200/student_/_mapping
{
"properties": {
"name": {
"type": "text",
"index": true
},
"nickname": {
"type": "text",
"index": true
},
"sex": {
"type": "text",
"index": true
},
"age": {
"type": "long",
"index": true
}
}
}
6、复制数据
POST http://127.0.0.1:9200/_reindex
查看数据:
Get请求:http://127.0.0.1:9200/student/_search
{
"query": {
"match_all": {}
}
}
7、删除临时索引就欧克啦
DELETE请求: http://127.0.0.1:9200/student_temp