ElasticSarch7:document的全量替换、强制创建以及图解lazy delete机制

1.全量替换

如果document的id不存在,那么就是创建,如果以及存在,那么就是全量创建

PUT /test_index/test_type/1
{
  "test_field":"xxx"
}

执行结果

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "test_field": "xxx"
  }
}


PUT /test_index/test_type/1
{
  "test_field":"xxx",
  "test_field2":"xxx2"
}

执行结果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "2",
  "_version": 1,
  "found": true,
  "_source": {
    "test_field": "xxx",
    "test_field2": "xxx2"
  }
}




2.强制创建

如果你不想做替换操作,而是希望做创建操作,那么可以指定命令为创建(即强制创建操作)

语法:两种语法

1)PUT /index/type/id?op_type=create

2)PUT /index/type/id?_create

普通创建:

PUT /test_index/test_type/2
{
  "test_field":"xxx1",
  "test_field2":"xxx3"
}

强制创建:

PUT /test_index/test_type/2?op_type=create
{
  "test_field":"xxx1",
  "test_field2":"xxx3"
}

结果是id冲突:

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_type][2]: version conflict, document already exists (current version [2])",
        "index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
        "shard": "2",
        "index": "test_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][2]: version conflict, document already exists (current version [2])",
    "index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
    "shard": "2",
    "index": "test_index"
  },
  "status": 409
}


另一种语法:


PUT /test_index/test_type/2/_create
{
  "test_field":"xxx0",
  "test_field2":"xxx02"
}
执行结果:id冲突

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_type][2]: version conflict, document already exists (current version [2])",
        "index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
        "shard": "2",
        "index": "test_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][2]: version conflict, document already exists (current version [2])",
    "index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
    "shard": "2",
    "index": "test_index"
  },
  "status": 409
}



3.delete删除的原理

命令:DELETE /test_index/test_type/2

此时如果执行get操作就查找不到数据了。但是实际上,delete不会进行物理删除,只会将其标记为deleted,当数据越来越多的时候,在后台自动删除。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值