Mapping 映射

映射是定义文档的存储和索引方式的过程。
es的mapping有点类型EDB的表结构,在mysql中表结构中有字段名称,类型、长度、索引等
在mapping中也存在字段名称、字段类型,分词器、评分、索引的等。

es支持的数据类型

常见类型

  1. 数字类型
    1. long
    2. integer
    3. short
    4. byte
    5. double
    6. float
    7. half_float
    8. scaled_float
    9. unsingned_float
  2. keywords
    1. keyword:适用于索引结构化的字段,只能被精准匹配(exact_value),id为keyword
    2. constant_keyword:始终包含相同值的关键字类型
    3. wildcard
  3. Dates:时间类型
  4. alias:别名
  5. binary:二进制
  6. range:区间了欧系
  7. text:全文检索字段,生成倒排索引(是为了做文本检索),一般不创建正排索引用于排序和聚合。

    对象关系类型

  8. object:单个json对象

  9. nested:json数组
  10. flattened:将json对象索引成单个字段

结构化类型:

  1. geo_point:经纬度积分
  2. geo_shape:多边形等复杂形状
  3. point:笛卡尔坐标系
  4. shape:笛卡尔任意几何图形

特殊类型

  1. ip:用于ipv4和IPV6
  2. completion

image.png

array数组

es中数组不需要专门的数据类型,任何字段都可以包含一个或者多个值,但是类型需要相同

新增

date_nanos:date plus 纳秒
features

两种映射类型

  1. dynamic field mapping 自动映射
    1. 整数 :long
    2. 浮点性:float
    3. true/false : boolean
    4. 日期:date
    5. 数组:取决于数组中的类型
    6. 对象:object
    7. 字符串:不是数字和日期那么是text和keyword类型
  2. expilcit field mapping 手动映射(除了以上都是需要手动的)

以下相当于手动建立表结构

  1. PUT /product
  2. {
  3. "mapping":{
  4. "properties":{
  5. "field":{
  6. "mapping_parameter": "parameter_value"
  7. }
  8. }
  9. }
  10. }

映射参数

  1. index:是否对当前字段创建倒排索引,默认为true
  2. analyzer:分词器
  3. boost:评分权重
  4. coerce:是否允许类型转换
  5. copy_to:可以将多个字段的值复制到组字段中
  6. doc_value:可以提升排序和聚合的效率 ,如果不需要建立正排索引可以禁用,以节省磁盘空间。
  7. dynamic:控制是否可以动态的创建新字段

