1.POST基于version做乐观锁控制并发
PUT /index_test/type_test/7
{
  "name":"steam"

}


{
  "_index": "index_test",
  "_type": "type_test",
  "_id": "7",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}
得到version=1
如果使用version=2会报错,如图:版本不一致
ElasticSearch6.2.4(8)——POST解决版本冲突
POST /index_test/type_test/7/_update?version=1
{
  "doc": {
    "name":"steam1111"
  }
}
添加的version参数版本必须和es里面的版本一致
{
  "_index": "index_test",
  "_type": "type_test",
  "_id": "7",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}


2.增加重试机制
POST /index_test/type_test/7/_update?retry_on_conflict=5
{
  "doc": {
    "name":"steam222222222"
  }
}
retry_on_conflict参数:指定更新操作在发生版本冲突时重试的次数,默认为0。

对于文档的更新操作,ElasticSearch引擎需要顺序执行三个阶段:获取文档(Get),更新文档(Update)和索引文档(Index)。在更新文档时,其他进程可能已经把相同的文档修改了。
在默认情况下,更新操作由于检测到版本冲突而就立即失败,抛出异常。参数retry_on_conflict控制在ElasticSearch引擎真正抛出异常之前,更新操作重新执行的次数。
{
  "_index": "index_test",
  "_type": "type_test",
  "_id": "7",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 5,
  "_primary_term": 1
}

相关文章:

  • 2022-12-23
  • 2021-11-09
  • 2021-05-07
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2021-07-23
  • 2022-01-05
  • 2022-01-06
  • 2022-01-17
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案