ES数据类型

https://blog.csdn.net/chengyuqiang/article/details/79048800

数组类型:没有明显的字段类型设置,任何一个字段的值,都可以被添加0个到多个,要求,他们的类型必须一致,当类型一直含有多个值存储到ES中会自动转化成数组类型
对于数组类型的数据,是一个数组元素做一个数据单元,如果是分词的话也只是会依一个数组元素作为词源进行分词。不会是所有的数组元素整合到一起。在查询的时候如果数组里面的元素有一个能够命中那么将视为命中,被召回。

2.1 字段类型概述
一级分类 二级分类 具体类型
核心类型 字符串类型 string,text,keyword
整数类型 integer,long,short,byte
浮点类型 double,float,half_float,scaled_float
逻辑类型 boolean
日期类型 date
范围类型 range
二进制类型 binary
复合类型 数组类型 array
对象类型 object
嵌套类型 nested
地理类型 地理坐标类型 geo_point
地理地图 geo_shape
特殊类型 IP类型 ip
范围类型 completion
令牌计数类型 token_count
附件类型 attachment
抽取类型 percolator
12.2 字符串类型
(1)string
string类型在ElasticSearch 旧版本中使用较多,从ElasticSearch 5.x开始不再支持string,由text和keyword类型替代。
(2)text
当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合。
(3)keyword
keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。

12.3 整数类型
类型 取值范围
byte -128~127
short -32768~32767
integer -231~231-1
long -263~263-1
在满足需求的情况下,尽可能选择范围小的数据类型。比如,某个字段的取值最大值不会超过100,那么选择byte类型即可。迄今为止吉尼斯记录的人类的年龄的最大值为134岁,对于年龄字段,short足矣。字段的长度越短,索引和搜索的效率越高。

12.4 浮点类型
类型 取值范围
doule 64位双精度IEEE 754浮点类型
float 32位单精度IEEE 754浮点类型
half_float 16位半精度IEEE 754浮点类型
scaled_float 缩放类型的的浮点数
对于float、half_float和scaled_float,-0.0和+0.0是不同的值,使用term查询查找-0.0不会匹配+0.0,同样range查询中上边界是-0.0不会匹配+0.0,下边界是+0.0不会匹配-0.0。

其中scaled_float,比如价格只需要精确到分,price为57.34的字段缩放因子为100,存起来就是5734
优先考虑使用带缩放因子的scaled_float浮点类型。

12.5 date类型
我们人类使用的计时系统是相当复杂的:秒是基本单位, 60秒为1分钟, 60分钟为1小时, 24小时是一天……如果计算机也使用相同的方式来计时, 那显然就要用多个变量来分别存放年月日时分秒, 不停的进行进位运算, 而且还要处理偶尔的闰年和闰秒以及协调不同的时区. 基于”追求简单”的设计理念, UNIX在内部采用了一种最简单的计时方式:

计算从UNIX诞生的UTC时间1970年1月1日0时0分0秒起, 流逝的秒数.
UTC时间1970年1月1日0时0分0秒就是UNIX时间0, UTC时间1970年1月2日0时0分0秒就是UNIX时间86400.
这个计时系统被所有的UNIX和类UNIX系统继承了下来, 而且影响了许多非UNIX系统.
1
2
3
日期类型表示格式可以是以下几种:
(1)日期格式的字符串,比如 “2018-01-13” 或 “2018-01-13 12:10:30”
(2)long类型的毫秒数( milliseconds-since-the-epoch,epoch就是指UNIX诞生的UTC时间1970年1月1日0时0分0秒)
(3)integer的秒数(seconds-since-the-epoch)

ElasticSearch 内部会将日期数据转换为UTC,并存储为milliseconds-since-the-epoch的long型整数。
例子:日期格式数据
(1)创建索引

DELETE test

