explicit-mapping.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. [[explicit-mapping]]
  2. == Explicit mapping
  3. You know more about your data than {es} can guess, so while dynamic
  4. mapping can be useful to get started, at some point you will want to specify
  5. your own explicit mappings.
  6. You can create field mappings when you <<create-mapping,create an index>> and
  7. <<add-field-mapping,add fields to an existing index>>.
  8. [discrete]
  9. [[create-mapping]]
  10. === Create an index with an explicit mapping
  11. You can use the <<indices-create-index,create index>> API to create a new index
  12. with an explicit mapping.
  13. [source,console]
  14. ----
  15. PUT /my-index-000001
  16. {
  17. "mappings": {
  18. "properties": {
  19. "age": { "type": "integer" }, <1>
  20. "email": { "type": "keyword" }, <2>
  21. "name": { "type": "text" } <3>
  22. }
  23. }
  24. }
  25. ----
  26. <1> Creates `age`, an <<number,`integer`>> field
  27. <2> Creates `email`, a <<keyword,`keyword`>> field
  28. <3> Creates `name`, a <<text,`text`>> field
  29. [discrete]
  30. [[add-field-mapping]]
  31. === Add a field to an existing mapping
  32. You can use the <<indices-put-mapping, update mapping>> API to add one or more new
  33. fields to an existing index.
  34. The following example adds `employee-id`, a `keyword` field with an
  35. <<mapping-index,`index`>> mapping parameter value of `false`. This means values
  36. for the `employee-id` field are stored but not indexed or available for search.
  37. [source,console]
  38. ----
  39. PUT /my-index-000001/_mapping
  40. {
  41. "properties": {
  42. "employee-id": {
  43. "type": "keyword",
  44. "index": false
  45. }
  46. }
  47. }
  48. ----
  49. // TEST[continued]
  50. [discrete]
  51. [[update-mapping]]
  52. === Update the mapping of a field
  53. include::{es-ref-dir}/indices/put-mapping.asciidoc[tag=change-field-mapping]
  54. include::{es-ref-dir}/indices/put-mapping.asciidoc[tag=rename-field]
  55. [discrete]
  56. [[view-mapping]]
  57. === View the mapping of an index
  58. You can use the <<indices-get-mapping, get mapping>> API to view the mapping of
  59. an existing index.
  60. [source,console]
  61. ----
  62. GET /my-index-000001/_mapping
  63. ----
  64. // TEST[continued]
  65. The API returns the following response:
  66. [source,console-result]
  67. ----
  68. {
  69. "my-index-000001" : {
  70. "mappings" : {
  71. "properties" : {
  72. "age" : {
  73. "type" : "integer"
  74. },
  75. "email" : {
  76. "type" : "keyword"
  77. },
  78. "employee-id" : {
  79. "type" : "keyword",
  80. "index" : false
  81. },
  82. "name" : {
  83. "type" : "text"
  84. }
  85. }
  86. }
  87. }
  88. }
  89. ----
  90. [discrete]
  91. [[view-field-mapping]]
  92. === View the mapping of specific fields
  93. If you only want to view the mapping of one or more specific fields, you can use
  94. the <<indices-get-field-mapping, get field mapping>> API.
  95. This is useful if you don't need the complete mapping of an index or your index
  96. contains a large number of fields.
  97. The following request retrieves the mapping for the `employee-id` field.
  98. [source,console]
  99. ----
  100. GET /my-index-000001/_mapping/field/employee-id
  101. ----
  102. // TEST[continued]
  103. The API returns the following response:
  104. [source,console-result]
  105. ----
  106. {
  107. "my-index-000001" : {
  108. "mappings" : {
  109. "employee-id" : {
  110. "full_name" : "employee-id",
  111. "mapping" : {
  112. "employee-id" : {
  113. "type" : "keyword",
  114. "index" : false
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. ----