es-文档版本号,操作类型,分片选择

时间:2021-05-12 15:03:27

一、版本号:

在es中每个文档都有一个版本号,默认情况下,版本号都是随着每次对该文档的修改或者删除自增的,当然你也可以自己指定。有了这个文档号,我们可以像mysql

乐观锁一样,用来进行控制字我们文档的更新,如果要更新的文档号与索引中的文档号不一致,那么es会拒绝该次操作。常用于事务的处理中。

url:PUT http://127.0.0.1:9200/myes/name/6?version=17&pretty/

参数:

{
"name":"zzq",
"age":17
}

结果:

成功的结果

{
"_index": "myes",
"_type": "name",
"_id": "6",
"_version": 18,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}

 

失败的结果:

{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[name][6]: version conflict, current [18], provided [17]",
"shard": "2",
"index": "myes"
}
],
"type": "version_conflict_engine_exception",
"reason": "[name][6]: version conflict, current [18], provided [17]",
"shard": "2",
"index": "myes"
},
"status": 409
}

  

二:操作类型

系统支持通过 op_type=create命令执行创建操作,只有不存在此文档的时候才会创建。如果不指定类型,那么存在此文档就会更新。

url:PUT http://127.0.0.1:9200/myes/name/8?op_type=create&pretty/

参数:

{
"name":"zzq5",
"age":17
}

 

结果:

{
"_index": "myes",
"_type": "name",
"_id": "8",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}

  再次执行上述的链接,结果如下:

{
"error": {
"root_cause": [
{
"type": "document_already_exists_exception",
"reason": "[name][8]: document already exists",
"shard": "1",
"index": "myes"
}
],
"type": "document_already_exists_exception",
"reason": "[name][8]: document already exists",
"shard": "1",
"index": "myes"
},
"status": 409
}

  

 三、分片选择

默认情况下,分片的选择是通过ID的散列值进行控制的。这个只能通过 router参数进行手动控制(通过hash的值)

url:PUT http://127.0.0.1:9200/myes/name/?router=myes&pretty/