PUT test
{
“mappings”:{
“my”:{
“properties”: {
“postdate”:{
“type”:”date”,
“format”: “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(2)写入文档

PUT test/my/1
{
“postdate”:”2018-01-13”
}
PUT test/my/2
{
“postdate”:”2018-01-01 00:01:05”
}
PUT test/my/3
{
“postdate”:”1420077400001”
}
1
2
3
4
5
6
7
8
9
10
11
12
(3)批量查询

GET test/my/_mget
{
“ids”:[“1”,”2”,”3”]
}
1
2
3
4
{
“docs”: [
{
“_index”: “test”,
“_type”: “my”,
“_id”: “1”,
“_version”: 1,
“found”: true,
“_source”: {
“postdate”: “2018-01-13”
}
},
{
“_index”: “test”,
“_type”: “my”,
“_id”: “2”,
“_version”: 1,
“found”: true,
“_source”: {
“postdate”: “2018-01-01 00:01:05”
}
},
{
“_index”: “test”,
“_type”: “my”,
“_id”: “3”,
“_version”: 2,
“found”: true,
“_source”: {
“postdate”: “1420077400001”
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
12.6 boolean类型
逻辑类型(布尔类型)可以接受true/false/”true”/”false”值
(1)先删除已经存在的索引,再创建

DELETE test

PUT test
{
“mappings”:{
“my”:{
“properties”: {
“empty”:{“type”:”boolean”}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
(2)添加文档

PUT test/my/1
{
“empty”:”true”
}

PUT test/my/2
{
“empty”:false
}
1
2
3
4
5
6
7
8
9
(3)查看文档

GET test/my/_mget
{
“ids”:[“1”,”2”]
}
1
2
3
4
{
“docs”: [
{
“_index”: “test”,
“_type”: “my”,
“_id”: “1”,
“_version”: 1,
“found”: true,
“_source”: {
“empty”: “true”
}
},
{
“_index”: “test”,
“_type”: “my”,
“_id”: “2”,
“_version”: 1,
“found”: true,
“_source”: {
“empty”: false
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
12.7 binary类型
二进制字段是指用base64来表示索引中存储的二进制数据,可用来存储二进制形式的数据,例如图像。默认情况下,该类型的字段只存储不索引。二进制类型只支持index_name属性。

12.7 array类型
在ElasticSearch中,没有专门的数组(Array)数据类型,但是,在默认情况下,任意一个字段都可以包含0或多个值,这意味着每个字段默认都是数组类型,只不过,数组类型的各个元素值的数据类型必须相同。在ElasticSearch中,数组是开箱即用的(out of box),不需要进行任何配置,就可以直接使用。

在同一个数组中,数组元素的数据类型是相同的,ElasticSearch不支持元素为多个数据类型:[ 10, “some string” ],常用的数组类型是:

(1)字符数组: [ “one”, “two” ]
(2)整数数组: productid:[ 1, 2 ]
(3)对象(文档)数组: “user”:[ { “name”: “Mary”, “age”: 12 }, { “name”: “John”, “age”: 10 }],ElasticSearch内部把对象数组展开为 {“user.name”: [“Mary”, “John”], “user.age”: [12,10]}

12.8 object类型
JSON天生具有层级关系,文档会包含嵌套的对象

DELETE test

PUT test

PUT test/my/1
{
“employee”:{
“age”:30,
“fullname”:{
“first”:”hadron”,
“last”:”cheng”
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上面文档整体是一个JSON,JSON中包含一个employee,employee又包含一个fullname。

GET test/_mapping
1
2
{
“test”: {
“mappings”: {
“my”: {
“properties”: {
“employee”: {
“properties”: {
“age”: { “type”: “long”},
“fullname”: {
“properties”: {
“first”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“last”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
}
}
}
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
12.9 ip类型
ip类型的字段用于存储IPv4或者IPv6的地址
(1)创建索引

DELETE test

PUT test
{
“mappings”: {
“my”:{
“properties”: {
“nodeIP”:{
“type”: “ip”
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(2)查询字段

GET test/_search
{
“query”: {
“term”: {
“nodeIP”: “192.168.0.0/16”
}
}
}
1
2
3
4
5
6
7
8
{
“took”: 111,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: 1,
“max_score”: 1,
“hits”: [
{
“_index”: “test”,
“_type”: “my”,
“_id”: “1”,
“_score”: 1,
“_source”: {
“nodeIP”: “192.168.1.2”
}
}
]
}
}

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值