1.本文总结自 Field datatypes | Elasticsearch Guide [7.2] | Elastic
2.本文罗列了 elasticsearch常用的字段类型;
3.es字段类型总结(https://www.elastic.co/guide/en/elasticsearch/reference/7.2/mapping-types.html):
1)text类型:文本类型(分词)
2)keyword:关键字类型(不分词)
数值类型的子类型列表:

- // 建立索引映射,带有 数值类型
- PUT my_index
- {
- "mappings": {
- "properties": {
- "number_of_bytes": {
- "type": "integer"
- },
- "time_in_seconds": {
- "type": "float"
- },
- "price": {
- "type": "scaled_float",
- "scaling_factor": 100
- }
- }
- }
- }
1)Json并没有日期数据类型,因此es中的date字段表示为以下几种形式之一:
补充:unix纪元是 1970-01-01 00:00:00
2)在es内部,日期被转为世界标准时间UTC(若指定时区)并存储为长整型表示自纪元以来的毫秒数;
3)对日期的查询:
4)日期通常表示为字符串,即便在json文档中以long类型提供;
5)日期格式能够自定义,但若没有指定格式,则默认为如下格式:
这意味着该字段可以接收可选的时间戳日期,这些时间戳符合 strict_date_optional_time 或 milliseconds-since-the-epoch 支持的格式。
6)多个日期格式:
- PUT my_index
- {
- "mappings": {
- "properties": {
- "date": {
- "type": "date",
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
- }
- }
- }
- }
1)该数据类型是对date类型的补充。 然而,两者有着很大不同。
2)date_nanos的其他属性同 date类型。如
- PUT my_index?include_type_name=true
- {
- "mappings": {
- "_doc": {
- "properties": {
- "date": {
- "type": "date_nanos"
- }
- }
- }
- }
- }
1)boolean 接收json的true 和 false值,也接收能够解释为 true或false 的字符串。
2)例子:
- PUT my_index
- {
- "mappings": {
- "properties": {
- "is_published": {
- "type": "boolean"
- }
- }
- }
- }
1)二进制类型接收二进制值作为base64编码的字符串。该类型字段默认不存储且不能被搜索。
- // 创建索引
- PUT my_index
- {
- "mappings": {
- "properties": {
- "name": {
- "type": "text"
- },
- "blob": {
- "type": "binary"
- }
- }
- }
- }
-
- // 新增文档
- PUT my_index/_doc/1
- {
- "name": "Some binary blob",
- "blob": "U29tZSBiaW5hcnkgYmxvYg=="
- }
1)范围子类型有:
- // 创建索引
- PUT range_index
- {
- "settings": {
- "number_of_shards": 2
- },
- "mappings": {
- "properties": {
- "expected_attendees": {
- "type": "integer_range"
- },
- "time_frame": {
- "type": "date_range",
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
- }
- }
- }
- }
- // 新增文档
- PUT range_index/_doc/1?refresh
- {
- "expected_attendees" : {
- "gte" : 10,
- "lte" : 20
- },
- "time_frame" : {
- "gte" : "2015-10-31 12:00:00",
- "lte" : "2015-11-01"
- }
- }
1)对象类型:针对单个json文档 ;
2)es文档类型
- PUT my_index/_doc/1
- {
- "region": "US",
- "manager": {
- "age": 30,
- "name": {
- "first": "John",
- "last": "Smith"
- }
- }
- }
-
- // 内部地,这个文档被索引为简单的,扁平的kv键值对列表,如下:
- {
- "region": "US",
- "manager.age": 30,
- "manager.name.first": "John",
- "manager.name.last": "Smith"
- }
- // es映射定义如下:
- PUT my_index
- {
- "mappings": {
- "properties": {
- "region": {
- "type": "keyword"
- },
- "manager": {
- "properties": {
- "age": { "type": "integer" },
- "name": {
- "properties": {
- "first": { "type": "text" },
- "last": { "type": "text" }
- }
- }
- }
- }
- }
- }
- }
1)嵌套类型:针对 json文档数组 ;
2)这种 nested 嵌套类型是object 类型的特别版本,它允许对象数组被索引,通过这种方式使得数组元素可以被独立搜索;
3)数组对象如何被扁平化
- PUT my_index/_doc/1
- {
- "group" : "fans",
- "user" : [
- {
- "first" : "John",
- "last" : "Smith"
- },
- {
- "first" : "Alice",
- "last" : "White"
- }
- ]
- }
- // user 字段被es默认设置为object类型
在es内部,该json对象会被转换为如下文档。
- {
- "group" : "fans",
- "user.first" : [ "alice", "john" ],
- "user.last" : [ "smith", "white" ]
- }
使用es对象类型存储对象数组带来的问题:
1)如果你需要索引对象数组且保持数组中每个对象的独立性,你应该使用 nested嵌套数据类型,而不是object对象类型。
2)在es内部,嵌套类型把数组中的每个对象索引为单个隐藏文档,这意味着每一个嵌套对象能够独立于其他嵌套对象被搜索。
3)嵌套搜索dsl如下:
- // 创建索引 (带有嵌套数据类型-nested)
- PUT my_index
- {
- "mappings": {
- "properties": {
- "user": {
- "type": "nested"
- }
- }
- }
- }
- // 新增文档 (以数组形式新增文档到嵌套类型)
- PUT my_index/_doc/1
- {
- "group" : "fans",
- "user" : [
- {
- "first" : "John",
- "last" : "Smith"
- },
- {
- "first" : "Alice",
- "last" : "White"
- }
- ]
- }
- // 搜索文档 (嵌套查询dsl)
- GET my_index/_search
- {
- "query": {
- "nested": {
- "path": "user",
- "query": {
- "bool": {
- "must": [
- { "match": { "user.first": "Alice" }},
- { "match": { "user.last": "Smith" }}
- ]
- }
- }
- }
- }
- }
4)nested-嵌套文档能够进行如下操作:
5)对nested嵌套映射和对象的限制设置
如以上描述所说,每一个嵌套文档会被索引为独立文档。
因为涉及到nested嵌套映射的成本,es需要做一些设置以防出现性能问题。
在es中, 数组不需要专用的数据类型。
1)根据不同目的以不同方式索引同一字段是有帮助的;
2)此外,你还可以用标准分词器,英文分词器或法语分词器来索引text字段;
3)通过 fields 参数来指定同一个字段的多种数据类型。
- // 字符串字段可以被映射为 text字段以便全文搜索
- // 也可以同时映射为keword以便于排序或聚合
- PUT my_index
- {
- "mappings": {
- "properties": {
- "city": {
- "type": "text",
- "fields": {
- "raw": {
- "type": "keyword"
- }
- }
- }
- }
- }
- }
如 地理位置信息数据类型,ip数据类型等。
详情参见: https://www.elastic.co/guide/en/elasticsearch/reference/7.2/mapping-types.html