- get localhost:9200/myshare/_doc/12
-
- {
- "_index": "myshare",
- "_type": "_doc",
- "_id": "12",
- "_version": 5,
- "_seq_no": 20,
- "_primary_term": 2,
- "found": true,
- "_source": {
- "addr": "sichuan chengdu12 after update by post with _update2",
- "age": "12",
- "email": "12@gmai.com",
- "name": "zhang san12 after update by post with _update2"
- }
- }
- post localhost:9200/myshare/_doc/12?version=5
- {
- "doc":{
- "addr":"sichuan chengdu12 after update by version=5
- , "name":"zhang san12 after update by version=5"
- }
- }
- // 结果:抛出异常
- {
- "error": {
- "root_cause": [
- {
- "type": "action_request_validation_exception",
- "reason": "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
- }
- ]
- },
- "status": 400
- }
【异常报文解说】
- post localhost:9200/myshare/_doc/12?if_seq_no=20&if_primary_term=2
- {
- "doc":{
- "addr":"sichuan chengdu12 after update by if_seq_no=20&if_primary_term=2"
- , "name":"zhang san12 after update by if_seq_no=20&if_primary_term=2"
- }
- }
- // 响应报文
- {
- "_index": "myshare",
- "_type": "_doc",
- "_id": "12",
- "_version": 6,
- "result": "updated",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 22,
- "_primary_term": 2
- }
-
【解说】
- post localhost:9200/myshare/_doc/12?version=6&version_type=external
- {
- "doc":{
- "addr":"sichuan chengdu12 after update by version_type=external"
- , "name":"zhang san12 after update by version_type=external"
- }
- }
-
- // 响应报文
- {
- "error": {
- "root_cause": [
- {
- "type": "version_conflict_engine_exception",
- "reason": "[12]: version conflict, current version [6] is higher or equal to the one provided [6]",
- "index_uuid": "FpuW3cmhR9Ws3M5JRzRgZA",
- "shard": "0",
- "index": "myshare"
- }
- ],
- …
- },
- "status": 409
- }
【解说】报错原因:
- post localhost:9200/myshare/_doc/12?version=7&version_type=external
- {
- "doc":{
- "addr":"sichuan chengdu12 after update by version_type=external"
- , "name":"zhang san12 after update by version_type=external"
- }
- }
-
- // 响应报文
- {
- "_index": "myshare",
- "_type": "_doc",
- "_id": "12",
- "_version": 7,
- "result": "updated",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 23,
- "_primary_term": 2
- }
-
refer2 What is seq_no and primary_term in elasticsearch? - DevOpsSchool.com
每次故障转移期间不同的分片成为主分片时,主项都会增加。这有助于解决重新联机的旧主节点上发生的更改与新主节点上发生的更改(新的胜利)。
复制组的主要术语只是主分片更改次数的计数器。
这些主要术语是增量的,并且在提升主要术语时会发生变化。它们保持在集群状态中,因此代表了集群所在的一种“版本”或“一代”的主节点。
为了确保文档的旧版本不会覆盖新版本,对文档执行的每个操作都由协调该更改的主分片分配一个序列号。
假设您的索引由 5 个主分片组成(这是版本 7 之前的默认值)。索引和更新请求是针对主分片执行的。如果您有多个主分片,elasticsearch 能够将传入请求(例如巨大的批量请求)并行化/分发到多个分片以提高性能。
因此,primary_term 提供了有关执行/协调更改/更新的主分片(在此示例中为#1、#2、#3、#4 或#5)的信息。
- put localhost:9200/myshare/_doc/20221106
- {
- "addr":"sichuan chengdu-20221106"
- , "age":"20221106"
- , "email":"20221106@gmai.com"
- , "name":"zhang san-20221106"
- }
-
- // 响应报文
- {
- "_index": "myshare",
- "_type": "_doc",
- "_id": "20221106",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 25,
- "_primary_term": 3
- }
【解析】
_version 等于1;
_seq_no 等于 25;
2)索引文档 20221107
- put localhost:9200/myshare/_doc/20221107
- {
- "addr":"sichuan chengdu-20221107"
- , "age":"20221107"
- , "email":"20221107@gmai.com"
- , "name":"zhang san-20221107"
- }
-
- // 响应报文
- {
- "_index": "myshare",
- "_type": "_doc",
- "_id": "20221107",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 26,
- "_primary_term": 3
- }
【解析】
_version 等于1;
_seq_no 等于 26;
我想应该可以理解 seq_no 与 version的区别了;