index_.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. [[docs-index_]]
  2. == Index API
  3. The index API adds or updates a typed JSON document in a specific index,
  4. making it searchable. The following example inserts the JSON document
  5. into the "twitter" index, under a type called "tweet" with an id of 1:
  6. [source,js]
  7. --------------------------------------------------
  8. $ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
  9. "user" : "kimchy",
  10. "post_date" : "2009-11-15T14:12:12",
  11. "message" : "trying out Elasticsearch"
  12. }'
  13. --------------------------------------------------
  14. The result of the above index operation is:
  15. [source,js]
  16. --------------------------------------------------
  17. {
  18. "_index" : "twitter",
  19. "_type" : "tweet",
  20. "_id" : "1",
  21. "_version" : 1,
  22. "created" : true
  23. }
  24. --------------------------------------------------
  25. [float]
  26. [[index-creation]]
  27. === Automatic Index Creation
  28. The index operation automatically creates an index if it has not been
  29. created before (check out the
  30. <<indices-create-index,create index API>> for manually
  31. creating an index), and also automatically creates a
  32. dynamic type mapping for the specific type if one has not yet been
  33. created (check out the <<indices-put-mapping,put mapping>>
  34. API for manually creating a type mapping).
  35. The mapping itself is very flexible and is schema-free. New fields and
  36. objects will automatically be added to the mapping definition of the
  37. type specified. Check out the <<mapping,mapping>>
  38. section for more information on mapping definitions.
  39. Note that the format of the JSON document can also include the type (very handy
  40. when using JSON mappers) if the `index.mapping.allow_type_wrapper` setting is
  41. set to true, for example:
  42. [source,js]
  43. --------------------------------------------------
  44. $ curl -XPOST 'http://localhost:9200/twitter' -d '{
  45. "settings": {
  46. "index": {
  47. "mapping.allow_type_wrapper": true
  48. }
  49. }
  50. }'
  51. {"acknowledged":true}
  52. $ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
  53. "tweet" : {
  54. "user" : "kimchy",
  55. "post_date" : "2009-11-15T14:12:12",
  56. "message" : "trying out Elasticsearch"
  57. }
  58. }'
  59. --------------------------------------------------
  60. Automatic index creation can be disabled by setting
  61. `action.auto_create_index` to `false` in the config file of all nodes.
  62. Automatic mapping creation can be disabled by setting
  63. `index.mapper.dynamic` to `false` in the config files of all nodes (or
  64. on the specific index settings).
  65. Automatic index creation can include a pattern based white/black list,
  66. for example, set `action.auto_create_index` to `+aaa*,-bbb*,+ccc*,-*` (+
  67. meaning allowed, and - meaning disallowed).
  68. [float]
  69. [[index-versioning]]
  70. === Versioning
  71. Each indexed document is given a version number. The associated
  72. `version` number is returned as part of the response to the index API
  73. request. The index API optionally allows for
  74. http://en.wikipedia.org/wiki/Optimistic_concurrency_control[optimistic
  75. concurrency control] when the `version` parameter is specified. This
  76. will control the version of the document the operation is intended to be
  77. executed against. A good example of a use case for versioning is
  78. performing a transactional read-then-update. Specifying a `version` from
  79. the document initially read ensures no changes have happened in the
  80. meantime (when reading in order to update, it is recommended to set
  81. `preference` to `_primary`). For example:
  82. [source,js]
  83. --------------------------------------------------
  84. curl -XPUT 'localhost:9200/twitter/tweet/1?version=2' -d '{
  85. "message" : "elasticsearch now has versioning support, double cool!"
  86. }'
  87. --------------------------------------------------
  88. *NOTE:* versioning is completely real time, and is not affected by the
  89. near real time aspects of search operations. If no version is provided,
  90. then the operation is executed without any version checks.
  91. By default, internal versioning is used that starts at 1 and increments
  92. with each update, deletes included. Optionally, the version number can be
  93. supplemented with an external value (for example, if maintained in a
  94. database). To enable this functionality, `version_type` should be set to
  95. `external`. The value provided must be a numeric, long value greater or equal to 0,
  96. and less than around 9.2e+18. When using the external version type, instead
  97. of checking for a matching version number, the system checks to see if
  98. the version number passed to the index request is greater than the
  99. version of the currently stored document. If true, the document will be
  100. indexed and the new version number used. If the value provided is less
  101. than or equal to the stored document's version number, a version
  102. conflict will occur and the index operation will fail.
  103. A nice side effect is that there is no need to maintain strict ordering
  104. of async indexing operations executed as a result of changes to a source
  105. database, as long as version numbers from the source database are used.
  106. Even the simple case of updating the elasticsearch index using data from
  107. a database is simplified if external versioning is used, as only the
  108. latest version will be used if the index operations are out of order for
  109. whatever reason.
  110. [float]
  111. ==== Version types
  112. Next to the `internal` & `external` version types explained above, Elasticsearch
  113. also supports other types for specific use cases. Here is an overview of
  114. the different version types and their semantics.
  115. `internal`:: only index the document if the given version is identical to the version
  116. of the stored document.
  117. `external` or `external_gt`:: only index the document if the given version is strictly higher
  118. than the version of the stored document *or* if there is no existing document. The given
  119. version will be used as the new version and will be stored with the new document. The supplied
  120. version must be a non-negative long number.
  121. `external_gte`:: only index the document if the given version is *equal* or higher
  122. than the version of the stored document. If there is no existing document
  123. the operation will succeed as well. The given version will be used as the new version
  124. and will be stored with the new document. The supplied version must be a non-negative long number.
  125. `force`:: the document will be indexed regardless of the version of the stored document or if there
  126. is no existing document. The given version will be used as the new version and will be stored
  127. with the new document. This version type is typically used for correcting errors.
  128. *NOTE*: The `external_gte` & `force` version types are meant for special use cases and should be used
  129. with care. If used incorrectly, they can result in loss of data.
  130. [float]
  131. [[operation-type]]
  132. === Operation Type
  133. The index operation also accepts an `op_type` that can be used to force
  134. a `create` operation, allowing for "put-if-absent" behavior. When
  135. `create` is used, the index operation will fail if a document by that id
  136. already exists in the index.
  137. Here is an example of using the `op_type` parameter:
  138. [source,js]
  139. --------------------------------------------------
  140. $ curl -XPUT 'http://localhost:9200/twitter/tweet/1?op_type=create' -d '{
  141. "user" : "kimchy",
  142. "post_date" : "2009-11-15T14:12:12",
  143. "message" : "trying out Elasticsearch"
  144. }'
  145. --------------------------------------------------
  146. Another option to specify `create` is to use the following uri:
  147. [source,js]
  148. --------------------------------------------------
  149. $ curl -XPUT 'http://localhost:9200/twitter/tweet/1/_create' -d '{
  150. "user" : "kimchy",
  151. "post_date" : "2009-11-15T14:12:12",
  152. "message" : "trying out Elasticsearch"
  153. }'
  154. --------------------------------------------------
  155. [float]
  156. === Automatic ID Generation
  157. The index operation can be executed without specifying the id. In such a
  158. case, an id will be generated automatically. In addition, the `op_type`
  159. will automatically be set to `create`. Here is an example (note the
  160. *POST* used instead of *PUT*):
  161. [source,js]
  162. --------------------------------------------------
  163. $ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
  164. "user" : "kimchy",
  165. "post_date" : "2009-11-15T14:12:12",
  166. "message" : "trying out Elasticsearch"
  167. }'
  168. --------------------------------------------------
  169. The result of the above index operation is:
  170. [source,js]
  171. --------------------------------------------------
  172. {
  173. "_index" : "twitter",
  174. "_type" : "tweet",
  175. "_id" : "6a8ca01c-7896-48e9-81cc-9f70661fcb32",
  176. "_version" : 1,
  177. "created" : true
  178. }
  179. --------------------------------------------------
  180. [float]
  181. [[index-routing]]
  182. === Routing
  183. By default, shard placement — or `routing` — is controlled by using a
  184. hash of the document's id value. For more explicit control, the value
  185. fed into the hash function used by the router can be directly specified
  186. on a per-operation basis using the `routing` parameter. For example:
  187. [source,js]
  188. --------------------------------------------------
  189. $ curl -XPOST 'http://localhost:9200/twitter/tweet?routing=kimchy' -d '{
  190. "user" : "kimchy",
  191. "post_date" : "2009-11-15T14:12:12",
  192. "message" : "trying out Elasticsearch"
  193. }'
  194. --------------------------------------------------
  195. In the example above, the "tweet" document is routed to a shard based on
  196. the `routing` parameter provided: "kimchy".
  197. When setting up explicit mapping, the `_routing` field can be optionally
  198. used to direct the index operation to extract the routing value from the
  199. document itself. This does come at the (very minimal) cost of an
  200. additional document parsing pass. If the `_routing` mapping is defined,
  201. and set to be `required`, the index operation will fail if no routing
  202. value is provided or extracted.
  203. [float]
  204. [[parent-children]]
  205. === Parents & Children
  206. A child document can be indexed by specifying its parent when indexing.
  207. For example:
  208. [source,js]
  209. --------------------------------------------------
  210. $ curl -XPUT localhost:9200/blogs/blog_tag/1122?parent=1111 -d '{
  211. "tag" : "something"
  212. }'
  213. --------------------------------------------------
  214. When indexing a child document, the routing value is automatically set
  215. to be the same as its parent, unless the routing value is explicitly
  216. specified using the `routing` parameter.
  217. [float]
  218. [[index-timestamp]]
  219. === Timestamp
  220. A document can be indexed with a `timestamp` associated with it. The
  221. `timestamp` value of a document can be set using the `timestamp`
  222. parameter. For example:
  223. [source,js]
  224. --------------------------------------------------
  225. $ curl -XPUT localhost:9200/twitter/tweet/1?timestamp=2009-11-15T14%3A12%3A12 -d '{
  226. "user" : "kimchy",
  227. "message" : "trying out Elasticsearch"
  228. }'
  229. --------------------------------------------------
  230. If the `timestamp` value is not provided externally or in the `_source`,
  231. the `timestamp` will be automatically set to the date the document was
  232. processed by the indexing chain. More information can be found on the
  233. <<mapping-timestamp-field,_timestamp mapping
  234. page>>.
  235. [float]
  236. [[index-ttl]]
  237. === TTL
  238. A document can be indexed with a `ttl` (time to live) associated with
  239. it. Expired documents will be expunged automatically. The expiration
  240. date that will be set for a document with a provided `ttl` is relative
  241. to the `timestamp` of the document, meaning it can be based on the time
  242. of indexing or on any time provided. The provided `ttl` must be strictly
  243. positive and can be a number (in milliseconds) or any valid time value
  244. as shown in the following examples:
  245. [source,js]
  246. --------------------------------------------------
  247. curl -XPUT 'http://localhost:9200/twitter/tweet/1?ttl=86400000' -d '{
  248. "user": "kimchy",
  249. "message": "Trying out elasticsearch, so far so good?"
  250. }'
  251. --------------------------------------------------
  252. [source,js]
  253. --------------------------------------------------
  254. curl -XPUT 'http://localhost:9200/twitter/tweet/1?ttl=1d' -d '{
  255. "user": "kimchy",
  256. "message": "Trying out elasticsearch, so far so good?"
  257. }'
  258. --------------------------------------------------
  259. [source,js]
  260. --------------------------------------------------
  261. curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
  262. "_ttl": "1d",
  263. "user": "kimchy",
  264. "message": "Trying out elasticsearch, so far so good?"
  265. }'
  266. --------------------------------------------------
  267. More information can be found on the
  268. <<mapping-ttl-field,_ttl mapping page>>.
  269. [float]
  270. [[index-distributed]]
  271. === Distributed
  272. The index operation is directed to the primary shard based on its route
  273. (see the Routing section above) and performed on the actual node
  274. containing this shard. After the primary shard completes the operation,
  275. if needed, the update is distributed to applicable replicas.
  276. [float]
  277. [[index-consistency]]
  278. === Write Consistency
  279. To prevent writes from taking place on the "wrong" side of a network
  280. partition, by default, index operations only succeed if a quorum
  281. (>replicas/2+1) of active shards are available. This default can be
  282. overridden on a node-by-node basis using the `action.write_consistency`
  283. setting. To alter this behavior per-operation, the `consistency` request
  284. parameter can be used.
  285. Valid write consistency values are `one`, `quorum`, and `all`.
  286. Note, for the case where the number of replicas is 1 (total of 2 copies
  287. of the data), then the default behavior is to succeed if 1 copy (the primary)
  288. can perform the write.
  289. [float]
  290. [[index-replication]]
  291. === Asynchronous Replication
  292. By default, the index operation only returns after all shards within the
  293. replication group have indexed the document (sync replication). To
  294. enable asynchronous replication, causing the replication process to take
  295. place in the background, set the `replication` parameter to `async`.
  296. When asynchronous replication is used, the index operation will return
  297. as soon as the operation succeeds on the primary shard.
  298. [float]
  299. [[index-refresh]]
  300. === Refresh
  301. To refresh the shard (not the whole index) immediately after the operation
  302. occurs, so that the document appears in search results immediately, the
  303. `refresh` parameter can be set to `true`. Setting this option to `true` should
  304. *ONLY* be done after careful thought and verification that it does not lead to
  305. poor performance, both from an indexing and a search standpoint. Note, getting
  306. a document using the get API is completely realtime.
  307. [float]
  308. [[timeout]]
  309. === Timeout
  310. The primary shard assigned to perform the index operation might not be
  311. available when the index operation is executed. Some reasons for this
  312. might be that the primary shard is currently recovering from a gateway
  313. or undergoing relocation. By default, the index operation will wait on
  314. the primary shard to become available for up to 1 minute before failing
  315. and responding with an error. The `timeout` parameter can be used to
  316. explicitly specify how long it waits. Here is an example of setting it
  317. to 5 minutes:
  318. [source,js]
  319. --------------------------------------------------
  320. $ curl -XPUT 'http://localhost:9200/twitter/tweet/1?timeout=5m' -d '{
  321. "user" : "kimchy",
  322. "post_date" : "2009-11-15T14:12:12",
  323. "message" : "trying out Elasticsearch"
  324. }'
  325. --------------------------------------------------