【ElasticSearch(十一)进阶】Mapping的查询、创建、修改、删除

  • Mapping(映射)是定义文档及其包含的字段的存储和索引方式的处理过程。

例如,使用Mapping定义:

哪些字符串字段应视为全文字段。
哪些字段包含数字,日期或地理位置。
日期值 的格式。
自定义规则,用于控制动态添加字段的映射 。

  • 字段的数据类型,会在插入第一条数据时 自动识别。

    但我们也可以自己 指定和修改

一、查询Mapping

查询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"
        },
        "city" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "email" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "employer" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "firstname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "gender" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

二、创建Mapping

可以利用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时,不可以通过这个属性查询该数据。

PUT /my_index/_mapping
{
  "properties":{
    "employee_id":{
      "type": "keyword",
      "index": false
    }
  }
}

返回结果:

{
  "acknowledged" : true
}

四、修改映射

对于已经存在的映射字段,我们不能更新。想要更新必须创建新的索引,然后进行数据迁移。

1.先创建新的映射关系

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"
      }
    }
  }
}

2.数据迁移

  • ES 7.x版本不包含Type属性,迁移方式:

直接把旧index下的数据 迁移到新index下

POST _reindex
{
    "source":{
        "index": "twitter"
    },
    "dest":{
        "index": "new_twitter"
    }
}
  • 旧版本有Type属性,在index的type下的数据 进行迁移:

把旧index的type下的数据 迁移到新index下,我们希望新的迁移后就不存在type了。

POST _reindex
{
  "source":{
    "index": "bank",
    "type": "account"
  },
  "dest":{
    "index": "newbank"
  }
}

返回结果,显示迁移成功

#! [types removal] Specifying types in reindex requests is deprecated.
{
  "took" : 51,
  "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

返回结果:

可以发现_type的值不是account了,变成_doc了(默认类型)。

{
  "took" : 836,
  "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",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      {
        "_index" : "newbank",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 6,
          "balance" : 5686,
          "firstname" : "Hattie",
          "lastname" : "Bond",
          "age" : 36,
          "gender" : "M",
          "address" : "671 Bristol Street",
          "employer" : "Netagy",
          "email" : "hattiebond@netagy.com",
          "city" : "Dante",
          "state" : "TN"
        }
      },
      。。。
    ]
  }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