Elasticsearch-查询数据&乐观锁
# 前言
前面已经新增了一些数据,现在来查询数据
# GET /_index/_type/_id
http://192.168.56.10:9200/customer/external/1
1
返回Status:200 OK
{
"_index": "customer", //哪个索引
"_type": "external", //类型
"_id": "1", //主键
"_version": 3, //版本
"_seq_no": 3, //并发控制字段,每次更新就会跟着更新,用来做乐观锁
"_primary_term": 1, //分片
"found": true, //找到了数据
"_source": { //数据内容
"name": "Jhon Doe"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 乐观锁
当遇到并发操作,需要用到_seq_no
,
比如A,B同时并发
A想要把name改成AAA
B想要把name改成BBB
- 请求路径?if_seq_no=number&if_primary_term=1
# A并发,PUT请求
- 请求路径
http://192.168.56.10:9200/customer/external/1?if_seq_no=3&if_primary_term=1
1
- 请求体
{
"name":"AAA"
}
1
2
3
2
3
- 返回Status:200 OK
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 9,
"_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
发现_seq_no
已经改成9了
# B并发,PUT请求
- 请求路径
http://192.168.56.10:9200/customer/external/1?if_seq_no=3&if_primary_term=1
1
- 请求体
{
"name":"BBB"
}
1
2
3
2
3
- 返回Status:409 Conflict
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [3], primary term [1]. current document has seqNo [9] and primary term [1]",
"index_uuid": "rzqpglKxSpK1SZFJdjTAfw",
"shard": "0",
"index": "customer"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [3], primary term [1]. current document has seqNo [9] and primary term [1]",
"index_uuid": "rzqpglKxSpK1SZFJdjTAfw",
"shard": "0",
"index": "customer"
},
"status": 409
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
这时候B请求就失败了,如果想要修改,必须拿到最新数据,拿到_seq_no
帮我改善此页面 (opens new window)
上次更新: 2020/12/17, 08:38:35