赞
踩
官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
es6中,string类型已经被废弃了,需要使用text、keyword类型来代替。
1)text类型:
当一个字段需要用于全文搜索(会被分词),比如产品名称、产品描述信息, 就应该使用text类型。该类型字段会通过分析器转成terms list,然后存入索引。该类型字段不用于排序、聚合操作。
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "full_name": {
- "type": "text"
- }
- }
- }
- }
- }
- '
2)keyword类型:
当一个字段需要按照精确值进行过滤、排序、聚合等操作时, 就应该使用keyword类型。该类型的字段值不会被分析器处理(分词)
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "tags": {
- "type": "keyword"
- }
- }
- }
- }
- }
- '
类型 | 说明 |
---|---|
byte | 有符号的8位整数, 范围: [-128 ~ 127] |
short | 有符号的16位整数, 范围: [-32768 ~ 32767] |
integer | 有符号的32位整数, 范围: [−231−231 ~ 231231-1] |
long | 有符号的64位整数, 范围: [−263−263 ~ 263263-1] |
float | 32位单精度浮点数 |
double | 64位双精度浮点数 |
half_float | 16位半精度IEEE 754浮点类型 |
scaled_float | 缩放类型的的浮点数, 比如price字段只需精确到分, 57.34缩放因子为100, 存储结果为5734 |
尽可能选择范围小的数据类型, 字段的长度越短, 索引和搜索的效率越高。示例:
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "number_of_bytes": {
- "type": "integer"
- },
- "time_in_seconds": {
- "type": "float"
- },
- "price": {
- "type": "scaled_float",
- "scaling_factor": 100
- }
- }
- }
- }
- }
- '

1)boolean类型:
可以使用boolean类型的(true、false)也可以使用string类型的(“true”、“false”)。
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "is_published": {
- "type": "boolean"
- }
- }
- }
- }
- }
- '
- #插入
- curl -X POST "localhost:9200/my_index/my_type/1?pretty" -H 'Content-Type: application/json' -d'
- {
- "is_published": "true"
- }
- '
- #查询
- curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
- {
- "query": {
- "term": {
- "is_published": true
- }
- }
- }
- '

2)binary类型:
二进制类型是Base64编码字符串的二进制值,不以默认的方式存储,且不能被搜索。
- #添加映射
- PUT website
- {
- "mappings": {
- "blog": {
- "properties": {
- "blob": {"type": "binary"} // 二进制
- }
- }
- }
- }
- #添加数据
- PUT website/blog/1
- {
- "title": "Some binary blog",
- "blob": "hED903KSrA084fRiD5JLgY=="
- }

JSON没有日期数据类型, 所以在ES中, 日期可以是:
1)使用format指定格式:
若未指定格式,则使用默认格式: strict_date_optional_time||epoch_millis
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "date": {
- "type": "date"
- }
- }
- }
- }
- }
- '
- curl -X PUT "localhost:9200/my_index/my_type/1?pretty" -H 'Content-Type: application/json' -d'
- { "date": "2015-01-01" }
- '
- curl -X PUT "localhost:9200/my_index/my_type/2?pretty" -H 'Content-Type: application/json' -d'
- { "date": "2015-01-01T12:10:30Z" }
- '
- curl -X PUT "localhost:9200/my_index/my_type/3?pretty" -H 'Content-Type: application/json' -d'
- { "date": 1420070400001 }
- '
- curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
- {
- "sort": { "date": "asc"}
- }
- '

