Elasticsearch-插入文档时create,index,update的区别

前言

本文基于elasticsearch7.3.0版本

本文内容如下
在这里插入图片描述

create

当我们索引一个文档,怎么确认我们正在创建一个完全新的文档,而不是覆盖现有的呢?
使用create

不指定文档id–create

# 请求
POST my_index/_doc
{
    
    
  "tag":"不指定id"
}

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  // 每次返回的id不一样
  "_id" : "XxB2sW8B0oo5OtBYmEWT",
  "_version" : 1,
  // 这里表明是create
  "result" : "created",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

指定文档id,不指定版本号,文档不存在–create

通过op_type=create指定操作类型

# 请求
POST my_index/_doc/3?op_type=create
{
    
    
  "tag":"指定id"
}
# 上面请求等价于
POST my_index/_doc/3/_create
{
    
    
  "tag":"指定id"
}

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 1
}

指定文档id,不指定版本号,文档存在–报错

# 请求
POST my_index/_doc/3?op_type=create
{
    
    
  "tag":"指定id"
}

# 响应
{
    
    
  "error": {
    
    
    "root_cause": [
      {
    
    
        "type": "version_conflict_engine_exception",
        "reason": "[3]: version conflict, document already exists (current version [1])",
        "index_uuid": "clHAdrqBS1Ku-wM1d3ebOg",
        "shard": "0",
        "index": "my_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[3]: version conflict, document already exists (current version [1])",
    "index_uuid": "clHAdrqBS1Ku-wM1d3ebOg",
    "shard": "0",
    "index": "my_index"
  },
  "status": 409
}

指定文档id,指定版本号–报错

注意:create操作只支持内部版本控制,需要使用外部指定版本,请使用index

  • op_type:操作类型
  • version:外部指定版本号
  • version_type:外部版本号校验类型,有两种:external(默认,外部版本号必须要大于内部版本号),external_gte(外部版本号大于等于内部版本号)
# 请求
POST my_index/_doc/3?op_type=create&version=2&version_type=external_gte
{
    
    
  "tag":"指定id"
}

# 响应
{
    
    
  "error": {
    
    
    "root_cause": [
      {
    
    
        "type": "action_request_validation_exception",
        "reason": "Validation Failed: 1: create operations only support internal versioning. use index instead;"
      }
    ],
    "type": "action_request_validation_exception",
    "reason": "Validation Failed: 1: create operations only support internal versioning. use index instead;"
  },
  "status": 400
}

index

不指定文档id–create

# 请求
POST my_index/_doc
{
    
    
  "tag":"不指定id"
}

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  // 每次返回的id不一样
  "_id" : "XxB2sW8B0oo5OtBYmEWT",
  "_version" : 1,
  // 这里表明是create
  "result" : "created",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

指定文档id,不指定版本号,文档不存在–create

POST my_index/_doc/1
{
    
    
  "tag":"指定id"
}

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

指定文档id,不指定版本号,文档存在–update

POST my_index/_doc/1
{
    
    
  "tag":"指定id"
}

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

指定文档id,指定版本号,文档不存在–create

POST my_index/_doc/2?op_type=index&version=2&version_type=external
{
    
    
  "tag":"指定id,指定外部版本号"
}

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 2,
  "result" : "created",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

指定文档id,指定版本号,文档存在–校验外部版本号

POST my_index/_doc/2?op_type=index&version=3&version_type=external
{
    
    
  "tag":"指定id,指定外部版本号"
}

# 校验通过
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}

# 校验不通过
{
    
    
  "error": {
    
    
    "root_cause": [
      {
    
    
        "type": "version_conflict_engine_exception",
        "reason": "[2]: version conflict, current version [2] is higher or equal to the one provided [2]",
        "index_uuid": "clHAdrqBS1Ku-wM1d3ebOg",
        "shard": "0",
        "index": "my_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[2]: version conflict, current version [2] is higher or equal to the one provided [2]",
    "index_uuid": "clHAdrqBS1Ku-wM1d3ebOg",
    "shard": "0",
    "index": "my_index"
  },
  "status": 409
}

update

在elasticsearch中文档是不可修改的,但是可以通过其它方式来实现文档的更新
内部原理: 删除旧文档,索引新文档
在内部,ElasticSearch已将旧文档标记为已删除,并增加一个全新的文档.尽管你不能再对旧版本的文档进行访问,但它并不会立即消失.当继续索引更多的数据,ElasticSearch会在后台清理这些已删除文档.

整体更新

# 整体更新
POST my_index/_doc/3
{
    
    
  "tag":"整体更新",
  "desc":"新增的字段"
}

# 查询
GET my_index/_doc/3

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 4,
  "_seq_no" : 10,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    
    
    "tag" : "整体更新",
    "desc" : "新增的字段"
  }
}

部分更新

# 部分更新,使用doc
POST my_index/_update/3
{
    
    
  "doc": {
    
    
    "desc":"部分更新"
  }
}

# 查询
GET my_index/_doc/3

# 响应
{
    
    
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 5,
  "_seq_no" : 11,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    
    
    "tag" : "整体更新",
    "desc" : "部分更新"
  }
}

猜你喜欢

转载自blog.csdn.net/qq_28988969/article/details/104016579