美文网首页
ES的document的全量替换、强制创建与删除

ES的document的全量替换、强制创建与删除

作者: WinnXUDONG | 来源:发表于2018-04-05 21:04 被阅读0次

    1、document的全量替换

    (1)语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容
    (2)document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容
    (3)es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document

    #添加
    PUT /test_insex/test_type/4
    {
      "test_field":"test test"
    }
    #全量替换
    PUT /test_insex/test_type/4
    {
      "test_field2":"test test test test2"
    }
    #查询
    GET /test_insex/test_type/4
    #返回
    {
      "_index": "test_insex",
      "_type": "test_type",
      "_id": "4",
      "_version": 2,
      "found": true,
      "_source": {
        "test_field2": "test test test test2"
      }
    }
    
    全量替换过程说明,也可以当做document delete的原理

    2、document的强制创建

    创建文档与全量替换的语法是一样的,有时我们只是想新建文档,不想替换文档,如果强制进行创建呢?

    #语法
    PUT /index/type/id?op_type=create 或者 PUT 
    /index/type/id/_create
    #一般采用后者的方式居多
    #操作
    PUT /test_insex/test_type/4/_create
    {
      "test_field":"test"
    }
    #如果是已存在的情况下去创建则会报错
    

    3、document的删除

    不会直接物理删除,只会将其标记为deleted,但是你已经查询不到的。当数据越来越多的时候,在后台自动删除。

    #语法
    DELETE /index/type/id
    #操作
    DELETE  /test_insex/test_type/4
    #返回
    {
      "found": true,
      "_index": "test_insex",
      "_type": "test_type",
      "_id": "4",
      "_version": 4,
      "result": "deleted",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }
    

    相关文章

      网友评论

          本文标题:ES的document的全量替换、强制创建与删除

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