同时附加/插入Elasticsearch

Using elasticsearch 5.x and Elastic package 5.x

Is there any way to handle simultaneous inserts or appends to an array field within a document in elasticsearch? I have an array field that can potentially have multiple items appended to it; however, that isn't always necessarily the case. Suppose I'm designing a forum/comment space. I have a document for posts, with "likes" or favorites being represented by arrays of usernames. How would I handle a case in which more than 1 user is appended to an array, without running the risk of users overwriting one another?

There is no "append" like mechanism for adding entries into an array using the rest endpoints. The only way to add an item to an array in Elasticsearch, is to:

  1. Get the Doc
  2. Add the element you want in memory
  3. Put the whole array back

The other option you have is the scripting api. This will allow you to do the "append" semantics above, on the ES cluster itself.

https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html#_using_scripts_to_make_partial_updates

Elasticsearch also uses optimistic locking to make sure that if multiple processes try to "patch" the field at the same time, only 1 will win, and the others will have to retrieve the doc again, and append what they want.

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