Elasticsearch中怎么对mapping进行修改,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
index:test_v1
type:item
alias:item_alias
mapping:
{
"properties": {
"itemId": {
"type": "long"
},
"itemName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
使用最新的mapping直接调用putMapping接口
PUT http://xxx.xxx.xxx.xxx:9200/goods_info/_mapping
{
"properties": {
"itemId": {
"type": "long"
},
"itemName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"brandName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
由于Elasticsearch底层使用了lucene的原因,不支持对mapping的修改,可使用索引重建的方式,步骤如下:
1,使用正确的mapping新建索引和类型
如需要将旧索引的itemId字段改为keyword类型,则执行以下请求:
创建index:
PUT /test_v2
设置mapping:
POST /test_v2/item/_mapping
{
"properties": {
"itemId": {
"type": "keyword"
},
"itemName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
2,使用reindex api将旧索引数据导入新索引
索引重建后可使用reindex命令迁移数据,如将test_v1数据迁移至test_v2请求如下:
POST _reindex
{
"source": {
"index": "test_v1",
"type": "item"
},
"dest": {
"index": "test_v2",
"type": "item"
}
}
3,为新索引添加别名
为索引添加别名后,在程序代码中可以使用固定别名查询动态的索引名称,然后进行查询,如此索引重建则不会引起程序的变动
添加别名请求:
POST /_aliases
{
"actions": [
{ "add": {
"alias": "item_alias",
"index": "test_v2"
}}
]
}
将旧索引别名迁移到新索引请求:
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test_v1", "alias" : "item_alias" } },
{ "add" : { "index" : "test_v2", "alias" : "item_alias" } }
]
}
看完上述内容,你们掌握Elasticsearch中怎么对mapping进行修改的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3777515/blog/5036447