1. #dynamic mapping,推断字段的类型
    2. PUT mapping_test/_doc/1
    3. {
    4. "uid" : "123",
    5. "isVip" : false,
    6. "isAdmin": "true",
    7. "age":19,
    8. "heigh":180
    9. }
    10. #####返回结果
    11. {
    12. "_index" : "mapping_test",
    13. "_type" : "_doc",
    14. "_id" : "1",
    15. "_version" : 1,
    16. "result" : "created",
    17. "_shards" : {
    18. "total" : 2,
    19. "successful" : 1,
    20. "failed" : 0
    21. },
    22. "_seq_no" : 0,
    23. "_primary_term" : 1
    24. }
    25. #查看 mapping
    26. GET mapping_test/_mapping
    27. #####查询结果
    28. {
    29. "mapping_test" : {
    30. "mappings" : {
    31. "properties" : {
    32. "age" : {
    33. "type" : "long"
    34. },
    35. "heigh" : {
    36. "type" : "long"
    37. },
    38. "isAdmin" : {
    39. "type" : "text",
    40. "fields" : {
    41. "keyword" : {
    42. "type" : "keyword",
    43. "ignore_above" : 256
    44. }
    45. }
    46. },
    47. "isVip" : {
    48. "type" : "boolean"
    49. },
    50. "uid" : {
    51. "type" : "text",
    52. "fields" : {
    53. "keyword" : {
    54. "type" : "keyword",
    55. "ignore_above" : 256
    56. }
    57. }
    58. }
    59. }
    60. }
    61. }
    62. }
    63. #默认Mapping支持dynamic,写入的文档中加入新的字段
    64. PUT mapping_test/_doc/1
    65. {
    66. "newField":"someValue"
    67. }
    68. #####返回结果
    69. {
    70. "_index" : "mapping_test",
    71. "_type" : "_doc",
    72. "_id" : "1",
    73. "_version" : 2,
    74. "result" : "updated",
    75. "_shards" : {
    76. "total" : 2,
    77. "successful" : 1,
    78. "failed" : 0
    79. },
    80. "_seq_no" : 1,
    81. "_primary_term" : 1
    82. }
    83. #查看 mapping, newField字段已被添加到mapping
    84. GET mapping_test/_mapping
    85. #该字段可以被搜索,数据也在_source中出现
    86. POST mapping_test/_search
    87. {
    88. "query":{
    89. "match":{
    90. "newField":"someValue"
    91. }
    92. }
    93. }
    94. #####返回结果
    95. {
    96. "took" : 480,
    97. "timed_out" : false,
    98. "_shards" : {
    99. "total" : 1,
    100. "successful" : 1,
    101. "skipped" : 0,
    102. "failed" : 0
    103. },
    104. "hits" : {
    105. "total" : {
    106. "value" : 1,
    107. "relation" : "eq"
    108. },
    109. "max_score" : 0.2876821,
    110. "hits" : [
    111. {
    112. "_index" : "mapping_test",
    113. "_type" : "_doc",
    114. "_id" : "1",
    115. "_score" : 0.2876821,
    116. "_source" : {
    117. "newField" : "someValue"
    118. }
    119. }
    120. ]
    121. }
    122. }
    123. #修改为dynamic false
    124. PUT mapping_test/_mapping
    125. {
    126. "dynamic": false
    127. }
    128. #新增 anotherField
    129. PUT mapping_test/_doc/10
    130. {
    131. "anotherField":"someValue"
    132. }
    133. #####返回结果
    134. {
    135. "_index" : "mapping_test",
    136. "_type" : "_doc",
    137. "_id" : "10",
    138. "_version" : 1,
    139. "result" : "created",
    140. "_shards" : {
    141. "total" : 2,
    142. "successful" : 1,
    143. "failed" : 0
    144. },
    145. "_seq_no" : 2,
    146. "_primary_term" : 1
    147. }
    148. #该字段不可以被搜索,因为dynamic已经被设置为false
    149. POST mapping_test/_search
    150. {
    151. "query":{
    152. "match":{
    153. "anotherField":"someValue"
    154. }
    155. }
    156. }
    157. #####返回结果
    158. {
    159. "took" : 303,
    160. "timed_out" : false,
    161. "_shards" : {
    162. "total" : 1,
    163. "successful" : 1,
    164. "skipped" : 0,
    165. "failed" : 0
    166. },
    167. "hits" : {
    168. "total" : {
    169. "value" : 0,
    170. "relation" : "eq"
    171. },
    172. "max_score" : null,
    173. "hits" : [ ]
    174. }
    175. }
    176. #只能通过id获得文档_source看到内容
    177. GET mapping_test/_doc/10
    178. #####返回结果
    179. {
    180. "_index" : "mapping_test",
    181. "_type" : "_doc",
    182. "_id" : "10",
    183. "_version" : 1,
    184. "_seq_no" : 2,
    185. "_primary_term" : 1,
    186. "found" : true,
    187. "_source" : {
    188. "anotherField" : "someValue"
    189. }
    190. }
    191. #修改为strict
    192. PUT mapping_test/_mapping
    193. {
    194. "dynamic": "strict"
    195. }
    196. #写入数据出错,HTTP Code 400
    197. PUT mapping_test/_doc/12
    198. {
    199. "lastField":"value"
    200. }
    201. #####返回结果
    202. {
    203. "error": {
    204. "root_cause": [
    205. {
    206. "type": "strict_dynamic_mapping_exception",
    207. "reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
    208. }
    209. ],
    210. "type": "strict_dynamic_mapping_exception",
    211. "reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
    212. },
    213. "status": 400
    214. }
    215. DELETE mapping_test