spring-data-elasticsearch icon indicating copy to clipboard operation
spring-data-elasticsearch copied to clipboard

Allow op_type to be set on save methods in (Reactive)ElasticsearchOperations.

Open frnandu opened this issue 2 years ago • 4 comments

Currently when using repository save operation it will always use op_type index, which doesn't allow to detect concurrent creation of same entity with same generated id (so that if first operation succeed and it creates, the second will fail since it already exists a document with same id). If I understand correctly usage of seq_no + primary_term is still not possible in the creation phase of the document, only after it is created for subsequent update operations. Since in ReactiveCrudRepository there isn't a create method which elasticsearch implementation can implement, there should be some way of telling to force creation of entity when using the save method or failing if it already exists. Without this it seems to me one cannot implement a fully optimistic locking lifecycle for entities/documents which need to have a generated id and concurrency control which avoids creating documents with same id being overwritten. Checking before saving for existence of entity doesn't resolve every scenario when concurrent requests are made. Do you have any ideas how to resolve this?

frnandu avatar Aug 30 '22 08:08 frnandu

what do you mean with "same generated id"? Who creates this id?****

If I understand correctly usage of seq_no + primary_term is still not possible in the creation phase of the document, only after it is created for subsequent update operations.

Right. Elasticsearch only returns seq_no and primary_term after having indexed a document.

Currently there is no possibility in the save operations of the ReactiveElasticsearchOperations to pass in the op_type.

sothawo avatar Aug 31 '22 19:08 sothawo

what do you mean with "same generated id"? Who creates this id?****

I mean composite id using few fields from an entity (ex: Vote which is identified by concatenating entity_name + user_id + entity_id). If concurrently requests are made to create such a Vote with same field values (which result in same generated id), there is no way of optimistic locking versioning to detect if a vote already was created and written on ES successfully or not, and subsequently be able to accordingly publish a messaging queue event of CREATE or UPDATE.

Currently there is no possibility in the save operations of the ReactiveElasticsearchOperations to pass in the op_type.

Could it be added such possibility easily?

Thanks

frnandu avatar Aug 31 '22 23:08 frnandu

That won't be too hard, it means to add new overloaded versions of save to the (Reactive)DocumentOperations interface and to implement these.

sothawo avatar Sep 02 '22 05:09 sothawo

see #2089 as well

sothawo avatar Sep 04 '22 17:09 sothawo