ElasticSearch学习笔记 | Mapping映射的创建、修改和删除
发布日期:2021-07-28 08:29:48 浏览次数:2 分类:技术文章

本文共 2720 字,大约阅读时间需要 9 分钟。

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

转载地址:https://blog.csdn.net/sinat_17775997/article/details/116118504 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ElasticSearch学习笔记 | 分词、IK分词器和自定义分词
下一篇:ElasticSearch学习笔记 | Match_ALL进阶检索

发表评论

最新留言

很好
[***.229.124.182]2024年03月30日 13时28分19秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章