es7.+(三)--mapping
目录
1.mapping
用来定义一个文档如何被处理的,这些属性字段是怎么被存储和被检索的
- 使用哪个mapping去定义哪个String字段应该被当做全文检索字段
- 哪一个字段包含数值、日期或者地理位置坐标
- 文档中的所有属性是否都能被索引(_all配置)
- 日期的格式化数据
- 自定义映射规则来执行动态添加属性
es会自动的为数据定义mapping,但有的时候需要自定义
1.1查询已经存有数据的索引index的信息
GET /bank/_mapping
返回结果
复制{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
...
1.2如何自定义映射
- 创建一个索引
PUT /my_index
- 在新索引保存数据之前,可以先创建映射规则
{
"mappings": {
"properties": {
"age":{
"type": "integer"
},
"email":{
"type": "keyword"
},
"name":{
"type": "text"
}
}
}
}
返回内容
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my_index"
}
1.3添加新的字段映射
比如我们需要新增一个字段,名字是 employee-id
该index选项控制是否对字段值建立索引。它接受true 或false,默认为true。未索引的字段不可查询
index选项控制这个属性的值能不能被索引,为true或false,默认是true。如果一个字段的index为false那么他就不会被检索到,他是来控制这个字段是不是参与检索的
PUT /my_index/_mapping
{
"properties":{
"employee-id":{
"type": "keyword",
"index": false
}
}
}
1.4修改映射数据迁移
对于已经存在的映射字段,我们不能更新,更新必须创建新的索引进行数据迁移。
我们先来创建新的映射关系,稍后我们将进行数据迁移
PUT /newbank
{
"mappings": {
"properties": {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text"
},
"age" : {
"type" : "integer"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "keyword"
},
"email" : {
"type" : "keyword"
},
"employer" : {
"type" : "keyword"
},
"firstname" : {
"type" : "text"
},
"gender" : {
"type" : "keyword"
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "keyword"
}
}
}
}
进行数据迁移
- 7.+版本
POST _reindex
{
"source":{
"index": "bank"
},
"dest":{
"index": "newbank"
}
}
- 7.+版本之前(还使用type的版本)
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 一天 Star 破万的开源项目「GitHub 热点速览」
· 瞧瞧别人家的日期处理,那叫一个优雅!
· 使用TypeScript开发微信小程序(云开发)-入门篇
· 没几个人需要了解的JDK知识,我却花了3天时间研究
· 定时任务稳定性解决方案-healthchecks监控系统