index_.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. [[docs-index_]]
  2. === Index API
  3. ++++
  4. <titleabbrev>Index</titleabbrev>
  5. ++++
  6. IMPORTANT: See <<removal-of-types>>.
  7. Adds a JSON document to the specified data stream or index and makes
  8. it searchable. If the target is an index and the document already exists,
  9. the request updates the document and increments its version.
  10. NOTE: You cannot use the index API to send update requests for existing
  11. documents to a data stream. See <<update-docs-in-a-data-stream-by-query>>
  12. and <<update-delete-docs-in-a-backing-index>>.
  13. [[docs-index-api-request]]
  14. ==== {api-request-title}
  15. `PUT /<target>/_doc/<_id>`
  16. `POST /<target>/_doc/`
  17. `PUT /<target>/_create/<_id>`
  18. `POST /<target>/_create/<_id>`
  19. IMPORTANT: You cannot add new documents to a data stream using the
  20. `PUT /<target>/_doc/<_id>` request format. To specify a document ID, use the
  21. `PUT /<target>/_create/<_id>` format instead. See
  22. <<add-documents-to-a-data-stream>>.
  23. [[docs-index-api-path-params]]
  24. ==== {api-path-parms-title}
  25. `<target>`::
  26. (Required, string) Name of the data stream or index to target.
  27. +
  28. If the target doesn't exist and matches the name or wildcard (`*`) pattern of an
  29. <<create-a-data-stream-template,index template with a `data_stream`
  30. definition>>, this request creates the data stream. See
  31. <<set-up-a-data-stream>>.
  32. +
  33. If the target doesn't exist and doesn't match a data stream template,
  34. this request creates the index.
  35. +
  36. You can check for existing targets using the resolve index API.
  37. `<_id>`::
  38. (Optional, string) Unique identifier for the document.
  39. +
  40. --
  41. This parameter is required for the following request formats:
  42. * `PUT /<target>/_doc/<_id>`
  43. * `PUT /<target>/_create/<_id>`
  44. * `POST /<target>/_create/<_id>`
  45. To automatically generate a document ID, use the `POST /<target>/_doc/` request
  46. format and omit this parameter.
  47. --
  48. [[docs-index-api-query-params]]
  49. ==== {api-query-parms-title}
  50. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=if_seq_no]
  51. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=if_primary_term]
  52. [[docs-index-api-op_type]]
  53. `op_type`::
  54. (Optional, enum) Set to `create` to only index the document
  55. if it does not already exist (_put if absent_). If a document with the specified
  56. `_id` already exists, the indexing operation will fail. Same as using the
  57. `<index>/_create` endpoint. Valid values: `index`, `create`.
  58. If document id is specified, it defaults to `index`. Otherwise, it defaults to `create`.
  59. +
  60. NOTE: If the request targets a data stream, an `op_type` of `create` is
  61. required. See <<add-documents-to-a-data-stream>>.
  62. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=pipeline]
  63. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=refresh]
  64. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  65. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  66. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=doc-version]
  67. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=version_type]
  68. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  69. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=require-alias]
  70. [[docs-index-api-request-body]]
  71. ==== {api-request-body-title}
  72. `<field>`::
  73. (Required, string) Request body contains the JSON source for the document
  74. data.
  75. [[docs-index-api-response-body]]
  76. ==== {api-response-body-title}
  77. `_shards`::
  78. Provides information about the replication process of the index operation.
  79. `_shards.total`::
  80. Indicates how many shard copies (primary and replica shards) the index operation
  81. should be executed on.
  82. `_shards.successful`::
  83. Indicates the number of shard copies the index operation succeeded on.
  84. When the index operation is successful, `successful` is at least 1.
  85. +
  86. NOTE: Replica shards might not all be started when an indexing operation
  87. returns successfully--by default, only the primary is required. Set
  88. `wait_for_active_shards` to change this default behavior. See
  89. <<index-wait-for-active-shards>>.
  90. `_shards.failed`::
  91. An array that contains replication-related errors in the case an index operation
  92. failed on a replica shard. 0 indicates there were no failures.
  93. `_index`::
  94. The name of the index the document was added to.
  95. `_type`::
  96. The document type. {es} indices now support a single document type, `_doc`.
  97. `_id`::
  98. The unique identifier for the added document.
  99. `_version`::
  100. The document version. Incremented each time the document is updated.
  101. `_seq_no`::
  102. The sequence number assigned to the document for the indexing operation.
  103. Sequence numbers are used to ensure an older version of a document
  104. doesn’t overwrite a newer version. See <<optimistic-concurrency-control-index>>.
  105. `_primary_term`::
  106. The primary term assigned to the document for the indexing operation.
  107. See <<optimistic-concurrency-control-index>>.
  108. `result`::
  109. The result of the indexing operation, `created` or `updated`.
  110. [[docs-index-api-desc]]
  111. ==== {api-description-title}
  112. You can index a new JSON document with the `_doc` or `_create` resource. Using
  113. `_create` guarantees that the document is only indexed if it does not already
  114. exist. To update an existing document, you must use the `_doc` resource.
  115. [[index-creation]]
  116. ===== Automatically create data streams and indices
  117. If request's target doesn't exist and matches an
  118. <<create-a-data-stream-template,index template with a `data_stream`
  119. definition>>, the index operation automatically creates the data stream. See
  120. <<set-up-a-data-stream>>.
  121. If the target doesn't exist and doesn't match a data stream template,
  122. the operation automatically creates the index and applies any matching
  123. <<indices-templates,index templates>>.
  124. If no mapping exists, the index operation
  125. creates a dynamic mapping. By default, new fields and objects are
  126. automatically added to the mapping if needed. For more information about field
  127. mapping, see <<mapping,mapping>> and the <<indices-put-mapping,put mapping>> API.
  128. Automatic index creation is controlled by the `action.auto_create_index`
  129. setting. This setting defaults to `true`, which allows any index to be created
  130. automatically. You can modify this setting to explicitly allow or block
  131. automatic creation of indices that match specified patterns, or set it to
  132. `false` to disable automatic index creation entirely. Specify a
  133. comma-separated list of patterns you want to allow, or prefix each pattern with
  134. `+` or `-` to indicate whether it should be allowed or blocked. When a list is
  135. specified, the default behaviour is to disallow.
  136. IMPORTANT: The `action.auto_create_index` setting only affects the automatic
  137. creation of indices. It does not affect the creation of data streams.
  138. [source,console]
  139. --------------------------------------------------
  140. PUT _cluster/settings
  141. {
  142. "persistent": {
  143. "action.auto_create_index": "twitter,index10,-index1*,+ind*" <1>
  144. }
  145. }
  146. PUT _cluster/settings
  147. {
  148. "persistent": {
  149. "action.auto_create_index": "false" <2>
  150. }
  151. }
  152. PUT _cluster/settings
  153. {
  154. "persistent": {
  155. "action.auto_create_index": "true" <3>
  156. }
  157. }
  158. --------------------------------------------------
  159. <1> Allow auto-creation of indices called `twitter` or `index10`, block the
  160. creation of indices that match the pattern `index1*`, and allow creation of
  161. any other indices that match the `ind*` pattern. Patterns are matched in
  162. the order specified.
  163. <2> Disable automatic index creation entirely.
  164. <3> Allow automatic creation of any index. This is the default.
  165. [discrete]
  166. [[operation-type]]
  167. ===== Put if absent
  168. You can force a create operation by using the `_create` resource or
  169. setting the `op_type` parameter to _create_. In this case,
  170. the index operation fails if a document with the specified ID
  171. already exists in the index.
  172. [discrete]
  173. [[create-document-ids-automatically]]
  174. ===== Create document IDs automatically
  175. When using the `POST /<target>/_doc/` request format, the `op_type` is
  176. automatically set to `create` and the index operation generates a unique ID for
  177. the document.
  178. [source,console]
  179. --------------------------------------------------
  180. POST twitter/_doc/
  181. {
  182. "user" : "kimchy",
  183. "post_date" : "2009-11-15T14:12:12",
  184. "message" : "trying out Elasticsearch"
  185. }
  186. --------------------------------------------------
  187. The API returns the following result:
  188. [source,console-result]
  189. --------------------------------------------------
  190. {
  191. "_shards": {
  192. "total": 2,
  193. "failed": 0,
  194. "successful": 2
  195. },
  196. "_index": "twitter",
  197. "_id": "W0tpsmIBdwcYyG50zbta",
  198. "_version": 1,
  199. "_seq_no": 0,
  200. "_primary_term": 1,
  201. "result": "created"
  202. }
  203. --------------------------------------------------
  204. // TESTRESPONSE[s/W0tpsmIBdwcYyG50zbta/$body._id/ s/"successful": 2/"successful": 1/]
  205. [discrete]
  206. [[optimistic-concurrency-control-index]]
  207. ===== Optimistic concurrency control
  208. Index operations can be made conditional and only be performed if the last
  209. modification to the document was assigned the sequence number and primary
  210. term specified by the `if_seq_no` and `if_primary_term` parameters. If a
  211. mismatch is detected, the operation will result in a `VersionConflictException`
  212. and a status code of 409. See <<optimistic-concurrency-control>> for more details.
  213. [discrete]
  214. [[index-routing]]
  215. ===== Routing
  216. By default, shard placement -- or `routing` -- is controlled by using a
  217. hash of the document's id value. For more explicit control, the value
  218. fed into the hash function used by the router can be directly specified
  219. on a per-operation basis using the `routing` parameter. For example:
  220. [source,console]
  221. --------------------------------------------------
  222. POST twitter/_doc?routing=kimchy
  223. {
  224. "user" : "kimchy",
  225. "post_date" : "2009-11-15T14:12:12",
  226. "message" : "trying out Elasticsearch"
  227. }
  228. --------------------------------------------------
  229. In this example, the document is routed to a shard based on
  230. the `routing` parameter provided: "kimchy".
  231. When setting up explicit mapping, you can also use the `_routing` field
  232. to direct the index operation to extract the routing value from the
  233. document itself. This does come at the (very minimal) cost of an
  234. additional document parsing pass. If the `_routing` mapping is defined
  235. and set to be `required`, the index operation will fail if no routing
  236. value is provided or extracted.
  237. NOTE: Data streams do not support custom routing. Instead, target the
  238. appropriate backing index for the stream.
  239. [discrete]
  240. [[index-distributed]]
  241. ===== Distributed
  242. The index operation is directed to the primary shard based on its route
  243. (see the Routing section above) and performed on the actual node
  244. containing this shard. After the primary shard completes the operation,
  245. if needed, the update is distributed to applicable replicas.
  246. [discrete]
  247. [[index-wait-for-active-shards]]
  248. ===== Active shards
  249. To improve the resiliency of writes to the system, indexing operations
  250. can be configured to wait for a certain number of active shard copies
  251. before proceeding with the operation. If the requisite number of active
  252. shard copies are not available, then the write operation must wait and
  253. retry, until either the requisite shard copies have started or a timeout
  254. occurs. By default, write operations only wait for the primary shards
  255. to be active before proceeding (i.e. `wait_for_active_shards=1`).
  256. This default can be overridden in the index settings dynamically
  257. by setting `index.write.wait_for_active_shards`. To alter this behavior
  258. per operation, the `wait_for_active_shards` request parameter can be used.
  259. Valid values are `all` or any positive integer up to the total number
  260. of configured copies per shard in the index (which is `number_of_replicas+1`).
  261. Specifying a negative value or a number greater than the number of
  262. shard copies will throw an error.
  263. For example, suppose we have a cluster of three nodes, `A`, `B`, and `C` and
  264. we create an index `index` with the number of replicas set to 3 (resulting in
  265. 4 shard copies, one more copy than there are nodes). If we
  266. attempt an indexing operation, by default the operation will only ensure
  267. the primary copy of each shard is available before proceeding. This means
  268. that even if `B` and `C` went down, and `A` hosted the primary shard copies,
  269. the indexing operation would still proceed with only one copy of the data.
  270. If `wait_for_active_shards` is set on the request to `3` (and all 3 nodes
  271. are up), then the indexing operation will require 3 active shard copies
  272. before proceeding, a requirement which should be met because there are 3
  273. active nodes in the cluster, each one holding a copy of the shard. However,
  274. if we set `wait_for_active_shards` to `all` (or to `4`, which is the same),
  275. the indexing operation will not proceed as we do not have all 4 copies of
  276. each shard active in the index. The operation will timeout
  277. unless a new node is brought up in the cluster to host the fourth copy of
  278. the shard.
  279. It is important to note that this setting greatly reduces the chances of
  280. the write operation not writing to the requisite number of shard copies,
  281. but it does not completely eliminate the possibility, because this check
  282. occurs before the write operation commences. Once the write operation
  283. is underway, it is still possible for replication to fail on any number of
  284. shard copies but still succeed on the primary. The `_shards` section of the
  285. write operation's response reveals the number of shard copies on which
  286. replication succeeded/failed.
  287. [source,js]
  288. --------------------------------------------------
  289. {
  290. "_shards": {
  291. "total": 2,
  292. "failed": 0,
  293. "successful": 2
  294. }
  295. }
  296. --------------------------------------------------
  297. // NOTCONSOLE
  298. [discrete]
  299. [[index-refresh]]
  300. ===== Refresh
  301. Control when the changes made by this request are visible to search. See
  302. <<docs-refresh,refresh>>.
  303. [discrete]
  304. [[index-noop]]
  305. ===== Noop updates
  306. When updating a document using the index API a new version of the document is
  307. always created even if the document hasn't changed. If this isn't acceptable
  308. use the `_update` API with `detect_noop` set to true. This option isn't
  309. available on the index API because the index API doesn't fetch the old source
  310. and isn't able to compare it against the new source.
  311. There isn't a hard and fast rule about when noop updates aren't acceptable.
  312. It's a combination of lots of factors like how frequently your data source
  313. sends updates that are actually noops and how many queries per second
  314. Elasticsearch runs on the shard receiving the updates.
  315. [discrete]
  316. [[timeout]]
  317. ===== Timeout
  318. The primary shard assigned to perform the index operation might not be
  319. available when the index operation is executed. Some reasons for this
  320. might be that the primary shard is currently recovering from a gateway
  321. or undergoing relocation. By default, the index operation will wait on
  322. the primary shard to become available for up to 1 minute before failing
  323. and responding with an error. The `timeout` parameter can be used to
  324. explicitly specify how long it waits. Here is an example of setting it
  325. to 5 minutes:
  326. [source,console]
  327. --------------------------------------------------
  328. PUT twitter/_doc/1?timeout=5m
  329. {
  330. "user" : "kimchy",
  331. "post_date" : "2009-11-15T14:12:12",
  332. "message" : "trying out Elasticsearch"
  333. }
  334. --------------------------------------------------
  335. [discrete]
  336. [[index-versioning]]
  337. ===== Versioning
  338. Each indexed document is given a version number. By default,
  339. internal versioning is used that starts at 1 and increments
  340. with each update, deletes included. Optionally, the version number can be
  341. set to an external value (for example, if maintained in a
  342. database). To enable this functionality, `version_type` should be set to
  343. `external`. The value provided must be a numeric, long value greater than or equal to 0,
  344. and less than around 9.2e+18.
  345. When using the external version type, the system checks to see if
  346. the version number passed to the index request is greater than the
  347. version of the currently stored document. If true, the document will be
  348. indexed and the new version number used. If the value provided is less
  349. than or equal to the stored document's version number, a version
  350. conflict will occur and the index operation will fail. For example:
  351. [source,console]
  352. --------------------------------------------------
  353. PUT twitter/_doc/1?version=2&version_type=external
  354. {
  355. "message" : "elasticsearch now has versioning support, double cool!"
  356. }
  357. --------------------------------------------------
  358. // TEST[continued]
  359. NOTE: Versioning is completely real time, and is not affected by the
  360. near real time aspects of search operations. If no version is provided,
  361. then the operation is executed without any version checks.
  362. In the previous example, the operation will succeed since the supplied
  363. version of 2 is higher than
  364. the current document version of 1. If the document was already updated
  365. and its version was set to 2 or higher, the indexing command will fail
  366. and result in a conflict (409 http status code).
  367. A nice side effect is that there is no need to maintain strict ordering
  368. of async indexing operations executed as a result of changes to a source
  369. database, as long as version numbers from the source database are used.
  370. Even the simple case of updating the Elasticsearch index using data from
  371. a database is simplified if external versioning is used, as only the
  372. latest version will be used if the index operations arrive out of order for
  373. whatever reason.
  374. [discrete]
  375. [[index-version-types]]
  376. ===== Version types
  377. In addition to the `external` version type, Elasticsearch
  378. also supports other types for specific use cases:
  379. [[_version_types]]
  380. `internal`:: Only index the document if the given version is identical to the version
  381. of the stored document.
  382. `external` or `external_gt`:: Only index the document if the given version is strictly higher
  383. than the version of the stored document *or* if there is no existing document. The given
  384. version will be used as the new version and will be stored with the new document. The supplied
  385. version must be a non-negative long number.
  386. `external_gte`:: Only index the document if the given version is *equal* or higher
  387. than the version of the stored document. If there is no existing document
  388. the operation will succeed as well. The given version will be used as the new version
  389. and will be stored with the new document. The supplied version must be a non-negative long number.
  390. NOTE: The `external_gte` version type is meant for special use cases and
  391. should be used with care. If used incorrectly, it can result in loss of data.
  392. There is another option, `force`, which is deprecated because it can cause
  393. primary and replica shards to diverge.
  394. [[docs-index-api-example]]
  395. ==== {api-examples-title}
  396. Insert a JSON document into the `twitter` index with an `_id` of 1:
  397. [source,console]
  398. --------------------------------------------------
  399. PUT twitter/_doc/1
  400. {
  401. "user" : "kimchy",
  402. "post_date" : "2009-11-15T14:12:12",
  403. "message" : "trying out Elasticsearch"
  404. }
  405. --------------------------------------------------
  406. The API returns the following result:
  407. [source,console-result]
  408. --------------------------------------------------
  409. {
  410. "_shards": {
  411. "total": 2,
  412. "failed": 0,
  413. "successful": 2
  414. },
  415. "_index": "twitter",
  416. "_id": "1",
  417. "_version": 1,
  418. "_seq_no": 0,
  419. "_primary_term": 1,
  420. "result": "created"
  421. }
  422. --------------------------------------------------
  423. // TESTRESPONSE[s/"successful": 2/"successful": 1/]
  424. Use the `_create` resource to index a document into the `twitter` index if
  425. no document with that ID exists:
  426. [source,console]
  427. --------------------------------------------------
  428. PUT twitter/_create/1
  429. {
  430. "user" : "kimchy",
  431. "post_date" : "2009-11-15T14:12:12",
  432. "message" : "trying out Elasticsearch"
  433. }
  434. --------------------------------------------------
  435. Set the `op_type` parameter to _create_ to index a document into the `twitter`
  436. index if no document with that ID exists:
  437. [source,console]
  438. --------------------------------------------------
  439. PUT twitter/_doc/1?op_type=create
  440. {
  441. "user" : "kimchy",
  442. "post_date" : "2009-11-15T14:12:12",
  443. "message" : "trying out Elasticsearch"
  444. }
  445. --------------------------------------------------