Elasticsearch第四篇:索引别名、添加或修改映射规则
2024-08-29 11:26:33
项目中经常出现的问题,例如添加字段、修改字段,那原先的索引规则就要跟着改变,最好是一开始就给索引一个别名,修改字段时新增映射,然后将笔名指向新的映射,当然需要将之前的索引搬迁到新的映射当中。
1、获取映射信息(例如索引库是db_student)
GET http://localhost:/db_student/_mapping
这时可以看到返回结果:
{
"db_student": {
"mappings": {
"properties": {
"chinese": {
"type": "integer",
"store": true
},
"class": {
"type": "integer",
"store": true
},
"english": {
"type": "integer",
"store": true
},
"math": {
"type": "integer",
"store": true
},
"name": {
"type": "text",
"store": true
},
"school": {
"type": "text",
"store": true,
"analyzer": "ik_max_word"
}
}
}
}
}
这正是上一篇我们设置的映射规则,现在我们想要增加一列 desc ,用来保存学生的自我介绍介绍信息,首先,我们可以为 索引库 db_student 取一个别名 student 。
2、为旧索引库 db_student 起一个别名 student
PUT http://localhost:/db_student/_alias/student
此时,再查一下就索引库有哪些别名:
GET http://localhost:/db_student/_alias
呈现的效果是:
{
"db_student": {
"aliases": {
"student": {}
}
}
}
此时,别名已经生效。
3、建立新的索引库 db_student_v1,在原索引库的基础上加上 desc 这一新的字段,并且使用 ik 分词
PUT http://localhost:/db_student_v1
{
"mappings": {
"properties": {
"class": {
"type": "integer",
"store": true,
"index":true
},
"chinese": {
"type": "integer",
"store": true,
"index":true
},
"english": {
"type": "integer",
"store": true,
"index":true
},
"math": {
"type": "integer",
"store": true,
"index":true
},
"name": {
"type": "text",
"store": true,
"index":true
},
"school": {
"type": "text",
"store": true,
"index":true,
"analyzer":"ik_max_word"
},
"desc": {
"type": "text",
"store": true,
"index":true,
"analyzer":"ik_max_word"
}
}
}
}
4、数据搬迁,现在将索引库 db_student 的数据搬到新的索引库 db_student_v1
POST http://localhost:/_reindex
{
"conflicts": "proceed",
"source": {
"index": "db_student"
},
"dest": {
"index": "db_student_v1",
"op_type": "create",
"version_type": "external"
}
}
5、更改别名,将旧版索引库 db_student 的别名去除,将别名指向新版的索引库 db_student_v1
去除旧版索引库 db_student 的别名:
POST http://localhost:/_aliases
{
"actions": [
{
"remove": {
"index": "db_student",
"alias": "student"
}
}
]
}
为新版索引库 db_student_v1 加上别名:
POST http://localhost:/_aliases
{
"actions": [
{ "add": {
"index": "db_student_v1",
"alias": "student"
}
}
]
}
此时就可以统一按照别名 student 来进行上一篇的所有查询动作。
最新文章
- Mac OS 下 mysql 找不到 mysql.sock 的问题
- css属性在各种浏览器上的兼容性
- SQL Server存储过程多角度介绍
- Windows 上的 Jetty 小工具
- iOS - OC NSStream		文件流
- msf命令全集
- $key 的用法
- Linux 性能监控的18个命令行工具
- AVR抗干扰能力一般
- A Guide to the Multiboot Process
- hdu3306 Another kind of Fibonacci【矩阵快速幂】
- 前端project师,确定你的目标吧!无能的人才管他叫命运
- JAVA判断32位还是64位,调用不同的DLL(转)
- MongoDB数据库文档操作
- Tomcat 笔记-配置虚拟目录
- log4donet 的 一篇简单使用实例
- HttpUtility.UrlEncode()关于空格的编码问题
- vue刷新路由,不刷新页面
- Docker 系列七(Dubbo 微服务部署实践).
- ARGB与RGB、RGBA的区别