Search code examples
performanceopensearch

the difference in terms of performance two types of update in opensearch


The opensearch allows us to update given document in two ways.

  1. PUT index/_doc/1 (replaces whole document)
  2. POST index/_update/1 (allows for partial update)

I conducted tests to observe concurrent updates on a single document. Interestingly, PUT requests consistently succeed, while POST requests trigger a version_conflict_engine_exception. Although this exception is expected, it raises questions about potential additional overhead in the update API. Unfortunately, I couldn’t find relevant information. Can you advise which method performs better in terms of execution performance?


Solution

  • Index API This operation indexes the result.

    PUT index/_doc/1
    

    When updating a document using the index API a new version of the document is always created even if the document hasn’t changed.

    _update API

    POST index/_update/1 
    

    This operation:

    1. Gets the document (collocated with the shard) from the index.
    2. Runs the specified script.
    3. Indexes the result.

    As a result, because Index API need less operation than Update API call, the Index API, PUT index/_doc/1, will perform better than Update API, POST index/_update/1.


    About VersionConflictException:

    Index operations can be made conditional and only be performed if the last modification to the document was assigned the sequence number and primary term specified by the if_seq_no and if_primary_term parameters. If a mismatch is detected, the operation will result in a VersionConflictException and a status code of 409. See Optimistic concurrency control for more details.

    When updating a document using the index API a new version of the document is always created even if the document hasn’t changed. If this isn’t acceptable use the _update API with detect_noop set to true. This option isn’t available on the index API because the index API doesn’t fetch the old source and isn’t able to compare it against the new source. https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-noop