Elasticsearch concurrent request handling with optimistic locking in Java UpdateRequest with Upsert

Concurrent request handling

I have one use case where application can get concurrent requests to update single document.
Earlier I was using IndexRequest with External versionining but IndexRequest I was facing problem that it was replacing the old document with new document rather than updating old document.

        <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.1.1</version>
    </dependency>

But with IndexRequest external versioning was working but to be honest I find it bit cumbersome.

Neverthless, Now I thought to use UpdateRequest which will create the document with givenId or update the document(rather than replacning).

        UpdateRequest updateRequest = new UpdateRequest(elasticSearchConfiguration.getIndexName(), id);
        updateRequest.doc(objectMapper.convertValue(object, Map.class));
        updateRequest.docAsUpsert(true);
        updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

I am not able to understand how elasticsearch API will handles the concurrent request and throw VersionConflict exception. As per my initial testing, I don't think UpdateRequest is helping .

Any help would be great like how to best handle the concurrent request with elasticsearch (if possible not manual optimistic locking)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.