ElasticSearch学习笔记 | Mapping映射的创建、修改和删除

本文测试数据为官方提供的测试数据,导入方法在学习笔记本章节第一篇中: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方法指定映射关系。

例如:

  1. 创建age为一个integer字段
  2. 创建email为一个keyword字段
  3. 创建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,

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员麻薯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值