Bulk

    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.

    copy

    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 bulk request body follows this pattern:

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

    All actions support the same metadata: _index, _id, and . 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.

    • 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

      1. { "title": "Rush", "year": 2013}
    • Update

      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.

      It can also include a script or upsert for more complex document updates.

      • Script

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

        1. { "update": { "_index": "movies", "_id": "tt0816711" } }

    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.