create-index.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $ curl -XPUT 'http://localhost:9200/twitter/' -d '{
  14. "settings" : {
  15. "index" : {
  16. "number_of_shards" : 3, <1>
  17. "number_of_replicas" : 2 <2>
  18. }
  19. }
  20. }'
  21. --------------------------------------------------
  22. <1> Default for `number_of_shards` is 5
  23. <2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
  24. The above second curl example shows how an index called `twitter` can be
  25. created with specific settings for it using http://www.yaml.org[YAML].
  26. In this case, creating an index with 3 shards, each with 2 replicas. The
  27. index settings can also be defined with http://www.json.org[JSON]:
  28. [source,js]
  29. --------------------------------------------------
  30. $ curl -XPUT 'http://localhost:9200/twitter/' -d '{
  31. "settings" : {
  32. "index" : {
  33. "number_of_shards" : 3,
  34. "number_of_replicas" : 2
  35. }
  36. }
  37. }'
  38. --------------------------------------------------
  39. or more simplified
  40. [source,js]
  41. --------------------------------------------------
  42. $ curl -XPUT 'http://localhost:9200/twitter/' -d '{
  43. "settings" : {
  44. "number_of_shards" : 3,
  45. "number_of_replicas" : 2
  46. }
  47. }'
  48. --------------------------------------------------
  49. [NOTE]
  50. You do not have to explicitly specify `index` section inside the
  51. `settings` section.
  52. For more information regarding all the different index level settings
  53. that can be set when creating an index, please check the
  54. <<index-modules,index modules>> section.
  55. [float]
  56. [[mappings]]
  57. === Mappings
  58. The create index API allows to provide a set of one or more mappings:
  59. [source,js]
  60. --------------------------------------------------
  61. curl -XPOST localhost:9200/test -d '{
  62. "settings" : {
  63. "number_of_shards" : 1
  64. },
  65. "mappings" : {
  66. "type1" : {
  67. "properties" : {
  68. "field1" : { "type" : "text" }
  69. }
  70. }
  71. }
  72. }'
  73. --------------------------------------------------
  74. [float]
  75. [[create-index-aliases]]
  76. === Aliases
  77. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  78. [source,js]
  79. --------------------------------------------------
  80. curl -XPUT localhost:9200/test -d '{
  81. "aliases" : {
  82. "alias_1" : {},
  83. "alias_2" : {
  84. "filter" : {
  85. "term" : {"user" : "kimchy" }
  86. },
  87. "routing" : "kimchy"
  88. }
  89. }
  90. }'
  91. --------------------------------------------------
  92. [float]
  93. === Creation Date
  94. When an index is created, a timestamp is stored in the index metadata for the creation date. By
  95. default this is automatically generated but it can also be specified using the
  96. `creation_date` parameter on the create index API:
  97. [source,js]
  98. --------------------------------------------------
  99. curl -XPUT localhost:9200/test -d '{
  100. "creation_date" : 1407751337000 <1>
  101. }'
  102. --------------------------------------------------
  103. <1> `creation_date` is set using epoch time in milliseconds.
  104. [float]
  105. [[create-index-wait-for-active-shards]]
  106. === Wait For Active Shards
  107. By default, index creation will only return a response to the client when the primary copies of
  108. each shard have been started, or the request times out. The index creation response will indicate
  109. what happened:
  110. [source,js]
  111. --------------------------------------------------
  112. {
  113. "acknowledged": true,
  114. "shards_acknowledged": true
  115. }
  116. --------------------------------------------------
  117. `acknowledged` indicates whether the index was successfully created in the cluster, while
  118. `shards_acknowledged` indices 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. curl -XPUT localhost:9200/test -d '{
  133. "settings": {
  134. "index.write.wait_for_active_shards": "2"
  135. }
  136. }
  137. --------------------------------------------------
  138. or through the request parameter `wait_for_active_shards`:
  139. [source,js]
  140. --------------------------------------------------
  141. curl -XPUT localhost:9200/test?wait_for_active_shards=2
  142. --------------------------------------------------
  143. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  144. <<index-wait-for-active-shards,here>>.