2)指定多个format:
使用双竖线||分隔指定多种日期格式,每个格式都会被依次尝试,直到找到匹配的。
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "date": {
- "type": "date",
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
- }
- }
- }
- }
- }
- '
类型 | 范围 |
---|---|
integer_range | −231−231 ~ 231−1231−1 |
long_range | −263−263 ~ 263−1263−1 |
float_range | 32位单精度浮点型 |
double_range | 64位双精度浮点型 |
date_range | 64位整数, 毫秒计时 |
ip_range | IP值的范围, 支持IPV4和IPV6, 或者这两种同时存在 |
- #添加映射
- PUT company
- {
- "mappings": {
- "department": {
- "properties": {
- "expected_number": { // 预期员工数
- "type": "integer_range"
- },
- "time_frame": { // 发展时间线
- "type": "date_range",
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
- }
- }
- }
- }
- }
- #插入数据
- PUT company/department/1
- {
- "expected_number" : {
- "gte" : 10,
- "lte" : 20
- },
- "time_frame" : {
- "gte" : "2018-10-01 12:00:00",
- "lte" : "2018-11-01"
- }
- }
- #查询
- GET company/department/_search
- {
- "query": {
- "term": {
- "expected_number": {
- "value": 12
- }
- }
- }
- }
- GET company/department/_search
- {
- "query": {
- "range": {
- "time_frame": {
- "gte": "208-08-01",
- "lte": "2018-12-01",
- "relation": "within"
- }
- }
- }
- }
- #查询结果
- {
- "took": 26,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": 1,
- "max_score": 1.0,
- "hits": [
- {
- "_index": "company",
- "_type": "department",
- "_id": "1",
- "_score": 1.0,
- "_source": {
- "expected_number": {
- "gte": 10,
- "lte": 20
- },
- "time_frame": {
- "gte": "2018-10-01 12:00:00",
- "lte": "2018-11-01"
- },
- "ip_whitelist" : "192.168.0.0/16"
- }
- }
- ]
- }
- }

es支持复杂的数据类型,包括:object、array、nested。用的不是很多,举个实例:
- #创建mapping
- curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
- {
- "mappings": {
- "my_type": {
- "properties": {
- "region": {
- "type": "keyword"
- },
- "manager": {
- "properties": {
- "age": { "type": "integer" },
- "name": {
- "properties": {
- "first": { "type": "text" },
- "last": { "type": "text" }
- }
- }
- }
- }
- }
- }
- }
- }
- '
- #插入数据
- curl -X PUT "localhost:9200/my_index/my_type/1?pretty" -H 'Content-Type: application/json' -d'
- {
- "region": "US",
- "manager": {
- "age": 30,
- "name": {
- "first": "John",
- "last": "Smith"
- }
- }
- }
- '

存储方式:
- {
- "region": "US",
- "manager.age": 30,
- "manager.name.first": "John",
- "manager.name.last": "Smith"
- }
geo point类型用于存储地理位置的经纬度对,可用于:
- #mapping
- PUT employee
- {
- "mappings": {
- "developer": {
- "properties": {
- "location": {"type": "geo_point"}
- }
- }
- }
- }
- # 方式一: 纬度 + 经度键值对
- PUT employee/developer/1
- {
- "text": "小蛮腰-键值对地理点参数",
- "location": {
- "lat": 23.11, "lon": 113.33 // 纬度: latitude, 经度: longitude
- }
- }
-
- # 方式二: "纬度, 经度"的字符串参数
- PUT employee/developer/2
- {
- "text": "小蛮腰-字符串地理点参数",
- "location": "23.11, 113.33" // 纬度, 经度
- }
-
- # 方式三: ["经度, 纬度"] 数组地理点参数
- PUT employee/developer/3
- {
- "text": "小蛮腰-数组参数",
- "location": [ 113.33, 23.11 ] // 经度, 纬度
- }
-
- #查询
- GET employee/_search
- {
- "query": {
- "geo_bounding_box": {
- "location": {
- "top_left": { "lat": 24, "lon": 113 }, // 地理盒子模型的上-左边
- "bottom_right": { "lat": 22, "lon": 114 } // 地理盒子模型的下-右边
- }
- }
- }
- }

使用较少,可参考:https://blog.csdn.net/u012332735/article/details/54971638
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。