es7.+(三)mapping

1.mapping

用来定义一个文档如何被处理的,这些属性字段是怎么被存储和被检索的

  • 使用哪个mapping去定义哪个String字段应该被当做全文检索字段
  • 哪一个字段包含数值、日期或者地理位置坐标
  • 文档中的所有属性是否都能被索引(_all配置)
  • 日期的格式化数据
  • 自定义映射规则来执行动态添加属性

es会自动的为数据定义mapping,但有的时候需要自定义

1.1查询已经存有数据的索引index的信息

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

1.2如何自定义映射

  • 创建一个索引
    PUT /my_index
  • 在新索引保存数据之前,可以先创建映射规则
{
  "mappings": {
    "properties": {
      "age":{
        "type": "integer"
      },
      "email":{
        "type": "keyword"
      },
      "name":{
        "type": "text"
      }
    }
  }
}

返回内容

    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "my_index"
    }

1.3添加新的字段映射

比如我们需要新增一个字段,名字是 employee-id

该index选项控制是否对字段值建立索引。它接受true 或false,默认为true。未索引的字段不可查询
index选项控制这个属性的值能不能被索引,为true或false,默认是true。如果一个字段的index为false那么他就不会被检索到,他是来控制这个字段是不是参与检索的

PUT /my_index/_mapping
{
  "properties":{
    "employee-id":{
      "type": "keyword",
      "index": false
    }
  }
}

1.4修改映射数据迁移

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

我们先来创建新的映射关系,稍后我们将进行数据迁移

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

进行数据迁移

  • 7.+版本
    POST _reindex
    {
        "source":{
            "index": "bank"
        },
        "dest":{
            "index": "newbank"
        }
    }
  • 7.+版本之前(还使用type的版本)
POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}

关于es7-去掉type的概念

原文地址:https://www.cnblogs.com/psyduck/p/14472015.html