string类型在ElasticSearch 旧版本中使用较多,从ElasticSearch 5.x开始再也不支持string,由text和keyword类型替代。html
当一个字段是要被全文搜索的,好比Email内容、产品描述,应该使用text类型。设置text类型之后,字段内容会被分析,在生成倒排索引之前,字符串会被分析器分红一个一个词项。text类型的字段不用于排序,不多用于聚合。数组
keyword类型适用于索引结构化的字段,好比email地址、主机名、状态码和标签。若是字段须要进行过滤(好比查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能经过精确值搜索到。app
类型 | 取值范围 |
---|---|
byte | -128~127 |
short | -32768~32767 |
integer | -231~231-1 |
long | -263~263-1 |
在知足需求的状况下,尽量选择范围小的数据类型。好比,某个字段的取值最大值不会超过100,那么选择byte类型便可。迄今为止吉尼斯记录的人类的年龄的最大值为134岁,对于年龄字段,short足矣。字段的长度越短,索引和搜索的效率越高。elasticsearch
类型 | 取值范围 |
---|---|
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。ide
其中scaled_float,好比价格只须要精确到分,price为57.34的字段缩放因子为100,存起来就是5734
优先考虑使用带缩放因子的scaled_float浮点类型。测试
日期类型表示格式能够是如下几种:ui
(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)spa
ElasticSearch 内部会将日期数据转换为UTC,并存储为milliseconds-since-the-epoch的long型整数。日志
定义日期类型和日志格式:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#strict-date-timecode
逻辑类型(布尔类型)能够接受true/false/”true”/”false”值
PUT information { "mappings": { "record": { "properties": { "is_delete": { "type": "boolean" } } } } }
新建文档:
PUT information/record/1 { "is_delete": "true" } PUT information/record/2 { "is_delete":false }
查询文档;
GET information/record/_mget { "ids":[1,2] }
查询结果:
{ "docs": [ { "_index": "information", "_type": "record", "_id": "1", "_version": 1, "found": true, "_source": { "is_delete": "true" } }, { "_index": "information", "_type": "record", "_id": "2", "_version": 1, "found": true, "_source": { "is_delete": false } } ] }
二进制字段是指用base64来表示索引中存储的二进制数据,可用来存储二进制形式的数据,例如图像。默认状况下,该类型的字段只存储不索引。二进制类型只支持index_name属性。
在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]}
JSON天生具备层级关系,文档会包含嵌套的对象
PUT information/record/1 { "title": "这是一个测试", "event": { "name": "sacn扫描", "times": 20 } }
建立文档包含title和event两个属性,其中event为对象类型也包含两个属性name和times。
{ "_index": "information", "_type": "record", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
查询mapping结构:
{ "information": { "mappings": { "record": { "properties": { "event": { "properties": { "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "times": { "type": "long" } } }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } } }
ip类型的字段用于存储IPv4或者IPv6的地址
PUT information { "mappings": { "record": { "properties": { "attack_ip": { "type": "ip" } } } } }
新增文档:
PUT information/record/1 { "attack_ip":"10.118.213.192" }
查询文档:
GET information/record/1
查询结果:
{ "_index": "information", "_type": "record", "_id": "1", "_version": 1, "found": true, "_source": { "attack_ip": "10.118.213.192" } }