六、ElasticSearch——映射详解(映射、字段类型、字段属性、多重字段、元字段、动态映射)

1 Mapping 映射
(1)简介
映射定义索引中有什么字段、字段的类型等结构信息。相当于数据库中表结构定义,或 solr中的schema。因为lucene索引文档时需要知道该如何来索引存储文档的字段。ES中支持手动定义映射,动态映射两种方式。

Create Index with mapping创建索引映射
在这里插入图片描述
映射类别 Mapping type 废除说明

ES最先的设计是用索引类比关系型数据库的数据库,用mapping type 来类比表,一个索引中可以包含多个映射类别。这个类比存在一个严重的问题,就是当多个mapping type中存在同名字段时(特别是同名字段还是不同类型的),在一个索引中不好处理,因为搜索引擎中只有 索引-文档的结构,不同映射类别的数据都是一个一个的文档(只是包含的字段不一样而已)

从6.0.0开始限定仅包含一个映射类别定义( “index.mapping.single_type”: true ),兼容5.x中的多映射类别。从7.0开始将移除映射类别。
为了与未来的规划匹配,请现在将这个唯一的映射类别名定义为“_doc”,因为索引的请求地址将规范为:PUT {index}/_doc/{id} and POST {index}/_doc

多个映射类别就转变为多个索引。如果你还想要在一个索引中放多类数据,也可像在数据库中定义骷髅表一样,通过定义一个数据类别字段,来区分不同的数据。

(2)Mapping 映射示例

PUT twitter
{
“mappings”: {
“_doc”: {
“properties”: {
“type”: { “type”: “keyword” },
“name”: { “type”: “text” },
“user_name”: { “type”: “keyword” },
“email”: { “type”: “keyword” },
“content”: { “type”: “text” },
“tweeted_at”: { “type”: “date” }
}
}
}
}

(3)多映射类别数据转储到独立的索引中
在这里插入图片描述

2 字段类型 datatypes

字段类型定义了该如何索引存储字段值。ES中提供了丰富的字段类型定义,请查看官网链接详细了解每种类型的特点:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

1)Core Datatypes 核心类型
(1)string
text(分词) and keyword (不分词)
(2)Numeric datatypes
long, integer, short, byte, double, float, half_float, scaled_float
(3)Date datatype
date
(4)Boolean datatype
boolean
(5)Binary datatype
binary
(6)Range datatypes 范围
integer_range, float_range, long_range, double_range, date_range

2)Complex datatypes 复合类型
(1)Array datatype
数组就是多值,不需要专门的类型
(2)Object datatype
object :表示值为一个JSON 对象
(3)Nested datatype
nested:for arrays of JSON objects(表示值为JSON对象数组 )

3)Geo datatypes 地理数据类型
(1)Geo-point datatype
geo_point: for lat/lon points (经纬坐标点)
(2)Geo-Shape datatype
geo_shape: for complex shapes like polygons (形状表示)

  1. Specialised datatypes 特别的类型
    (1) IP datatype
    ip: for IPv4 and IPv6 addresses
    (2)Completion datatype
    completion: to provide auto-complete suggestions
    (3)Token count datatype
    token_count: to count the number of tokens in a string
    (4)mapper-murmur3
    murmur3: to compute hashes of values at index-time and store them in the index
    (5)Percolator type
    Accepts queries from the query-dsl
    (6)join datatype
    Defines parent/child relation for documents within the same index

3 字段定义属性介绍

字段的type (Datatype)定义了如何索引存储字段值,还有一些属性可以让我们根据需要来覆盖默认的值或进行特别定义。请参考官网介绍详细了解:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html
analyzer 指字段定分词器
normalizer 指字段定标准化器
boost 指定权重值
coerce 强制类型转换
copy_to 值复制给另一字段
doc_values 是否存储docValues
dynamic
enabled 字段是否可用(设置为false,不会被搜索)
fielddata(对于分词的字段是否需要做聚合及排序时需要考虑设置该属性)
eager_global_ordinals
format 指定时间值的格式
ignore_above 忽略大于
ignore_malformed
index_options
index
fields
norms
null_value
position_increment_gap
properties
search_analyzer
similarity
store
term_vector

字段定义属性—示例

PUT my_index
{
“mappings”: {
“_doc”: {
“properties”: {
“date”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”
}
}
}
}
}

4 Multi Field 多重字段

当我们需要对一个字段进行多种不同方式的索引时,可以使用fields多重字段定义。如一个字符串字段即需要进行text分词索引,也需要进行keyword 关键字索引来支持排序、聚合;或需要用不同的分词器进行分词索引。

Multi Field 多重字段—示例
在这里插入图片描述
5 元字段

元字段是ES中定义的文档字段,有以下几类:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
详细了解:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-fields.html

-source
如果索引的数据是网页、新闻、文章,应该把_source字段关闭,把那些需要在搜索结果中返回的字段,设置store为true

6 动态映射

动态映射:ES中提供的重要特性,让我们可以快速使用ES,而不需要先创建索引、定义映射。 如我们直接向ES提交文档进行索引:

PUT data/_doc/1
{ “count”: 5 }

ES将自动为我们创建data索引、_doc 映射、类型为 long 的字段 count索引文档时,当有新字段时, ES将根据我们字段的json的数据类型为我们自动加人字段定义到mapping中。
(1) 字段动态映射规则
在这里插入图片描述
(2) Date detection 时间侦测
date_detection 默认是开启的,默认的格式dynamic_date_formats为:
[ “strict_date_optional_time”,“yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z”]

PUT my_index/_doc/1
{
“create_date”: “2015/09/02”
}

GET my_index/_mapping

在这里插入图片描述
(3) Numeric detection 数值侦测

PUT my_index
{
“mappings”: {
“_doc”: {
“numeric_detection”: true 开启数值侦测(默认是禁用的)

}

}
}

PUT my_index/_doc/1
{
“my_float”: “1.0”,
“my_integer”: “1”
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值