create-index.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. [[indices-create-index]]
  2. === Create Index
  3. The Create Index API is used to manually create an index in Elasticsearch. All documents in Elasticsearch
  4. are stored inside of one index or another.
  5. The most basic command is the following:
  6. [source,js]
  7. --------------------------------------------------
  8. PUT twitter
  9. --------------------------------------------------
  10. // CONSOLE
  11. This creates an index named `twitter` with all default setting.
  12. [NOTE]
  13. .Index name limitations
  14. ======================================================
  15. There are several limitations to what you can name your index. The complete list of limitations are:
  16. - Lowercase only
  17. - Cannot include `\`, `/`, `*`, `?`, `"`, `<`, `>`, `|`, ` ` (space character), `,`, `#`
  18. - Indices prior to 7.0 could contain a colon (`:`), but that's been deprecated and won't be supported in 7.0+
  19. - Cannot start with `-`, `_`, `+`
  20. - Cannot be `.` or `..`
  21. - Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster)
  22. ======================================================
  23. [float]
  24. [[create-index-settings]]
  25. ==== Index Settings
  26. Each index created can have specific settings
  27. associated with it, defined in the body:
  28. [source,js]
  29. --------------------------------------------------
  30. PUT twitter
  31. {
  32. "settings" : {
  33. "index" : {
  34. "number_of_shards" : 3, <1>
  35. "number_of_replicas" : 2 <2>
  36. }
  37. }
  38. }
  39. --------------------------------------------------
  40. // CONSOLE
  41. <1> Default for `number_of_shards` is 1
  42. <2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
  43. or more simplified
  44. [source,js]
  45. --------------------------------------------------
  46. PUT twitter
  47. {
  48. "settings" : {
  49. "number_of_shards" : 3,
  50. "number_of_replicas" : 2
  51. }
  52. }
  53. --------------------------------------------------
  54. // CONSOLE
  55. [NOTE]
  56. You do not have to explicitly specify `index` section inside the
  57. `settings` section.
  58. For more information regarding all the different index level settings
  59. that can be set when creating an index, please check the
  60. <<index-modules,index modules>> section.
  61. [float]
  62. [[mappings]]
  63. ==== Mappings
  64. The create index API allows for providing a mapping definition:
  65. [source,js]
  66. --------------------------------------------------
  67. PUT test
  68. {
  69. "settings" : {
  70. "number_of_shards" : 1
  71. },
  72. "mappings" : {
  73. "properties" : {
  74. "field1" : { "type" : "text" }
  75. }
  76. }
  77. }
  78. --------------------------------------------------
  79. // CONSOLE
  80. NOTE: Before 7.0.0, the 'mappings' definition used to include a type name. Although specifying
  81. types in requests is now deprecated, a type can still be provided if the request parameter
  82. include_type_name is set. For more details, please see <<removal-of-types>>.
  83. [float]
  84. [[create-index-aliases]]
  85. ==== Aliases
  86. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  87. [source,js]
  88. --------------------------------------------------
  89. PUT test
  90. {
  91. "aliases" : {
  92. "alias_1" : {},
  93. "alias_2" : {
  94. "filter" : {
  95. "term" : {"user" : "kimchy" }
  96. },
  97. "routing" : "kimchy"
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. // CONSOLE
  103. [float]
  104. [[create-index-wait-for-active-shards]]
  105. ==== Wait For Active Shards
  106. By default, index creation will only return a response to the client when the primary copies of
  107. each shard have been started, or the request times out. The index creation response will indicate
  108. what happened:
  109. [source,js]
  110. --------------------------------------------------
  111. {
  112. "acknowledged": true,
  113. "shards_acknowledged": true,
  114. "index": "test"
  115. }
  116. --------------------------------------------------
  117. // TESTRESPONSE
  118. `acknowledged` indicates whether the index was successfully created in the cluster, while
  119. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  120. each shard in the index before timing out. Note that it is still possible for either
  121. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  122. These values simply indicate whether the operation completed before the timeout. If
  123. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  124. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  125. is `false`, then we timed out before the requisite number of shards were started (by default
  126. just the primaries), even if the cluster state was successfully updated to reflect the newly
  127. created index (i.e. `acknowledged=true`).
  128. We can change the default of only waiting for the primary shards to start through the index
  129. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  130. the `wait_for_active_shards` value on all subsequent write operations):
  131. [source,js]
  132. --------------------------------------------------
  133. PUT test
  134. {
  135. "settings": {
  136. "index.write.wait_for_active_shards": "2"
  137. }
  138. }
  139. --------------------------------------------------
  140. // CONSOLE
  141. // TEST[skip:requires two nodes]
  142. or through the request parameter `wait_for_active_shards`:
  143. [source,js]
  144. --------------------------------------------------
  145. PUT test?wait_for_active_shards=2
  146. --------------------------------------------------
  147. // CONSOLE
  148. // TEST[skip:requires two nodes]
  149. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  150. <<index-wait-for-active-shards,here>>.