Elasticsearch的Mapping的简介

一、Mapping的概念

1.Mapping的定义

Mapping:是定义文档及其包含的字段如何存储和索引的过程。它类似于关系型数据库中的schema,主要有以下几个作用:

  • 定义索引中的字段的名称
  • 定义字段的数据类型,如字符串、数字、日期、布尔等等
  • 字段,倒排索引的相关配置(Analyzed or Not Analyzed, Analyzer)

Mapping会把JSON文档映射成Lucene所需要的扁平格式。注意,从7.0开始,不需要在Mapping定义中指定type信息,默认就是_doc。

2.Elasticsearch中字段的数据类型

Elasticsearch主要包以下几种数据类型:

  • 简单(核心)类型
    • String ( text and keyword)
    • Date (date)
    • Numeric (long,integer,short,byte,double,float,half_float,scaled_float)
    • Boolean (boolean)
    • Binary (binary)
    • Range (integer_range,float_range,long_range,double_range,date_range)
  • 复杂类型
    • Object对象类型 object for single JSON objects
    • Nested嵌套类型 nested for arrays of JSON objects
  • 特殊类型
    • IP (ip for IPv4 and IPv6 addresses)
    • 地理位置 (geo_point,geo_shape)
    • 等等

2.显示Mapping的设置及常见参数

1.显示Mapping的设置

有两种方式可以设置Mapping

  • 创建index的直接加入Mapping的配置信息
  • 利用PUT mapping API给已经存在的索引创建或者新增Mapping

1.创建index的直接加入Mapping的配置信息

PUT student
{
  "mappings" : {
      "dynamic": "strict",
      "properties" : {
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer": "ik_smart"
        },
        "age" : {
          "type" : "long"
        },
        "bio" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
         "analyzer": "ik_smart"  
        },
        "birthDate" : {
          "type" : "date"
        },
        "height" : {
          "type" : "long"
        },
        "interest" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
         "analyzer": "ik_smart"
        },
        "isLeagueMember" : {
          "type" : "boolean"
        },
        "mobilePhone" : {
          "type" : "keyword",
          "index": false
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer": "ik_smart"  
        },
        "nation" : {
          "type" : "keyword",
          "index": true
        },
        "no" : {
          "type" : "keyword",
          "index": true
        },
        "sex" : {
          "type" : "keyword",
          "index": true
        },
        "weigt" : {
          "type" : "long"
        }
      }
    }
  }
}

插入测试数据:

POST student/_doc
{
  "no":"2016090298093",
  "name":"张三",
  "age":12,
  "sex": "男",
  "mobilePhone":"19900000000",
  "address":"山东省济南市历下区光明小区2号楼1单元",
  "weigt": 65,
  "height": 176,
  "birthDate":"1991-02-04",
  "interest":["篮球","排球","象棋","唱歌"],
  "bio":"个人经历个人经历个人经历个人经历",
  "nation":"汉族",
  "isLeagueMember": true
}

POST student/_doc
{
  "no":"2016090298094",
  "name":"王五",
  "age":13,
  "sex": "男",
  "mobilePhone":"19900000001",
  "address":"山东省济南市历城区温馨家园5号楼4单元",
  "weigt": 66,
  "height": 186,
  "birthDate":"1990-02-04",
  "interest":["足球","羽毛器球","军旗","看电影"],
  "bio":"个人经历个人经历个人经历个人经历",
  "nation":"回族族",
  "isLeagueMember": true
}

POST student/_doc
{
  "no":"2016090298095",
  "name":"马红",
  "age":13,
  "sex": "女",
  "mobilePhone":"19900000002",
  "address":"山东省济宁市兖州区康泰家园6号楼3单元",
  "weigt": 48,
  "height": 166,
  "birthDate":"1990-02-04",
  "interest":["读书","美容","书法","跳舞"],
  "bio":"个人经历个人经历个人经历个人经历",
  "nation":"维吾尔族",
  "isLeagueMember": true
}
POST student/_update/cVGu63cBenb0rF_OKNwV/
{
  "doc": {
    "nation":"回族"
  }
}

简单查询语句

GET student/_search
{
  "query": {
    "match_all": {
    }
  }
}


GET student/_search
{
  "query": {
    "match": {
      "no": "2016090298094"
    }
  }
}

GET student/_search
{
  "query": {
    "match": {
      "address": "济南市"
    }
  }
}

GET student/_search
{
  "query": {
    "match": {
      "mobilePhone": "1888"
    }
  }
}

GET student/_search
{
  "query": {
    "match": {
      "nation": "汉族"
    }
  }
}

GET student/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  }
}

2.利用PUT mapping API给已经存在的索引创建或者新增Mapping

加入我们加入一个字段department(在读哪个系)

PUT /student/_mapping?pretty
{
  "properties":{
    "department":{
      "type": "keyword"
    }
  }
}

查看mapping

{
  "student" : {
    "mappings" : {
      "dynamic" : "strict",
      "properties" : {
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer" : "ik_smart"
        },
        "age" : {
          "type" : "long"
        },
        "bio" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer" : "ik_smart"
        },
        "birthDate" : {
          "type" : "date"
        },
        "department" : {
          "type" : "keyword"
        },
        "height" : {
          "type" : "long"
        },
        "interest" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer" : "ik_smart"
        },
        "isLeagueMember" : {
          "type" : "boolean"
        },
        "mobilePhone" : {
          "type" : "keyword",
          "index" : false
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer" : "ik_smart"
        },
        "nation" : {
          "type" : "keyword"
        },
        "no" : {
          "type" : "keyword"
        },
        "sex" : {
          "type" : "keyword"
        },
        "weigt" : {
          "type" : "long"
        }
      }
    }
  }
}

3.自定义建立Mapping的方式

1.纯手写

可以直接参考API的手册,纯手写。关于Mapping的API文档:

https://www.elastic.co/guide/en/elasticsearch/reference/7.1/mapping.html#mapping

2.基于Dynamic Mapping生成的样例,进行二次修改

  1. 创建一个临时的index,写入一些样本数据
  2. 通过访问Mapping API获取该临时Index的动态Mapping定义
  3. 基于自己的需要,修改Mapping的配置,然后使用修改后的Mapping配置来创建索引
  4. 删除第一步建立的临时index
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值