Mapping 是定义文档及其包含的字段是如何存储和索引的过程
例如,我们用映射来定义:

  • 哪些字符串字段应该被当做全文字段
  • 哪些字段包含数字、日期或地理位置
  • 是否应该将文档中所有字段的值索引到catch-all字段中
  • Mapping 相当与MySql 里的Schema

    有用的链接:

    https://www.cnblogs.com/cjsblog/p/10035629.html

    映射类型

    每个索引都有一个映射类型,以决定文档将被如何索引
    映射类型包含两部分:
    Meta-fields
    Meta-fields通常用于自定义文档的元数据。例如,meta-fields包括文档的 _index, _type, _id, _source等字段
    Fields 或 properties
    一个映射类型包含一个字段列表或属性列表
    可以在kibana 里直接查询索引的映射类型

    1. GET {index_name}

    字段数据类型

    每个字段有一个数据类型,它可以是下列之一:

  • 简单类型,比如 text, keyword, date, long, double, boolean , ip

  • 支持JSON层级结构的类型,比如 object 或者 nested
  • 特别的类型,比如 geo_point, geo_shape, completion
  • 所有text 类型都有一个keyword field

以下例子表现了一个完整的映射:

  1. {
  2. "bank" : {
  3. "aliases" : { },
  4. "mappings" : {
  5. "properties" : {
  6. "account_number" : {
  7. "type" : "long"
  8. },
  9. "address" : {
  10. "type" : "text",
  11. "fields" : {
  12. "keyword" : {
  13. "type" : "keyword",
  14. "ignore_above" : 256
  15. }
  16. }
  17. },
  18. "age" : {
  19. "type" : "long"
  20. },
  21. "balance" : {
  22. "type" : "long"
  23. },
  24. "city" : {
  25. "type" : "text",
  26. "fields" : {
  27. "keyword" : {
  28. "type" : "keyword",
  29. "ignore_above" : 256
  30. }
  31. }
  32. },
  33. "email" : {
  34. "type" : "text",
  35. "fields" : {
  36. "keyword" : {
  37. "type" : "keyword",
  38. "ignore_above" : 256
  39. }
  40. }
  41. },
  42. "employer" : {
  43. "type" : "text",
  44. "fields" : {
  45. "keyword" : {
  46. "type" : "keyword",
  47. "ignore_above" : 256
  48. }
  49. }
  50. },
  51. "firstname" : {
  52. "type" : "text",
  53. "fields" : {
  54. "keyword" : {
  55. "type" : "keyword",
  56. "ignore_above" : 256
  57. }
  58. }
  59. },
  60. "gender" : {
  61. "type" : "text",
  62. "fields" : {
  63. "keyword" : {
  64. "type" : "keyword",
  65. "ignore_above" : 256
  66. }
  67. }
  68. },
  69. "lastname" : {
  70. "type" : "text",
  71. "fields" : {
  72. "keyword" : {
  73. "type" : "keyword",
  74. "ignore_above" : 256
  75. }
  76. }
  77. },
  78. "state" : {
  79. "type" : "text",
  80. "fields" : {
  81. "keyword" : {
  82. "type" : "keyword",
  83. "ignore_above" : 256
  84. }
  85. }
  86. }
  87. }
  88. },
  89. "settings" : {
  90. "index" : {
  91. "creation_date" : "1641019815370",
  92. "number_of_shards" : "1",
  93. "number_of_replicas" : "1",
  94. "uuid" : "AGH7EVzpTxGDfO4uN05Adg",
  95. "version" : {
  96. "created" : "7100299"
  97. },
  98. "provided_name" : "bank"
  99. }
  100. }
  101. }
  102. }

Field Datatypes(字段类型)

核心类型:

字符串: text, keyword
日期类型: date
二进制类型:binary
布尔类型 boolean
数值;long , integer , short , byte , double , float , half_float , scaled_float
范围类型: integer_range , float_range , long_range , double_range , date_range

复杂类型:

数组,对象,内嵌

地理类型

geo_point : 用于地理位置经纬度坐标
geo_shape: 用于复杂形状

专门的数据类型

IP类型: ip (用于IPv4和IPv6地址)
Completion类型: completion (用于自动补全提示)
Token count 类型: token_count (用于计数字符串中的token)
mapper-murmur3: murmur3 (计算值的hashcode,并将其存储到索引中)
过滤器类型: 接受一个查询语句
join 类型: 为同一索引内的文档定义父/子关系

元数据字段:

每个文档都有与之关联的元数据:

标识

_index 文档属于哪个索引
_id 文档ID
_type 文档的映射类型
_uid 由 _type和 _id组成的一个组合字段

文档来源:
_source : 文档的原始JSON
_size: source 字段的长度
_all 索引其它字段的值,默认情况下是禁用的
_field_names 所有非空字段
_routing 一个自定义的路由值,用于分片的
_meta 其它

ES keyword 和 text 的区别

text: 存储数据的时候会自动分词并对每个词创建索引,支持模糊, 精确查询,不支持聚合。
keyword: 存储数据的时候,该类型的字段不会分词建立索引,支持模糊,精确查询,支持聚合
term 查询不会对查询词进行分词。所以如果term 查询会查询到keyword 字段的词,不一定会查询到text字段的词。