Medvednic
Medvednic

Reputation: 692

Is updating an array in Elasticsearch document thread safe?

I have a document which has an array of strings as one of it's properties.

There is a case where multiple update queries are executed on the index from two threads and potentially can update the same document.

For example given a document where the value of my_array is:

my_array = [1,2,3,4]

The two threads execute the following update query which runs a painless script on the index with different params.

thread_0 -> my_item=3

thread_1 -> my_item=2

int index_of_my_item = ctx._source.my_array.indexOf(params.my_item);

if (-1 != index_of_my_item) {
    ctx._source.my_array.remove(index_of_item);                    
}   

Is the execution thread safe? and the property value in the document will be:

my_array = [1,4]

Or are there race conditions to consider?

Thanks,

Upvotes: 0

Views: 968

Answers (1)

Pankaj Sharma
Pankaj Sharma

Reputation: 123

ES works on Optimistic concurrency control and it uses version number to implement this, so there is no concept of thread safe (from ES perspective).

Just go through these links and it will give you fair idea what exactly update means for ES

https://www.elastic.co/guide/en/elasticsearch/guide/master/version-control.html https://www.elastic.co/guide/en/elasticsearch/guide/master/optimistic-concurrency-control.html

When updating a document with the index API, we read the original document, make our changes, and then reindex the whole document in one go. The most recent indexing request wins: whichever document was indexed last is the one stored in Elasticsearch. If somebody else had changed the document in the meantime, their changes would be lost.

Upvotes: 1

Related Questions