image.png
image.png

  1. #Dynamic mapping
  2. DELETE product_mapping
  3. GET product_mapping/_mapping
  4. PUT /product_mapping/_doc/1
  5. {
  6. "name": "xiaomi phone",
  7. "desc": "shouji zhong de zhandouji",
  8. "count": 123456,
  9. "price": 123.123,
  10. "date": "2020-05-20",
  11. "isdel": false,
  12. "tags": [
  13. "xingjiabi",
  14. "fashao",
  15. "buka"
  16. ]
  17. }
  18. #手工创建mapping(fields的mapping只能创建,无法修改)
  19. #语法
  20. GET product/_mapping
  21. PUT /product
  22. {
  23. "mappings": {
  24. "properties": {
  25. "date": {
  26. "type": "text"
  27. }
  28. }
  29. }
  30. }
  31. GET product/_mapping
  32. #1 index
  33. #案例
  34. PUT /product
  35. {
  36. "mappings": {
  37. "properties": {
  38. "date": {
  39. "type": "text"
  40. },
  41. "desc": {
  42. "type": "text",
  43. "analyzer": "english"
  44. },
  45. "name": {
  46. "type": "text",
  47. "index": "false"
  48. },
  49. "price": {
  50. "type": "long"
  51. },
  52. "tags": {
  53. "type": "text",
  54. "index": "true"
  55. },
  56. "parts": {
  57. "type": "object"
  58. },
  59. "partlist": {
  60. "type": "nested"
  61. }
  62. }
  63. }
  64. }
  65. #插入数据
  66. GET product/_mapping
  67. PUT /product/_doc/1
  68. {
  69. "name": "xiaomi phone",
  70. "desc": "shouji zhong de zhandouji",
  71. "count": 123456,
  72. "price": 3999,
  73. "date": "2020-05-20",
  74. "isdel": false,
  75. "tags": [
  76. "xingjiabi",
  77. "fashao",
  78. "buka"
  79. ],
  80. "parts": {
  81. "name": "adapter",
  82. "desc": "5V 2A"
  83. },
  84. "partlist": [
  85. {
  86. "name": "adapter",
  87. "desc": "5V 2A"
  88. },
  89. {
  90. "name": "USB-C",
  91. "desc": "5V 2A 1.5m"
  92. },
  93. {
  94. "name": "erji",
  95. "desc": "boom"
  96. }
  97. ]
  98. }
  99. #查看
  100. GET /product/_search
  101. {
  102. "query": {
  103. "match_all": {}
  104. }
  105. }
  106. #验证
  107. GET /product/_search
  108. {
  109. "query": {
  110. "match": {
  111. "name": "xiaomi"
  112. }
  113. }
  114. }
  115. #copy_to
  116. PUT copy_to
  117. {
  118. "mappings": {
  119. "properties": {
  120. "field1": {
  121. "type": "text",
  122. "copy_to": "field_all"
  123. },
  124. "field2": {
  125. "type": "text",
  126. "copy_to": "field_all"
  127. },
  128. "field_all": {
  129. "type": "text"
  130. }
  131. }
  132. }
  133. }
  134. PUT copy_to/_doc/1
  135. {
  136. "field1": "field1",
  137. "field2": "field2"
  138. }
  139. GET copy_to/_search
  140. GET copy_to/_search
  141. {
  142. "query": {
  143. "match": {
  144. "field_all": {
  145. "query": "field1 field2"
  146. }
  147. }
  148. }
  149. }
  150. #coerce:是否允许强制类型转换
  151. PUT coerce
  152. {
  153. "mappings": {
  154. "properties": {
  155. "number_one": {
  156. "type": "integer"
  157. },
  158. "number_two": {
  159. "type": "integer",
  160. "coerce": false
  161. }
  162. }
  163. }
  164. }
  165. PUT coerce/_doc/1
  166. {
  167. "number_one": "10"
  168. }
  169. #//拒绝,因为设置了false
  170. PUT coerce/_doc/2
  171. {
  172. "number_two": "10"
  173. }
  174. DELETE coerce
  175. PUT coerce
  176. {
  177. "settings": {
  178. "index.mapping.coerce": false
  179. },
  180. "mappings": {
  181. "properties": {
  182. "number_one": {
  183. "type": "integer",
  184. "coerce": true
  185. },
  186. "number_two": {
  187. "type": "integer"
  188. }
  189. }
  190. }
  191. }
  192. PUT coerce/_doc/1
  193. {
  194. "number_one": "10"
  195. }
  196. #拒绝,因为设置了false
  197. PUT coerce/_doc/2
  198. {
  199. "number_two": "10"
  200. }
  201. PUT /product/_mapping
  202. {
  203. "properties": {
  204. "date": {
  205. "type": "text"
  206. }
  207. }
  208. }
  209. #7- 7
  210. PUT dynamic
  211. {
  212. "mappings": {
  213. "dynamic": false,
  214. "properties": {
  215. "user": {
  216. "properties": {
  217. "date": {
  218. "type": "text"
  219. },
  220. "desc": {
  221. "type": "text",
  222. "analyzer": "english"
  223. },
  224. "name": {
  225. "type": "text",
  226. "index": "false"
  227. },
  228. "price": {
  229. "type": "long"
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. PUT /product/_mapping
  237. {
  238. "properties": {
  239. "date": {
  240. "type": "text"
  241. }
  242. }
  243. }
  244. #7-11
  245. GET /product/_mapping
  246. #给city创建一个keyword
  247. PUT fields_test
  248. {
  249. "mappings": {
  250. "properties": {
  251. "city": {
  252. "type": "text",
  253. "fields": {
  254. "raw": {
  255. "type": "keyword"
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. PUT fields_test/_doc/1
  263. {
  264. "city": "New York"
  265. }
  266. PUT fields_test/_doc/2
  267. {
  268. "city": "York"
  269. }
  270. GET fields_test/_mapping
  271. GET fields_test/_search
  272. {
  273. "query": {
  274. "match": {
  275. "city": "york"
  276. }
  277. },
  278. "sort": {
  279. "city.raw": "asc"
  280. },
  281. "aggs": {
  282. "cities": {
  283. "terms": {
  284. "field": "city.raw"
  285. }
  286. }
  287. }
  288. }
  289. #忽略类型错误-常用于数据同步
  290. PUT ignore_malformed
  291. {
  292. "mappings": {
  293. "properties": {
  294. "number_one": {
  295. "type": "integer",
  296. "ignore_malformed": true
  297. },
  298. "number_two": {
  299. "type": "integer"
  300. }
  301. }
  302. }}
  303. PUT ignore_malformed/_doc/1
  304. {
  305. "text": "Some text value",
  306. "number_one": "foo"
  307. }
  308. #//虽然有异常 但是不抛出
  309. PUT ignore_malformed/_doc/2
  310. {
  311. "text": "Some text value",
  312. "number_two": "foo"
  313. }
  314. GET my_index/_search
  315. #//数据格式不对
  316. #fielddata
  317. #每个tag产品的数量 "size":0, 不显示原始结果
  318. GET /product/_search
  319. {
  320. "aggs": {
  321. "tag_agg_group": {
  322. "terms": {
  323. "field": "tags"
  324. }
  325. }
  326. },
  327. "size":0
  328. }
  329. GET /product/_mapping
  330. #将文本field的fielddata属性设置为true
  331. PUT /product/_mapping
  332. {
  333. "properties": {
  334. "tags": {
  335. "type": "text",
  336. "fielddata": true
  337. }
  338. }
  339. }