ElasticSearch中常用的数据类型
一、映射
Elasticsearch中通过映射来指定字段的数据类型,映射方式有2种,静态映射和动态映射。
1.动态映射
使用动态映射时,无须指定字段的数据类型,Elasticshearch会自动根据字段内容来判断映射到哪个数据类型。
比如,插入’2024/4/14 12:12:12’格式的字符串,会将其动态映射到Date类型。
true和false会动态映射到boolean类型
2.静态映射
使用静态映射时,需要在创建索引时,指定字段名称和映射的数据类型。后续创建文档时就根据设定好的映射来存储数据。
二、常用字段类型
1.Alias别名
使用别名可以索引中的字段定义一个替代名称,搜索替代名称时,相当于搜索其原字段内容
PUT userinfo
{"mappings": {"properties": {"age":{"type": "long"},"aliasage":{#指定类型为别名"type": "alias",#指向age字段"path":"age"}}}
}
2.Binary二进制值
用来存储Base64的字符串或二进制值
PUT myindex-2_02
{"mappings": {"properties": {"name":{"type": "text"},"blob":{#指定类型为二进制"type": "binary",}}}
}
3.Boolean布尔类型
用来存储true和false的布尔值
PUT myindex-2_03
{"mappings": {"properties": {"is_published":{"type": "booolean"}}}
}
4.Date日期
用来存储包含日期格式的字符串
PUT myindex-2_04
{"mappings": {"properties": {"date":{"type": "date"}}}
}
5.数字类型
数字类型 | 说明 |
---|---|
long | 表示有符号的64位整数 |
integer | 表示有符号的32位整数 |
short | 表示有符号的16位整数 |
byte | 表示有符号的8位整数 |
double | 表示双精度浮点数 |
float | 表示单精度浮点数 |
half_float | 存储最小值2^-24,最大值65504的整数 |
unsigned_long | 存储最小值为0,最大值2^64-1的数值 |
PUT myindex-2_05
{"mappings": {"properties": {"number":{"type": "integer"}}}
}
6.object(json数据格式)
object类型其实就是json数据格式
PUT myindex-object
{"mappings": {"properties": {"manager":{"properties": {"age":{"type":"integer"},"name":{"properties":{"first":{"type":"text"},"last":{"type":"text"}}}}}}}
}
7.Geopoint地理位置
用来存储经纬度,使内容支持经纬度查询
PUT myindex_hotels
{"mappings": {"properties": {"hotels":{"properties": {"bin":{"properties":{"loaction": {"type":"geo_point"}}}}}}}
}
8.Keyword关键字
用于存储结构化内容,该类型字段通常用于排序,聚合查询等。该类型内容存储时不会进行分词
PUT myindex-2_11
{"mappings": {"properties": {"tag":{"type": "keyword"}}}
}
9.Text文本
用于存储文本,内容会被分词器分词,可以用于全文检索
PUT myindex-2_12
{"mappings": {"properties": {"tagname":{"type": "text"}}}
}