PUT

作者: 潘大的笔记 | 来源:发表于2019-03-01 18:38 被阅读0次

    文档是不可改变的,不能修改他们。
    如果想要更新现在有的文档,需要重建索引或者进行替换
    不能访问旧版本的文档,但它不是立即删除的

    PUT /website/blog/123
    {
      "title": "My first blog entry",
      "text":  "I am starting to get the hang of this...",
      "date":  "2014/01/02"
    }
    

    返回的结果:_version自增,created,表示不是create

    {
      "_index" :   "website",
      "_type" :    "blog",
      "_id" :      "123",
      "_version" : 2,
      "created":   false
    }
    

    可以利用_version来保证安全

    put请求,(1)是指_version在1的时候才能更新

    PUT /website/blog/1?version=1 (1)
    {
      "title": "My first blog entry",
      "text":  "Starting to get the hang of this..."
    }
    

    更新成功

    {
      "_index":   "website",
      "_type":    "blog",
      "_id":      "1",
      "_version": 2
      "created":  false
    }
    

    再次相同请求,则```490 Conflict

    {
       "error": {
          "root_cause": [
             {
                "type": "version_conflict_engine_exception",
                "reason": "[blog][1]: version conflict, current [2], provided [1]",
                "index": "website",
                "shard": "3"
             }
          ],
          "type": "version_conflict_engine_exception",
          "reason": "[blog][1]: version conflict, current [2], provided [1]",
          "index": "website",
          "shard": "3"
       },
       "status": 409
    }
    
    

    外部系统保证安全

    指定版本号需要大于ES当前版本号
    创建外部版本号为5的博客

    PUT /website/blog/2?version=5&version_type=external
    {
      "title": "My first external blog entry",
      "text":  "Starting to get the hang of this..."
    }
    

    成功响应:

    {
      "_index":   "website",
      "_type":    "blog",
      "_id":      "2",
      "_version": 5,
      "created":  true
    }
    

    更新这个文档,指定新版本号

    PUT /website/blog/2?version=10&version_type=external
    {
      "title": "My first external blog entry",
      "text":  "This is a piece of cake..."
    }
    

    成功,失败则返回之前的冲突错误

    {
      "_index":   "website",
      "_type":    "blog",
      "_id":      "2",
      "_version": 10,
      "created":  false
    }
    

    相关文章

      网友评论

          本文标题:PUT

          本文链接:https://www.haomeiwen.com/subject/mgoduqtx.html