| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 | [[docs-bulk]]== Bulk APIThe bulk API makes it possible to perform many index/delete operationsin a single API call. This can greatly increase the indexing speed..Client support for bulk requests*********************************************Some of the officially supported clients provide helpers to assist withbulk requests and reindexing of documents from one index to another:Perl::    See https://metacpan.org/pod/Search::Elasticsearch::Bulk[Search::Elasticsearch::Bulk]    and https://metacpan.org/pod/Search::Elasticsearch::Scroll[Search::Elasticsearch::Scroll]Python::    See http://elasticsearch-py.readthedocs.org/en/master/helpers.html[elasticsearch.helpers.*]*********************************************The REST API endpoint is `/_bulk`, and it expects the following JSONstructure:[source,js]--------------------------------------------------action_and_meta_data\noptional_source\naction_and_meta_data\noptional_source\n....action_and_meta_data\noptional_source\n--------------------------------------------------*NOTE*: the final line of data must end with a newline character `\n`.The possible actions are `index`, `create`, `delete` and `update`.`index` and `create` expect a source on the nextline, and have the same semantics as the `op_type` parameter to thestandard index API (i.e. create will fail if a document with the sameindex and type exists already, whereas index will add or replace adocument as necessary). `delete` does not expect a source on thefollowing line, and has the same semantics as the standard delete API.`update` expects that the partial doc, upsert and script and its optionsare specified on the next line.If you're providing text file input to `curl`, you *must* use the`--data-binary` flag instead of plain `-d`. The latter doesn't preservenewlines. Example:[source,js]--------------------------------------------------$ cat requests{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }{ "field1" : "value1" }$ curl -s -XPOST localhost:9200/_bulk --data-binary "@requests"; echo{"took":7,"items":[{"create":{"_index":"test","_type":"type1","_id":"1","_version":1}}]}--------------------------------------------------Because this format uses literal `\n`'s as delimiters, please be surethat the JSON actions and sources are not pretty printed. Here is anexample of a correct sequence of bulk commands:[source,js]--------------------------------------------------{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }{ "field1" : "value1" }{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }{ "field1" : "value3" }{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }{ "doc" : {"field2" : "value2"} }--------------------------------------------------In the above example `doc` for the `update` action is a partialdocument, that will be merged with the already stored document.The endpoints are `/_bulk`, `/{index}/_bulk`, and `{index}/{type}/_bulk`.When the index or the index/type are provided, they will be used bydefault on bulk items that don't provide them explicitly.A note on the format. The idea here is to make processing of this asfast as possible. As some of the actions will be redirected to othershards on other nodes, only `action_meta_data` is parsed on thereceiving node side.Client libraries using this protocol should try and strive to dosomething similar on the client side, and reduce buffering as much aspossible.The response to a bulk action is a large JSON structure with theindividual results of each action that was performed. The failure of asingle action does not affect the remaining actions.There is no "correct" number of actions to perform in a single bulkcall. You should experiment with different settings to find the optimumsize for your particular workload.If using the HTTP API, make sure that the client does not send HTTPchunks, as this will slow things down.[float][[bulk-versioning]]=== VersioningEach bulk item can include the version value using the`_version`/`version` field. It automatically follows the behavior of theindex / delete operation based on the `_version` mapping. It alsosupport the `version_type`/`_version_type` (see <<index-versioning, versioning>>)[float][[bulk-routing]]=== RoutingEach bulk item can include the routing value using the`_routing`/`routing` field. It automatically follows the behavior of theindex / delete operation based on the `_routing` mapping.[float][[bulk-parent]]=== ParentEach bulk item can include the parent value using the `_parent`/`parent`field. It automatically follows the behavior of the index / deleteoperation based on the `_parent` / `_routing` mapping.[float][[bulk-timestamp]]=== TimestampEach bulk item can include the timestamp value using the`_timestamp`/`timestamp` field. It automatically follows the behavior ofthe index operation based on the `_timestamp` mapping.[float][[bulk-ttl]]=== TTLEach bulk item can include the ttl value using the `_ttl`/`ttl` field.It automatically follows the behavior of the index operation based onthe `_ttl` mapping.[float][[bulk-consistency]]=== Write ConsistencyWhen making bulk calls, you can require a minimum number of activeshards in the partition through the `consistency` parameter. The valuesallowed are `one`, `quorum`, and `all`. It defaults to the node levelsetting of `action.write_consistency`, which in turn defaults to`quorum`.For example, in a N shards with 2 replicas index, there will have to beat least 2 active shards within the relevant partition (`quorum`) forthe operation to succeed. In a N shards with 1 replica scenario, therewill need to be a single shard active (in this case, `one` and `quorum`is the same).[float][[bulk-refresh]]=== RefreshThe `refresh` parameter can be set to `true` in order to refresh the relevantprimary and replica shards immediately after the bulk operation has occurredand make it searchable, instead of waiting for the normal refresh interval toexpire. Setting it to `true` can trigger additional load, and may slow downindexing. Due to its costly nature, the `refresh` parameter is set on the bulk request leveland is not supported on each individual bulk item.[float][[bulk-update]]=== UpdateWhen using `update` action `_retry_on_conflict` can be used as field inthe action itself (not in the extra payload line), to specify how manytimes an update should be retried in the case of a version conflict.The `update` action payload, supports the following options: `doc`(partial document), `upsert`, `doc_as_upsert`, `script`, `params` (forscript), `lang` (for script) and `fields`. See update documentation for details onthe options. Curl example with update actions:[source,js]--------------------------------------------------{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }{ "doc" : {"field" : "value"} }{ "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }{ "script" : { "inline": "ctx._source.counter += param1", "lang" : "js", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}{ "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "_retry_on_conflict" : 3} }{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }{ "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "fields" : ["_source"]} }{ "doc" : {"field" : "value"} }{ "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }{ "doc" : {"field" : "value"}, "fields": ["_source"]}--------------------------------------------------[float][[bulk-security]]=== SecuritySee <<url-access-control>>
 |