Elasticsearch更新冲突

版权声明:本文为博主原创文章,请尊重劳动成果,觉得不错就在文章下方顶一下呗,转载请标明原地址。 https://blog.csdn.net/m0_37739193/article/details/82634226

问题描述:

手机APP端两个用户同时评论一篇文章该篇文章的评论量只增加了1。
 
问题代码:

XContentBuilder updateSource = XContentFactory.jsonBuilder().startObject()
        .field("atdCnt", atdCnt + 1).endObject();
updateResponse = getTransportClient()
        .prepareUpdate(esProperties.getES_Index(), esProperties.getES_Type(), docID)
        .setRouting(esProperties.getES_Routing()).setDoc(updateSource).get();

 
解决:

UpdateRequest updateRequest = new UpdateRequest();  
updateRequest.index(esProperties.getES_Index());  
updateRequest.type(esProperties.getES_Type());  
updateRequest.id(docID);
updateRequest.routing(esProperties.getES_Routing());
updateRequest.script(new Script("ctx._source.cmtCnt++")).retryOnConflict(2);
updateResponse = getTransportClient().update(updateRequest).get();

参考:
https://blog.csdn.net/qq_35431789/article/details/78653554

猜你喜欢

转载自blog.csdn.net/m0_37739193/article/details/82634226