ES映射

ES有个强大的功能,在索引的同时自动完成索引、类型、映射的创建。
什么是映射呢?映射就是描述字段的类型、如何进行分析、如何进行索引等内容。

字段自动检测

字段如果在定义是没有映射,ES会自动检测他可能对应的字段类型,创建相应的映射。

JSON数据 ES中数据类型
null null
true、false boolean
浮点数 double
object object
整数 long
数组 取决于第一个非空的值
字符串 如果可以转换为date,则为date;可以转化为数字,则为number

以上是自动检测的基本结果,其他高级的类型如ip、geo手动指定。

日期自动检测

日期自动检测,即date_detection是默认开启的,因此只要符合默认的日期格式,就可以自动创建成date类型。
日期的格式为:

1
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]

如原始数据格式为:

_index _type _id _score title text date
website blog 123 1 My first blog entry Just trying this out… 2014/01/01

通过 http://localhost:9200/website/_mapping?pretty/ 查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"website":  {
"mappings":  {
"blog":  {
"properties":  {
"date":  {
"type":   "date",
"format":   "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"text":  {
"type":   "string"
},
"title":  {
"type":   "string"
}
}
}
}
}
}

数字自动检测

数字自动检测,即numeric_detection默认是关闭的。因此需要手动打开:

1
2
3
4
5
6
7
8
PUT my_index
{
"mappings": {
"my_type": {
"numeric_detection": true
}
}
}

ES映射
https://leehoward.cn/2019/10/16/ES映射/
作者
lihao
发布于
2019年10月16日
许可协议