Bulk

Introduced 1.0

The bulk operation lets you add, update, or delete multiple documents in a single request. Compared to individual OpenSearch indexing requests, the bulk operation has significant performance benefits. Whenever practical, we recommend batching indexing operations into bulk requests.

Beginning in OpenSearch 2.9, when indexing documents using the bulk operation, the document _id must be 512 bytes or less in size.

Path and HTTP methods

  1. POST _bulk
  2. POST <index>/_bulk

Specifying the index in the path means you don’t need to include it in the request body.

OpenSearch also accepts PUT requests to the _bulk path, but we highly recommend using POST. The accepted usage of PUT—adding or replacing a single resource at a given path—doesn’t make sense for bulk requests.

Query parameters

All parameters are optional.

ParameterTypeDescription
pipelineStringThe pipeline ID for preprocessing documents.
refreshEnumWhether to refresh the affected shards after performing the indexing operations. Default is false. true causes the changes show up in search results immediately but degrades cluster performance. wait_for waits for a refresh. Requests take longer to return, but cluster performance isn’t degraded.
require_aliasBooleanSet to true to require that all actions target an index alias rather than an index. Default is false.
routingStringRoutes the request to the specified shard.
timeoutTimeHow long to wait for the request to return. Default is 1m.
typeString(Deprecated) The default document type for documents that don’t specify a type. Default is _doc. We highly recommend ignoring this parameter and using the _doc type for all indexes.
wait_for_active_shardsStringSpecifies the number of active shards that must be available before OpenSearch processes the bulk request. Default is 1 (only the primary shard). Set to all or a positive integer. Values greater than 1 require replicas. For example, if you specify a value of 3, the index must have 2 replicas distributed across 2 additional nodes in order for the request to succeed.
batch_sizeInteger(Deprecated) Specifies the number of documents to be batched and sent to an ingest pipeline to be processed together. Default is 2147483647 (documents are ingested by an ingest pipeline all at once). If the bulk request doesn’t explicitly specify an ingest pipeline or the index doesn’t have a default ingest pipeline, then this parameter is ignored. Only documents with create, index, or update actions can be grouped into batches.

Request body

The bulk request body follows this pattern:

  1. Action and metadata\n
  2. Optional document\n
  3. Action and metadata\n
  4. Optional document\n

The optional JSON document doesn’t need to be minified—spaces are fine—but it does need to be on a single line. OpenSearch uses newline characters to parse bulk requests and requires that the request body end with a newline character.

All actions support the same metadata: _index, _id, and _require_alias. If you don’t provide an ID, OpenSearch generates one automatically, which can make it challenging to update the document at a later time.

Create

Creates a document if it doesn’t already exist and returns an error otherwise. The next line must include a JSON document:

  1. { "create": { "_index": "movies", "_id": "tt1392214" } }
  2. { "title": "Prisoners", "year": 2013 }

Delete

This action deletes a document if it exists. If the document doesn’t exist, OpenSearch doesn’t return an error but instead returns not_found under result. Delete actions don’t require documents on the next line:

  1. { "delete": { "_index": "movies", "_id": "tt2229499" } }

Index

Index actions create a document if it doesn’t yet exist and replace the document if it already exists. The next line must include a JSON document:

  1. { "index": { "_index": "movies", "_id": "tt1979320" } }
  2. { "title": "Rush", "year": 2013}

Update

By default, this action updates existing documents and returns an error if the document doesn’t exist. The next line must include a full or partial JSON document, depending on how much of the document you want to update:

  1. { "update": { "_index": "movies", "_id": "tt0816711" } }
  2. { "doc" : { "title": "World War Z" } }

Upsert

To upsert a document, specify doc_as_upsert as true. If a document exists, it is updated; if it does not exist, a new document is indexed with the parameters specified in the doc field:

  1. { "update": { "_index": "movies", "_id": "tt0816711" } }
  2. { "doc" : { "title": "World War Z" }, "doc_as_upsert": true }

Script

You can specify a script for more complex document updates by defining the script with the source or id from a document:

  1. { "update": { "_index": "movies", "_id": "tt0816711" } }
  2. { "script" : { "source": "ctx._source.title = \"World War Z\"" } }

Example request

  1. POST _bulk
  2. { "delete": { "_index": "movies", "_id": "tt2229499" } }
  3. { "index": { "_index": "movies", "_id": "tt1979320" } }
  4. { "title": "Rush", "year": 2013 }
  5. { "create": { "_index": "movies", "_id": "tt1392214" } }
  6. { "title": "Prisoners", "year": 2013 }
  7. { "update": { "_index": "movies", "_id": "tt0816711" } }
  8. { "doc" : { "title": "World War Z" } }

copy

Example response

In the response, pay particular attention to the top-level errors boolean. If true, you can iterate over the individual actions for more detailed information.

  1. {
  2. "took": 11,
  3. "errors": true,
  4. "items": [
  5. {
  6. "index": {
  7. "_index": "movies",
  8. "_id": "tt1979320",
  9. "_version": 1,
  10. "result": "created",
  11. "_shards": {
  12. "total": 2,
  13. "successful": 1,
  14. "failed": 0
  15. },
  16. "_seq_no": 1,
  17. "_primary_term": 1,
  18. "status": 201
  19. }
  20. },
  21. {
  22. "create": {
  23. "_index": "movies",
  24. "_id": "tt1392214",
  25. "status": 409,
  26. "error": {
  27. "type": "version_conflict_engine_exception",
  28. "reason": "[tt1392214]: version conflict, document already exists (current version [1])",
  29. "index": "movies",
  30. "shard": "0",
  31. "index_uuid": "yhizhusbSWmP0G7OJnmcLg"
  32. }
  33. }
  34. },
  35. {
  36. "update": {
  37. "_index": "movies",
  38. "_id": "tt0816711",
  39. "status": 404,
  40. "error": {
  41. "type": "document_missing_exception",
  42. "reason": "[_doc][tt0816711]: document missing",
  43. "index": "movies",
  44. "shard": "0",
  45. "index_uuid": "yhizhusbSWmP0G7OJnmcLg"
  46. }
  47. }
  48. }
  49. ]
  50. }