Bulk

    The bulk operation lets you add, update, or delete many 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.

    1. 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.

    All bulk URL parameters are optional.

    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 }
    • 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.

    • 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

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