当前位置:   article > 正文

es 数据类型

es 数据类型

官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

核心数据类型

1、字符串类型:

es6中,string类型已经被废弃了,需要使用text、keyword类型来代替。

1)text类型:

当一个字段需要用于全文搜索(会被分词),比如产品名称、产品描述信息, 就应该使用text类型。该类型字段会通过分析器转成terms list,然后存入索引。该类型字段不用于排序、聚合操作。

  1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "full_name": {
  7. "type": "text"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. '

2)keyword类型:

当一个字段需要按照精确值进行过滤、排序、聚合等操作时, 就应该使用keyword类型。该类型的字段值不会被分析器处理(分词)

  1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "tags": {
  7. "type": "keyword"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. '

2、数字类型

类型说明
byte有符号的8位整数, 范围: [-128 ~ 127]
short有符号的16位整数, 范围: [-32768 ~ 32767]
integer有符号的32位整数, 范围: [−231−231 ~ 231231-1]
long有符号的64位整数, 范围: [−263−263 ~ 263263-1]
float32位单精度浮点数
double64位双精度浮点数
half_float16位半精度IEEE 754浮点类型
scaled_float缩放类型的的浮点数, 比如price字段只需精确到分, 57.34缩放因子为100, 存储结果为5734

尽可能选择范围小的数据类型, 字段的长度越短, 索引和搜索的效率越高。示例:

  1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "number_of_bytes": {
  7. "type": "integer"
  8. },
  9. "time_in_seconds": {
  10. "type": "float"
  11. },
  12. "price": {
  13. "type": "scaled_float",
  14. "scaling_factor": 100
  15. }
  16. }
  17. }
  18. }
  19. }
  20. '

3、boolean、binary类型

1)boolean类型:

可以使用boolean类型的(true、false)也可以使用string类型的(“true”、“false”)

  1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "is_published": {
  7. "type": "boolean"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. '
  14. #插入
  15. curl -X POST "localhost:9200/my_index/my_type/1?pretty" -H 'Content-Type: application/json' -d'
  16. {
  17. "is_published": "true"
  18. }
  19. '
  20. #查询
  21. curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
  22. {
  23. "query": {
  24. "term": {
  25. "is_published": true
  26. }
  27. }
  28. }
  29. '

2)binary类型:

二进制类型是Base64编码字符串的二进制值,不以默认的方式存储,且不能被搜索。

  1. #添加映射
  2. PUT website
  3. {
  4. "mappings": {
  5. "blog": {
  6. "properties": {
  7. "blob": {"type": "binary"} // 二进制
  8. }
  9. }
  10. }
  11. }
  12. #添加数据
  13. PUT website/blog/1
  14. {
  15. "title": "Some binary blog",
  16. "blob": "hED903KSrA084fRiD5JLgY=="
  17. }

4、日期类型:

JSON没有日期数据类型, 所以在ES中, 日期可以是:

  • 包含格式化日期的字符串, "2018-10-01", 或"2018/10/01 12:10:30".
  • 代表时间毫秒数的长整型数字.
  • 代表时间秒数的整数.

1)使用format指定格式:

若未指定格式,则使用默认格式: strict_date_optional_time||epoch_millis

  1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "date": {
  7. "type": "date"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. '
  14. curl -X PUT "localhost:9200/my_index/my_type/1?pretty" -H 'Content-Type: application/json' -d'
  15. { "date": "2015-01-01" }
  16. '
  17. curl -X PUT "localhost:9200/my_index/my_type/2?pretty" -H 'Content-Type: application/json' -d'
  18. { "date": "2015-01-01T12:10:30Z" }
  19. '
  20. curl -X PUT "localhost:9200/my_index/my_type/3?pretty" -H 'Content-Type: application/json' -d'
  21. { "date": 1420070400001 }
  22. '
  23. curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
  24. {
  25. "sort": { "date": "asc"}
  26. }
  27. '

2)指定多个format:

使用双竖线||分隔指定多种日期格式,每个格式都会被依次尝试,直到找到匹配的。

  1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "date": {
  7. "type": "date",
  8. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  9. }
  10. }
  11. }
  12. }
  13. }
  14. '

5、范围类型:

