本文测试数据为官方提供的测试数据,导入方法在学习笔记本章节第一篇中:https://blog.csdn.net/qq_20051535/article/details/113242821
一、查询映射
映射是定义文档及其包含的字段的存储和索引方式的过程。例如,使用映射定义:
哪些字符串字段应视为全文字段。
哪些字段包含数字,日期或地理位置。
日期值 的格式。
自定义规则,用于控制动态添加字段的映射 。
查询bank的映射信息
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"
},
...
二、新建映射
我们可以利用put方法指定映射关系。
例如:
创建age为一个integer字段
创建email为一个keyword字段
创建name为一个text字段
PUT /my_index
{
"mappings": {
"properties": {
"age":{
"type": "integer"
},
"email":{
"type": "keyword"
},
"name":{
"type": "text"
}
}
}
}
返回内容
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my_index"
}
三、添加新的字段映射
比如我们需要新增一个字段,名字是 employee-id
该index选项控制是否对字段值建立索引。它接受true 或false,默认为true。未索引的字段不可查询
PUT /my_index/_mapping
{
"properties":{
"employee-id":{
"type": "keyword",
"index": false
}
}
}
返回结果:
{
"acknowledged" : true
}
四、更改映射
对于已经存在的映射字段,我们不能更新,更新必须创建新的索引进行数据迁移。
我们先来创建新的映射关系,稍后我们将进行数据迁移
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"
}
}
}
}
五、数据迁移
先创建新的映射,然后使用如下的方式进行迁移
新版本不包含Type属性的迁移方式:
POST _reindex
{
"source":{
"index": "twitter"
},
"dest":{
"index": "new_twitter"
}
}
将旧索引的type下的数据进行迁移
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}
返回结果,显示迁移成功
#! Deprecation: [types removal] Specifying types in reindex requests is deprecated.
{
"took" : 529,
"timed_out" : false,
"total" : 1000,
"updated" : 0,
"created" : 1000,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
再次查询确认迁移数据成功
GET /newbank/_search
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "newbank",
"_type" : "_doc", //type都变成了_doc,8.0将彻底取消type属性
"_id" : "1",
"_score" : 1.0,
————————————————
版权声明:本文为CSDN博主「北鹤M」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_20051535/article/details/113250151