ElasticSearch 映射
官方文档: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/mapping.html
上文说过了ElasticSearch的基本使用,本文来说说ElasticSearch的映射
映射是定义文档及其包含的字段如何存储和索引的过程
文档是字段的集合,一开始,我们都没为文档的字段设置类型,但是ES会自动为它决定类型
字段数据类型
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/mapping-types.html

查看映射
未来ES可能会删除类型
查看映射就是查看索引下映射
GET /bank/_mapping

创建映射
创建新索引my-index并指定映射
PUT /my-index
{
"mappings": {
"properties": {
"age" : {
"type": "integer"
},
"name" : {
"type": "text"
},
"email" : {
"type" : "keyword"
}
}
}
}

添加新的映射
PUT /my-index/_mapping
{
"properties": {
"newMapping" :{
"type" : "keyword",
"index" : false
}
}
}

index为false 表示该字段不能被检索
修改映射与数据迁移
规定好映射后不允许修改映射
因为修改映射规则可能对数据有影响
所以只能重新创建索引规定好映射再进行数据迁移
创建新索引
PUT /newbank
{
"mappings": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text"
},
"age": {
"type": "integer"
},
"balance": {
"type": "long"
},
"city": {
"type": "text"
},
"email": {
"type": "keyword"
},
"employer": {
"type": "text"
},
"firstname": {
"type": "text"
},
"gender": {
"type": "text"
},
"lastname": {
"type": "text"
},
"state": {
"type": "text"
}
}
}
}
数据传输
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index" : "newbank"
}
}

查看

未使用类型后,类型默认为_doc
下篇文章将简单介绍ElasticSearch分词
本文介绍了ElasticSearch的映射概念,它是定义文档字段存储和索引方式的过程。映射包括字段数据类型,如integer、text和keyword。可以通过GET请求查看映射,PUT请求创建或更新映射。创建新索引并定义映射后,数据迁移通常涉及使用_reindex API。由于映射修改可能影响数据,因此建议先规划好映射再进行数据导入。
1013





