create-index.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. [[indices-create-index]]
  2. == Create Index
  3. The create index API allows to instantiate an index. Elasticsearch
  4. provides support for multiple indices, including executing operations
  5. across several indices.
  6. [float]
  7. [[create-index-settings]]
  8. === Index Settings
  9. Each index created can have specific settings
  10. associated with it.
  11. [source,js]
  12. --------------------------------------------------
  13. PUT twitter
  14. {
  15. "settings" : {
  16. "index" : {
  17. "number_of_shards" : 3, <1>
  18. "number_of_replicas" : 2 <2>
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. <1> Default for `number_of_shards` is 5
  25. <2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
  26. The above second curl example shows how an index called `twitter` can be
  27. created with specific settings for it using http://www.yaml.org[YAML].
  28. In this case, creating an index with 3 shards, each with 2 replicas. The
  29. index settings can also be defined with http://www.json.org[JSON]:
  30. [source,js]
  31. --------------------------------------------------
  32. PUT twitter
  33. {
  34. "settings" : {
  35. "index" : {
  36. "number_of_shards" : 3,
  37. "number_of_replicas" : 2
  38. }
  39. }
  40. }
  41. --------------------------------------------------
  42. // CONSOLE
  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 to provide a type mapping:
  65. [source,js]
  66. --------------------------------------------------
  67. PUT test
  68. {
  69. "settings" : {
  70. "number_of_shards" : 1
  71. },
  72. "mappings" : {
  73. "type1" : {
  74. "properties" : {
  75. "field1" : { "type" : "text" }
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. [float]
  83. [[create-index-aliases]]
  84. === Aliases
  85. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  86. [source,js]
  87. --------------------------------------------------
  88. PUT test
  89. {
  90. "aliases" : {
  91. "alias_1" : {},
  92. "alias_2" : {
  93. "filter" : {
  94. "term" : {"user" : "kimchy" }
  95. },
  96. "routing" : "kimchy"
  97. }
  98. }
  99. }
  100. --------------------------------------------------
  101. // CONSOLE
  102. [float]
  103. [[create-index-wait-for-active-shards]]
  104. === Wait For Active Shards
  105. By default, index creation will only return a response to the client when the primary copies of
  106. each shard have been started, or the request times out. The index creation response will indicate
  107. what happened:
  108. [source,js]
  109. --------------------------------------------------
  110. {
  111. "acknowledged": true,
  112. "shards_acknowledged": true,
  113. "index": "test"
  114. }
  115. --------------------------------------------------
  116. // TESTRESPONSE
  117. `acknowledged` indicates whether the index was successfully created in the cluster, while
  118. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  119. each shard in the index before timing out. Note that it is still possible for either
  120. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  121. These values simply indicate whether the operation completed before the timeout. If
  122. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  123. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  124. is `false`, then we timed out before the requisite number of shards were started (by default
  125. just the primaries), even if the cluster state was successfully updated to reflect the newly
  126. created index (i.e. `acknowledged=true`).
  127. We can change the default of only waiting for the primary shards to start through the index
  128. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  129. the `wait_for_active_shards` value on all subsequent write operations):
  130. [source,js]
  131. --------------------------------------------------
  132. PUT test
  133. {
  134. "settings": {
  135. "index.write.wait_for_active_shards": "2"
  136. }
  137. }
  138. --------------------------------------------------
  139. // CONSOLE
  140. // TEST[skip:requires two nodes]
  141. or through the request parameter `wait_for_active_shards`:
  142. [source,js]
  143. --------------------------------------------------
  144. PUT test?wait_for_active_shards=2
  145. --------------------------------------------------
  146. // CONSOLE
  147. // TEST[skip:requires two nodes]
  148. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  149. <<index-wait-for-active-shards,here>>.