类型范围
integer_range−231−231 ~ 231−1231−1
long_range−263−263 ~ 263−1263−1
float_range32位单精度浮点型
double_range64位双精度浮点型
date_range64位整数, 毫秒计时
ip_rangeIP值的范围, 支持IPV4和IPV6, 或者这两种同时存在
  1. #添加映射
  2. PUT company
  3. {
  4. "mappings": {
  5. "department": {
  6. "properties": {
  7. "expected_number": { // 预期员工数
  8. "type": "integer_range"
  9. },
  10. "time_frame": { // 发展时间线
  11. "type": "date_range",
  12. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. #插入数据
  19. PUT company/department/1
  20. {
  21. "expected_number" : {
  22. "gte" : 10,
  23. "lte" : 20
  24. },
  25. "time_frame" : {
  26. "gte" : "2018-10-01 12:00:00",
  27. "lte" : "2018-11-01"
  28. }
  29. }
  30. #查询
  31. GET company/department/_search
  32. {
  33. "query": {
  34. "term": {
  35. "expected_number": {
  36. "value": 12
  37. }
  38. }
  39. }
  40. }
  41. GET company/department/_search
  42. {
  43. "query": {
  44. "range": {
  45. "time_frame": {
  46. "gte": "208-08-01",
  47. "lte": "2018-12-01",
  48. "relation": "within"
  49. }
  50. }
  51. }
  52. }
  53. #查询结果
  54. {
  55. "took": 26,
  56. "timed_out": false,
  57. "_shards": {
  58. "total": 5,
  59. "successful": 5,
  60. "skipped": 0,
  61. "failed": 0
  62. },
  63. "hits": {
  64. "total": 1,
  65. "max_score": 1.0,
  66. "hits": [
  67. {
  68. "_index": "company",
  69. "_type": "department",
  70. "_id": "1",
  71. "_score": 1.0,
  72. "_source": {
  73. "expected_number": {
  74. "gte": 10,
  75. "lte": 20
  76. },
  77. "time_frame": {
  78. "gte": "2018-10-01 12:00:00",
  79. "lte": "2018-11-01"
  80. },
  81. "ip_whitelist" : "192.168.0.0/16"
  82. }
  83. }
  84. ]
  85. }
  86. }

复杂数据类型

es支持复杂的数据类型,包括:object、array、nested。用的不是很多,举个实例:

  1. #创建mapping
  2. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
  3. {
  4. "mappings": {
  5. "my_type": {
  6. "properties": {
  7. "region": {
  8. "type": "keyword"
  9. },
  10. "manager": {
  11. "properties": {
  12. "age": { "type": "integer" },
  13. "name": {
  14. "properties": {
  15. "first": { "type": "text" },
  16. "last": { "type": "text" }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. '
  26. #插入数据
  27. curl -X PUT "localhost:9200/my_index/my_type/1?pretty" -H 'Content-Type: application/json' -d'
  28. {
  29. "region": "US",
  30. "manager": {
  31. "age": 30,
  32. "name": {
  33. "first": "John",
  34. "last": "Smith"
  35. }
  36. }
  37. }
  38. '

 存储方式:

  1. {
  2. "region": "US",
  3. "manager.age": 30,
  4. "manager.name.first": "John",
  5. "manager.name.last": "Smith"
  6. }

地理位置数据

1、geo point类型:

geo point类型用于存储地理位置的经纬度对,可用于:

  • 查找一定范围内的地理点;
  • 通过地理位置或相对某个中心点的距离聚合文档;
  • 将距离整合到文档的相关性评分中;
  • 通过距离对文档进行排序。
  1. #mapping
  2. PUT employee
  3. {
  4. "mappings": {
  5. "developer": {
  6. "properties": {
  7. "location": {"type": "geo_point"}
  8. }
  9. }
  10. }
  11. }
  12. # 方式一: 纬度 + 经度键值对
  13. PUT employee/developer/1
  14. {
  15. "text": "小蛮腰-键值对地理点参数",
  16. "location": {
  17. "lat": 23.11, "lon": 113.33 // 纬度: latitude, 经度: longitude
  18. }
  19. }
  20. # 方式二: "纬度, 经度"的字符串参数
  21. PUT employee/developer/2
  22. {
  23. "text": "小蛮腰-字符串地理点参数",
  24. "location": "23.11, 113.33" // 纬度, 经度
  25. }
  26. # 方式三: ["经度, 纬度"] 数组地理点参数
  27. PUT employee/developer/3
  28. {
  29. "text": "小蛮腰-数组参数",
  30. "location": [ 113.33, 23.11 ] // 经度, 纬度
  31. }
  32. #查询
  33. GET employee/_search
  34. {
  35. "query": {
  36. "geo_bounding_box": {
  37. "location": {
  38. "top_left": { "lat": 24, "lon": 113 }, // 地理盒子模型的上-左边
  39. "bottom_right": { "lat": 22, "lon": 114 } // 地理盒子模型的下-右边
  40. }
  41. }
  42. }
  43. }

2、geo shape类型:

使用较少,可参考:https://blog.csdn.net/u012332735/article/details/54971638

参考:

https://www.cnblogs.com/shoufeng/p/10692113.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/357149
推荐阅读
相关标签
  

闽ICP备14008679号